From 1da6a6bbbb7fbd6e7940b3dcfcf7b82863925dbc Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Mon, 1 Apr 2024 10:36:25 +0800 Subject: [PATCH] rlp: using slices.IndexFunc --- rlp/encode.go | 6 +++++- rlp/typecache.go | 10 ---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/rlp/encode.go b/rlp/encode.go index ffb42b2997..0b311871fd 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -22,6 +22,7 @@ import ( "io" "math/big" "reflect" + "slices" "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" "github.com/holiman/uint256" @@ -351,7 +352,10 @@ func makeStructWriter(typ reflect.Type) (writer, error) { } var writer writer - firstOptionalField := firstOptionalField(fields) + firstOptionalField := slices.IndexFunc(fields, func(f field) bool { return f.optional }) + if firstOptionalField == -1 { + firstOptionalField = len(fields) + } if firstOptionalField == len(fields) { // This is the writer function for structs without any optional fields. writer = func(val reflect.Value, w *encBuffer) error { diff --git a/rlp/typecache.go b/rlp/typecache.go index 3e37c9d2fc..38cea32a42 100644 --- a/rlp/typecache.go +++ b/rlp/typecache.go @@ -159,16 +159,6 @@ func structFields(typ reflect.Type) (fields []field, err error) { return fields, nil } -// firstOptionalField returns the index of the first field with "optional" tag. -func firstOptionalField(fields []field) int { - for i, f := range fields { - if f.optional { - return i - } - } - return len(fields) -} - type structFieldError struct { typ reflect.Type field int