Skip to content

Commit c114751

Browse files
committed
doc: new functions in rlp package
1 parent 7f6289d commit c114751

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

core/types/rlp_backwards_compat.libevm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func testBodyRLPBackwardsCompatibility(t *testing.T, seed uint64) {
175175
body.Withdrawals = tt.withdrawals
176176

177177
// The original [Body] doesn't implement [rlp.Encoder] nor
178-
// [rlp.Decoder] so we can use a methodless equivalent as gold
178+
// [rlp.Decoder] so we can use a methodless equivalent as the gold
179179
// standard.
180180
type withoutMethods Body
181181
wantRLP, err := rlp.EncodeToBytes((*withoutMethods)(body))

rlp/list.libevm.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package 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.
1922
func (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].
2834
func 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.
3948
func (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.
4964
func 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

Comments
 (0)