reflect: add Value.Bytes, Value.SetBytes methods

This allows code that wants to handle
[]byte separately to get at the actual slice
instead of just at individual bytes.
It seems to come up often enough.

R=r
CC=golang-dev
https://golang.org/cl/4942051
This commit is contained in:
Russ Cox 2011-08-23 22:50:08 -04:00
parent 66bedf8221
commit 00d64c7239
2 changed files with 50 additions and 0 deletions

View file

@ -398,6 +398,18 @@ func (v Value) Bool() bool {
return *(*bool)(unsafe.Pointer(&iv.word))
}
// Bytes returns v's underlying value.
// It panics if v's underlying value is not a slice of bytes.
func (v Value) Bytes() []byte {
iv := v.internal()
iv.mustBe(Slice)
typ := iv.typ.toType()
if typ.Elem().Kind() != Uint8 {
panic("reflect.Value.Bytes of non-byte slice")
}
return *(*[]byte)(iv.addr)
}
// CanAddr returns true if the value's address can be obtained with Addr.
// Such values are called addressable. A value is addressable if it is
// an element of a slice, an element of an addressable array,
@ -1224,6 +1236,19 @@ func (v Value) SetBool(x bool) {
*(*bool)(iv.addr) = x
}
// SetBytes sets v's underlying value.
// It panics if v's underlying value is not a slice of bytes.
func (v Value) SetBytes(x []byte) {
iv := v.internal()
iv.mustBeAssignable()
iv.mustBe(Slice)
typ := iv.typ.toType()
if typ.Elem().Kind() != Uint8 {
panic("reflect.Value.SetBytes of non-byte slice")
}
*(*[]byte)(iv.addr) = x
}
// SetComplex sets v's underlying value to x.
// It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
func (v Value) SetComplex(x complex128) {