mus-format / mus-go

A set of serialization primitives for Golang
MIT License
78 stars 3 forks source link

Why using generics in interface definition ? #7

Closed sylr closed 6 months ago

sylr commented 1 year ago

Hi,

I'm interested in go-mus but I don't understand the interfaces defined in this package. I've a lot of structs I'd like to encode/decode and it would be much more easier if the interfaces would look like encoding/json's, i.e.:

type Marshaller interface {
    MarshalMUS() (bs []byte, n int, err error)
}

type Unmarshaller interface {
    UnmarshalMUS(bs []byte) (n int, err error)
}

type Sizer interface {
    SizeMUS() (size int)
}

type Skipper interface {
    SkipMUS(bs []byte) (n int, err error)
}

Is there something I'm missing that dictates the use of generics ?

Regards.

ymz-ncnk commented 1 year ago

Hi,

If I understood you correctly, you mean that it would be more convenient to implement the Marshal, Unmarshal, and Size as structure methods rather than functions. However, in this case we would have to deal with pointers (for example, the receiver of the Unmarshal method would have to be a pointer), which mus-go tries not to use for performance reasons.