rlp: remove workaround for Value.Bytes (#32433)

As of Go 1.19, it is permitted to call Bytes() on a reflect.Value
representing an adressable byte array. So we can remove our workaround,
undoing #22924.

https://go.dev/doc/go1.19#reflectpkgreflect

> The method [Value.Bytes](https://go.dev/pkg/reflect/#Value.Bytes) now
accepts addressable arrays in addition to slices.
This commit is contained in:
Felix Lange 2025-08-15 14:08:27 +02:00 committed by GitHub
parent 1d29e3ec0e
commit 1693a48f8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 2 additions and 60 deletions

View file

@ -371,7 +371,7 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
if err != nil {
return err
}
slice := byteArrayBytes(val, val.Len())
slice := val.Bytes()
switch kind {
case Byte:
if len(slice) == 0 {

View file

@ -240,7 +240,6 @@ func makeByteArrayWriter(typ reflect.Type) writer {
case 1:
return writeLengthOneByteArray
default:
length := typ.Len()
return func(val reflect.Value, w *encBuffer) error {
if !val.CanAddr() {
// Getting the byte slice of val requires it to be addressable. Make it
@ -249,7 +248,7 @@ func makeByteArrayWriter(typ reflect.Type) writer {
copy.Set(val)
val = copy
}
slice := byteArrayBytes(val, length)
slice := val.Bytes()
w.encodeStringHeader(len(slice))
w.str = append(w.str, slice...)
return nil

View file

@ -1,27 +0,0 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build nacl || js || !cgo
// +build nacl js !cgo
package rlp
import "reflect"
// byteArrayBytes returns a slice of the byte array v.
func byteArrayBytes(v reflect.Value, length int) []byte {
return v.Slice(0, length).Bytes()
}

View file

@ -1,30 +0,0 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build !nacl && !js && cgo
// +build !nacl,!js,cgo
package rlp
import (
"reflect"
"unsafe"
)
// byteArrayBytes returns a slice of the byte array v.
func byteArrayBytes(v reflect.Value, length int) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(v.UnsafeAddr())), length)
}