diff options
author | Joe Tsai <joetsai@digital-static.net> | 2021-09-04 13:02:09 -0700 |
---|---|---|
committer | Joseph Tsai <joetsai@digital-static.net> | 2022-03-02 18:06:42 +0000 |
commit | a5b8b56d1d05d186999e4abf1e2147b6aa203ec9 (patch) | |
tree | 78a3ef01c71bcf0a173efd7087208cebee92039b /src/reflect/value.go | |
parent | ec4687f337465b719efdeef72b357fa0b05879bb (diff) | |
download | go-a5b8b56d1d05d186999e4abf1e2147b6aa203ec9.tar.gz go-a5b8b56d1d05d186999e4abf1e2147b6aa203ec9.zip |
reflect: allow Value.Bytes on addressable byte arrays
Modify Value.Bytes to be callable addressable byte arrays.
While related, the behavior of Value.SetBytes was not modified.
Fixes #47066
Change-Id: Ic3ba4432353b8da5f33b3188e20034a33b2f6ee8
Reviewed-on: https://go-review.googlesource.com/c/go/+/357331
Trust: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r-- | src/reflect/value.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index dcc359dae4..89f0253570 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -286,14 +286,28 @@ func (v Value) Bool() bool { } // Bytes returns v's underlying value. -// It panics if v's underlying value is not a slice of bytes. +// It panics if v's underlying value is not a slice of bytes or +// an addressable array of bytes. func (v Value) Bytes() []byte { - v.mustBe(Slice) - if v.typ.Elem().Kind() != Uint8 { - panic("reflect.Value.Bytes of non-byte slice") + switch v.kind() { + case Slice: + if v.typ.Elem().Kind() != Uint8 { + panic("reflect.Value.Bytes of non-byte slice") + } + // Slice is always bigger than a word; assume flagIndir. + return *(*[]byte)(v.ptr) + case Array: + if v.typ.Elem().Kind() != Uint8 { + panic("reflect.Value.Bytes of non-byte array") + } + if !v.CanAddr() { + panic("reflect.Value.Bytes of unaddressable byte array") + } + p := (*byte)(v.ptr) + n := int((*arrayType)(unsafe.Pointer(v.typ)).len) + return unsafe.Slice(p, n) } - // Slice is always bigger than a word; assume flagIndir. - return *(*[]byte)(v.ptr) + panic(&ValueError{"reflect.Value.Bytes", v.kind()}) } // runes returns v's underlying value. |