Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Efficient byte arrays in Go #18

Closed
riptl opened this issue Sep 7, 2022 · 2 comments
Closed

[Feature Request] Efficient byte arrays in Go #18

riptl opened this issue Sep 7, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@riptl
Copy link

riptl commented Sep 7, 2022

馃殌 Feature Request

Motivation

Code generation is inefficient for static size byte arrays

Hash:
  NEWTYPESTRUCT:
    TUPLEARRAY:
      CONTENT: U8
      SIZE: 32

generates

type Hash [32]uint8

func (obj *Hash) Serialize(serializer serde.Serializer) error {
	if err := serializer.IncreaseContainerDepth(); err != nil {
		return err
	}
	if err := serialize_array32_u8_array((([32]uint8)(*obj)), serializer); err != nil {
		return err
	}
	serializer.DecreaseContainerDepth()
	return nil
}

func serialize_array32_u8_array(value [32]uint8, serializer serde.Serializer) error {
	for _, item := range value {
		if err := serializer.SerializeU8(item); err != nil {
			return err
		}
	}
	return nil
}

Pitch

Describe the solution you'd like

  • Extend serde Go interface to be aware of byte arrays
  • Implement bincode serde for byte arrays with a single function call

Describe alternatives you've considered

none

Are you willing to open a pull request? (See CONTRIBUTING)

Maybe later

Additional context

@riptl riptl added the enhancement New feature or request label Sep 7, 2022
@ma2bd
Copy link
Contributor

ma2bd commented Dec 31, 2022

Assuming that you are ok with []byte, you may use serde_bytes in Rust so that the schema uses the primitive type Bytes. Then, the code generator will create a []byte in Go:
https://github.com/zefchain/serde-reflection/blob/main/serde-generate/src/golang.rs#L222

If you want [32]byte, then the answer is more complicated. Probably just add conversion methods to and from [32]uint8 in Go. There are ways to inject custom code during code generation.

@ma2bd
Copy link
Contributor

ma2bd commented Dec 31, 2022

I'm going to close the issue. Few languages have static-size arrays and Rust doesn't have a Byte type anyway.

@ma2bd ma2bd closed this as completed Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants