MCPcopy
hub / github.com/tinylib/msgp / EncoderToBytes

Function EncoderToBytes

msgp/iter.go:371–386  ·  view source on GitHub ↗

EncoderToBytes allows augmenting any type with a MarshalMsg method into a method that reads from T and returns a []byte. Provide an instance of T. This value isn't used. See ReadArrayBytes or ReadMapBytes "struct" examples for usage.

(_ T)

Source from the content-addressed store, hash-verified

369// Provide an instance of T. This value isn't used.
370// See ReadArrayBytes or ReadMapBytes "struct" examples for usage.
371func EncoderToBytes[T any, _ FlexibleMarshaler[T]](_ T) func([]byte, T) []byte {
372 return func(b []byte, t T) []byte {
373 // Check if T implements Marshaler
374 if marshaler, ok := any(t).(Marshaler); ok {
375 b, _ = marshaler.MarshalMsg(b)
376 return b
377 }
378 // Check if *T implements Marshaler
379 if ptrMarshaler, ok := any(&t).(Marshaler); ok {
380 b, _ = ptrMarshaler.MarshalMsg(b)
381 return b
382 }
383 // The compiler should have asserted this.
384 panic("type does not implement Marshaler")
385 }
386}

Callers 2

Calls 1

MarshalMsgMethod · 0.65

Tested by 2

Used in the wild real call sites across dependent graphs

searching dependent graphs…