-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reflect: add SliceAt function #61308
Comments
It seems to me that the new functionality is to func SliceAt(typ Type, p unsafe.Pointer, len int) Value That has the additional advantage of passing through the type |
Why does the proposed |
Those functions were introduced starting in 1.17 and then more in 1.20.
That won't work. reflect.ValueOf(unsafe.String((*byte)(unsafe.Pointer(v.Pointer())), v.Len()))
reflect.ValueOf(unsafe.Slice((*T)(unsafe.Pointer(v.Pointer())), v.Len())) |
Because |
For the string case, reflect.ValueOf(unsafe.String(v.Interface().(*byte), v.Len())) or reflect.ValueOf(unsafe.String((*byte)(v.UnsafePointer()), v.Len())) will often suffice. |
I think the function name should be
|
Change https://go.dev/cl/516597 mentions this issue: |
Change https://go.dev/cl/516596 mentions this issue: |
Another reason is that these are in fact unsafe, so the name should begin with Unsafe to distinguish it from the normal, safe reflect API. |
It seems like we should also update UnsafePointer to handle String (not just update Pointer). |
This proposal has been added to the active column of the proposals project |
Have all remaining concerns about this proposal been addressed? Two parts to the proposal. First, add func UnsafeSlice, mimicking unsafe.Slice. Second, update Value.Pointer and Value.UnsafePointer to handle String. |
I don't think the signature concern in #61308 (comment) has been addressed yet. The other functions and methods in the func Slice(vp Value, n int) Value (as described in #61308 (comment)) does not have that property. |
I'm not sure that's quite true of the existing Note that the proposed function is now named |
I also prefer @bcmills ' alternate signature. It has the clear advantage of using |
Have all remaining concerns about this proposal been addressed? Two parts to the proposal. First, add func SliceAt, analogous to NewAt (which probably should have been called ValueAt) but for slices:
Second, update Value.Pointer and Value.UnsafePointer to handle String. |
Based on the discussion above, this proposal seems like a likely accept. Two parts to the proposal. First, add func SliceAt, analogous to NewAt (which probably should have been called ValueAt) but for slices:
Second, update Value.Pointer and Value.UnsafePointer to handle String. |
For golang#61308 Change-Id: Ib799bf536eef9df63bf2c56c28bd62c286960a69
Change https://go.dev/cl/572117 mentions this issue: |
DO NOT SUBMIT Fixes golang#61308 Change-Id: I707e788591aa4545ceef340aab424a9ef24dcc07
Change https://go.dev/cl/572118 mentions this issue: |
Does
|
Yes, reflect.SliceAt should be no more unsafe than unsafe.Slice. So it should do the same validations. |
No change in consensus, so accepted. 🎉 Two parts to the proposal. First, add func SliceAt, analogous to NewAt (which probably should have been called ValueAt) but for slices:
Second, update Value.Pointer and Value.UnsafePointer to handle String. |
Updates #61308 Change-Id: I92d459383c520d137787ce5c8f135d205af74e5d Reviewed-on: https://go-review.googlesource.com/c/go/+/516596 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
Change https://go.dev/cl/575956 mentions this issue: |
CL 516596 changes Value.UnsafePointer to handle String case. However, the method's doc comment is not updated to reflect this change. Updates #61308 Change-Id: I84e02fd969ae0244184e1a2f05cac4651cdf7bff Reviewed-on: https://go-review.googlesource.com/c/go/+/575956 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
Change https://go.dev/cl/592197 mentions this issue: |
For #61308. For #65614. Change-Id: I36b4f2392075d5a3fb9f53a28bd19b997e7be363 Reviewed-on: https://go-review.googlesource.com/c/go/+/592197 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Bypass: Dmitri Shuralyov <[email protected]>
In Go 1.21 we are introducing some new functions in the unsafe package:
unsafe.Slice
,unsafe.SliceData
,unsafe.String
,unsafe.StringData
. It should be possible to access this functionality through the reflect package as well.The value returned by
unsafe.SliceData
is already available by calling thereflect.Value.Pointer
method.I propose that we extend
reflect.Value.Pointer
so that if it is called on a value of kindString
, it returns the equivalent ofunsafe.StringData
.The equivalent to
unsafe.String
is simplyreflect.ValueOf(unsafe.String((*byte)(vp.UnsafePointer()), n))
, so I don't think we need to add anything for that.I propose one new function:
(edited to change function name from
Slice
to UnsafeSlice`)The text was updated successfully, but these errors were encountered: