mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-14 07:53:40 +00:00
reflect: add SetBytes()
This commit is contained in:
committed by
Damian Gryski
parent
0da97e2427
commit
91d6ca057c
@@ -982,7 +982,13 @@ func (v Value) SetString(x string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v Value) SetBytes(x []byte) {
|
func (v Value) SetBytes(x []byte) {
|
||||||
panic("unimplemented: (reflect.Value).SetBytes()")
|
v.checkAddressable()
|
||||||
|
if v.typecode.Kind() != Slice || v.typecode.elem().Kind() != Uint8 {
|
||||||
|
panic("reflect.Value.SetBytes called on not []byte")
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy the header contents over
|
||||||
|
*(*sliceHeader)(v.value) = *(*sliceHeader)(unsafe.Pointer(&x))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Value) SetCap(n int) {
|
func (v Value) SetCap(n int) {
|
||||||
|
|||||||
@@ -321,6 +321,20 @@ func TestNilType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetBytes(t *testing.T) {
|
||||||
|
var b []byte
|
||||||
|
refb := ValueOf(&b).Elem()
|
||||||
|
s := []byte("hello")
|
||||||
|
refb.SetBytes(s)
|
||||||
|
s[0] = 'b'
|
||||||
|
|
||||||
|
refbSlice := refb.Interface().([]byte)
|
||||||
|
|
||||||
|
if len(refbSlice) != len(s) || b[0] != s[0] || refbSlice[0] != s[0] {
|
||||||
|
t.Errorf("SetBytes(): reflection slice mismatch")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func equal[T comparable](a, b []T) bool {
|
func equal[T comparable](a, b []T) bool {
|
||||||
if len(a) != len(b) {
|
if len(a) != len(b) {
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user