]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: add Value.Bytes, Value.SetBytes methods
authorRuss Cox <rsc@golang.org>
Wed, 24 Aug 2011 02:50:08 +0000 (22:50 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 24 Aug 2011 02:50:08 +0000 (22:50 -0400)
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

src/pkg/reflect/all_test.go
src/pkg/reflect/value.go

index d63e986fde58939674a4c8b43918e5031108defa..610ba4b6674b6959412a0422e87023b21081885f 100644 (file)
@@ -1562,3 +1562,28 @@ func TestTagGet(t *testing.T) {
                }
        }
 }
+
+func TestBytes(t *testing.T) {
+       type B []byte
+       x := B{1, 2, 3, 4}
+       y := ValueOf(x).Bytes()
+       if !bytes.Equal(x, y) {
+               t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
+       }
+       if &x[0] != &y[0] {
+               t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
+       }
+}
+
+func TestSetBytes(t *testing.T) {
+       type B []byte
+       var x B
+       y := []byte{1, 2, 3, 4}
+       ValueOf(&x).Elem().SetBytes(y)
+       if !bytes.Equal(x, y) {
+               t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
+       }
+       if &x[0] != &y[0] {
+               t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
+       }
+}
index d3c510ac2d15bcdce9d8cf42b49261339ba929db..99b1f24eafd070a5a2653291b97fee1403a51cdb 100644 (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) {