Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to use reflect.NewAt() as opposed to unsafe.Unreflect()

  • Loading branch information...
commit f3458301e4334cb0f17feff75810ad57130a17e2 1 parent f6908c0
@feyeleanor feyeleanor authored
View
14 byte_slice.go
@@ -4,8 +4,11 @@ package raw
// THE PACKAGE SHOULD BE INSTALLABLE VIA GOINSTALL
// AND NEEDS EXTENSIVE TESTS!!!!
-import "reflect"
-import "unsafe"
+import (
+ "reflect"
+ "unsafe"
+)
+
var _BYTE_SLICE reflect.Type
var _STRING reflect.Type
@@ -60,7 +63,12 @@ func ByteSlice(i interface{}) []byte {
// then create a []byte sliceheader and return a valid slice
header = valueHeader(value)
}
- return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
+
+ bs := reflect.NewAt(_BYTE_SLICE, unsafe.Pointer(header))
+ if !bs.Elem().IsValid() {
+ return []byte{}
+ }
+ return bs.Elem().Interface().([]byte)
}
func valueHeader(v reflect.Value) (h *reflect.SliceHeader) {
View
9 byte_slice_test.go
@@ -1,8 +1,11 @@
package raw
-import "reflect"
-import "testing"
-import "unsafe"
+import (
+ "reflect"
+ "testing"
+ "unsafe"
+)
+
func TestByteSliceWithNil(t *testing.T) {
b := ByteSlice(nil)
View
10 reslice.go
@@ -1,7 +1,9 @@
package raw
-import "reflect"
-import "unsafe"
+import (
+ "reflect"
+ "unsafe"
+)
func sliceHeaderFromValue(v reflect.Value) (s *reflect.SliceHeader) {
switch v.Kind() {
@@ -38,10 +40,10 @@ func Scale(oldHeader *reflect.SliceHeader, oldElementSize, newElementSize int) (
return
}
-func Reslice(slice, sliceType interface{}, elementSize int) interface{} {
+func Reslice(slice interface{}, sliceType reflect.Type, elementSize int) interface{} {
b := ByteSlice(slice)
h := Scale(&reflect.SliceHeader{ uintptr(DataAddress(b)), len(b), cap(b) }, 1, elementSize)
- return unsafe.Unreflect(sliceType, unsafe.Pointer(h))
+ return reflect.NewAt(sliceType, unsafe.Pointer(h)).Elem().Interface()
}
func PointerSlice(i interface{}) []unsafe.Pointer {
View
10 reslice_test.go
@@ -1,9 +1,11 @@
package raw
-import "fmt"
-import "reflect"
-import "testing"
-import "unsafe"
+import (
+ "fmt"
+ "reflect"
+ "testing"
+ "unsafe"
+)
type HeaderMismatch struct{
View
8 type.go
@@ -1,8 +1,10 @@
package raw
-import "fmt"
-import "reflect"
-import "unsafe"
+import (
+ "fmt"
+ "reflect"
+ "unsafe"
+)
func ConcreteValue(value interface{}) (r reflect.Value) {
for r = reflect.ValueOf(value); r.Kind() == reflect.Ptr; r = reflect.Indirect(r) {}
Please sign in to comment.
Something went wrong with that request. Please try again.