1616
1717package rlp
1818
19+ // InList is a convenience wrapper, calling `fn` between calls to
20+ // [EncoderBuffer.List] and [EncoderBuffer.ListEnd]. If `fn` returns an error,
21+ // it is propagated directly.
1922func (b EncoderBuffer ) InList (fn func () error ) error {
2023 l := b .List ()
2124 if err := fn (); err != nil {
@@ -25,6 +28,9 @@ func (b EncoderBuffer) InList(fn func() error) error {
2528 return nil
2629}
2730
31+ // EncodeListToBuffer is equivalent to [Encode], writing the RLP encoding of
32+ // each element to `b`, except that it wraps the writes inside a call to
33+ // [EncoderBuffer.InList].
2834func EncodeListToBuffer [T any ](b EncoderBuffer , vals []T ) error {
2935 return b .InList (func () error {
3036 for _ , v := range vals {
@@ -36,6 +42,9 @@ func EncodeListToBuffer[T any](b EncoderBuffer, vals []T) error {
3642 })
3743}
3844
45+ // FromList is a convenience wrapper, calling `fn` between calls to
46+ // [Stream.List] and [Stream.ListEnd]. If `fn` returns an error, it is
47+ // propagated directly.
3948func (s * Stream ) FromList (fn func () error ) error {
4049 if _ , err := s .List (); err != nil {
4150 return err
@@ -46,14 +55,13 @@ func (s *Stream) FromList(fn func() error) error {
4655 return s .ListEnd ()
4756}
4857
58+ // DecodeList assumes that the next item in `s` is a list and decodes every item
59+ // in said list to a `*T`.
60+ //
61+ // The returned slice is guaranteed to be non-nil, even if the list is empty.
62+ // This is in keeping with other behaviour in this package and it is therefore
63+ // the responsibility of callers to respect `rlp:"nil"` struct tags.
4964func DecodeList [T any ](s * Stream ) ([]* T , error ) {
50- // From the package-level documentation:
51- //
52- // > Note that package rlp never leaves a pointer-type struct field as nil
53- // > unless one of the "nil" struct tags is present.
54- //
55- // We therefore return a non-nil pointer to maintain said invariant as it
56- // makes use of this function easier.
5765 vals := make ([]* T , 0 )
5866 err := s .FromList (func () error {
5967 for s .MoreDataInList () {
0 commit comments