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

Function EncoderTo

msgp/iter.go:327–340  ·  view source on GitHub ↗

EncoderTo allows augmenting any type with an EncodeMsg method into a method that writes to Writer on each call. Provide an instance of T. This value isn't used. See ReadArray or ReadMap "struct" examples for usage.

(w *Writer, _ T)

Source from the content-addressed store, hash-verified

325// Provide an instance of T. This value isn't used.
326// See ReadArray or ReadMap "struct" examples for usage.
327func EncoderTo[T any, _ FlexibleEncoder[T]](w *Writer, _ T) func(T) error {
328 return func(t T) error {
329 // Check if T implements Marshaler
330 if marshaler, ok := any(t).(Encodable); ok {
331 return marshaler.EncodeMsg(w)
332 }
333 // Check if *T implements Marshaler
334 if ptrMarshaler, ok := any(&t).(Encodable); ok {
335 return ptrMarshaler.EncodeMsg(w)
336 }
337 // The compiler should have asserted this.
338 panic("type does not implement Marshaler")
339 }
340}
341
342// UnmarshalPtr is a convenience type for unmarshaling into a pointer.
343type UnmarshalPtr[T any] interface {

Callers 2

ExampleReadArray_structFunction · 0.85
ExampleReadMap_structFunction · 0.85

Calls 1

EncodeMsgMethod · 0.65

Tested by 2

ExampleReadArray_structFunction · 0.68
ExampleReadMap_structFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…