Skip to content

Commit 2387a92

Browse files
reflect: add SliceAt
DO NOT SUBMIT Fixes #61308 Change-Id: I707e788591aa4545ceef340aab424a9ef24dcc07
1 parent 68d3a9e commit 2387a92

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/reflect/all_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -8512,3 +8512,19 @@ func TestClear(t *testing.T) {
85128512
})
85138513
}
85148514
}
8515+
8516+
func TestSliceAt(t *testing.T) {
8517+
s := make([]string, 10)
8518+
et := TypeOf(s).Elem()
8519+
len := 5
8520+
v := SliceAt(et, unsafe.Pointer(unsafe.SliceData(s)), len)
8521+
if v.Len() != len {
8522+
t.Fatalf("Value.Len() got %d, want %d", v.Len(), len)
8523+
}
8524+
if v.Cap() != len {
8525+
t.Fatalf("Value.Cap() got %d, want %d", v.Cap(), len)
8526+
}
8527+
if v.UnsafePointer() != unsafe.Pointer(unsafe.SliceData(s)) {
8528+
t.Fatalf("v.UnsafePointer(%p) != unsafe.SliceData(%p)", v.UnsafePointer(), unsafe.SliceData(s))
8529+
}
8530+
}

src/reflect/value.go

+12
Original file line numberDiff line numberDiff line change
@@ -3297,6 +3297,18 @@ func New(typ Type) Value {
32973297
return Value{pt, ptr, fl}
32983298
}
32993299

3300+
// SliceAt returns a Value representing
3301+
// a slice whose underlying data starts at p,
3302+
// with length and capacity equal to len.
3303+
// This is like unsafe.Slice.
3304+
func SliceAt(typ Type, p unsafe.Pointer, len int) Value {
3305+
fl := flag(Slice)
3306+
st := SliceOf(typ)
3307+
s := &unsafeheader.Slice{Data: p, Len: len, Cap: len}
3308+
t := &st.(*rtype).t
3309+
return Value{t, unsafe.Pointer(s), fl}
3310+
}
3311+
33003312
// NewAt returns a Value representing a pointer to a value of the
33013313
// specified type, using p as that pointer.
33023314
func NewAt(typ Type, p unsafe.Pointer) Value {

0 commit comments

Comments
 (0)