WriteArray writes an array to the provided Writer. The writeFn parameter specifies the function to use to write each element of the array.
(w *Writer, a []T, writeFn func(T) error)
| 42 | // WriteArray writes an array to the provided Writer. |
| 43 | // The writeFn parameter specifies the function to use to write each element of the array. |
| 44 | func WriteArray[T any](w *Writer, a []T, writeFn func(T) error) error { |
| 45 | // Check if nil |
| 46 | if a == nil { |
| 47 | return w.WriteNil() |
| 48 | } |
| 49 | if uint64(len(a)) > math.MaxUint32 { |
| 50 | return fmt.Errorf("array too large to encode: %d elements", len(a)) |
| 51 | } |
| 52 | // Write array header |
| 53 | err := w.WriteArrayHeader(uint32(len(a))) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | // Write elements |
| 58 | for _, v := range a { |
| 59 | err = writeFn(v) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // ReadMap returns an iterator that can be used to iterate over the elements |
| 68 | // of a map in the MessagePack data while being read by the provided Reader. |
searching dependent graphs…