Skip to content

Commit

Permalink
doc: new functions in rlp package
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Jan 31, 2025
1 parent 7f6289d commit c114751
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/types/rlp_backwards_compat.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func testBodyRLPBackwardsCompatibility(t *testing.T, seed uint64) {
body.Withdrawals = tt.withdrawals

// The original [Body] doesn't implement [rlp.Encoder] nor
// [rlp.Decoder] so we can use a methodless equivalent as gold
// [rlp.Decoder] so we can use a methodless equivalent as the gold
// standard.
type withoutMethods Body
wantRLP, err := rlp.EncodeToBytes((*withoutMethods)(body))
Expand Down
22 changes: 15 additions & 7 deletions rlp/list.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package rlp

// InList is a convenience wrapper, calling `fn` between calls to
// [EncoderBuffer.List] and [EncoderBuffer.ListEnd]. If `fn` returns an error,
// it is propagated directly.
func (b EncoderBuffer) InList(fn func() error) error {
l := b.List()
if err := fn(); err != nil {
Expand All @@ -25,6 +28,9 @@ func (b EncoderBuffer) InList(fn func() error) error {
return nil
}

// EncodeListToBuffer is equivalent to [Encode], writing the RLP encoding of
// each element to `b`, except that it wraps the writes inside a call to
// [EncoderBuffer.InList].
func EncodeListToBuffer[T any](b EncoderBuffer, vals []T) error {
return b.InList(func() error {
for _, v := range vals {
Expand All @@ -36,6 +42,9 @@ func EncodeListToBuffer[T any](b EncoderBuffer, vals []T) error {
})
}

// FromList is a convenience wrapper, calling `fn` between calls to
// [Stream.List] and [Stream.ListEnd]. If `fn` returns an error, it is
// propagated directly.
func (s *Stream) FromList(fn func() error) error {
if _, err := s.List(); err != nil {
return err
Expand All @@ -46,14 +55,13 @@ func (s *Stream) FromList(fn func() error) error {
return s.ListEnd()
}

// DecodeList assumes that the next item in `s` is a list and decodes every item
// in said list to a `*T`.
//
// The returned slice is guaranteed to be non-nil, even if the list is empty.
// This is in keeping with other behaviour in this package and it is therefore
// the responsibility of callers to respect `rlp:"nil"` struct tags.
func DecodeList[T any](s *Stream) ([]*T, error) {
// From the package-level documentation:
//
// > Note that package rlp never leaves a pointer-type struct field as nil
// > unless one of the "nil" struct tags is present.
//
// We therefore return a non-nil pointer to maintain said invariant as it
// makes use of this function easier.
vals := make([]*T, 0)
err := s.FromList(func() error {
for s.MoreDataInList() {
Expand Down

0 comments on commit c114751

Please sign in to comment.