reflect: add Bytes()

This commit is contained in:
Damian Gryski
2023-02-25 17:51:27 -08:00
committed by Ayke
parent 12e3d1d81d
commit d98c0afbab
2 changed files with 52 additions and 1 deletions
+21 -1
View File
@@ -378,7 +378,27 @@ func (v Value) String() string {
}
func (v Value) Bytes() []byte {
panic("unimplemented: (reflect.Value).Bytes()")
switch v.Kind() {
case Slice:
if v.typecode.elem().Kind() != Uint8 {
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
return *(*[]byte)(v.value)
case Array:
v.checkAddressable()
if v.typecode.elem().Kind() != Uint8 {
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
// Small inline arrays are not addressable, so we only have to
// handle addressable arrays which will be stored as pointers
// in v.value
return unsafe.Slice((*byte)(v.value), v.Len())
}
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
func (v Value) Slice(i, j int) Value {