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)
| 325 | // Provide an instance of T. This value isn't used. |
| 326 | // See ReadArray or ReadMap "struct" examples for usage. |
| 327 | func 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. |
| 343 | type UnmarshalPtr[T any] interface { |
searching dependent graphs…