Skip to content

Commit

Permalink
unsafe helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfOne committed Sep 12, 2014
1 parent e8c3966 commit 4de148b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions unsafe/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !appengine

package unsafe

import (
"reflect"
"unsafe"
)

// ByteSlice returns s as a byte slice without copying,
// any attempt to modifythe resulting slice might end all life as we know.
func ByteSlice(s *string) []byte {
sh := (*reflect.SliceHeader)(unsafe.Pointer(s))
sh.Cap = sh.Len
return *(*[]byte)(unsafe.Pointer(sh))
}

// VoidToBytes is a helper function to convert a void* buffer from a cgo call
// to a byte slice
func VoidToBytes(p unsafe.Pointer, ln int) []byte {
sh := &reflect.SliceHeader{
Data: uintptr(p),
Len: ln,
Cap: ln,
}
return *(*[]byte)(unsafe.Pointer(sh))
}

0 comments on commit 4de148b

Please sign in to comment.