zefchain / serde-reflection

Rust libraries and tools to help with interoperability and testing of serialization formats based on Serde.
Apache License 2.0
138 stars 26 forks source link

[Feature Request] Efficient byte arrays in Go #18

Closed riptl closed 1 year ago

riptl commented 2 years ago

🚀 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

Describe alternatives you've considered

none

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

Maybe later

Additional context

ma2bd commented 1 year ago

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 commented 1 year ago

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