Skip to content

Commit

Permalink
Use unsafe.Slice where possible (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuonglm committed Aug 26, 2022
1 parent 87f040c commit b8d8d44
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
6 changes: 1 addition & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "C"

import (
"fmt"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -2472,10 +2471,7 @@ func LoadLatestOptions(path string, env *Env, ignoreUnknownOpts bool, cache *Cac

if err = fromCError(cErr); err == nil {
// convert **C.rocksdb_options_t into []Options
var cfOptions_ []*C.rocksdb_options_t
sH := (*reflect.SliceHeader)(unsafe.Pointer(&cfOptions_))
sH.Cap, sH.Len, sH.Data = int(numCF), int(numCF), uintptr(unsafe.Pointer(cfOpts))

cfOptions_ := unsafe.Slice(cfOpts, int(numCF))
cfOptions := make([]Options, int(numCF))
for i := range cfOptions {
cfOptions[i] = Options{c: cfOptions_[i]}
Expand Down
16 changes: 3 additions & 13 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package grocksdb
import "C"
import (
"errors"
"reflect"
"unsafe"
)

Expand All @@ -24,10 +23,7 @@ func charToBool(c C.uchar) bool {

// charToByte converts a *C.char to a byte slice.
func charToByte(data *C.char, len C.size_t) []byte {
var value []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
return unsafe.Slice((*byte)(unsafe.Pointer(data)), int(len))
}

// byteToChar returns *C.char from byte slice.
Expand All @@ -51,10 +47,7 @@ func cByteSlice(b []byte) *C.char {

// charSlice converts a C array of *char to a []*C.char.
func charSlice(data **C.char, len C.int) []*C.char {
var value []*C.char
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
return unsafe.Slice(data, int(len))
}

// charSliceIntoStringSlice converts a C array of *char to a []string.
Expand All @@ -71,10 +64,7 @@ func charSliceIntoStringSlice(data **C.char, len C.int) []string {

// sizeSlice converts a C array of size_t to a []C.size_t.
func sizeSlice(data *C.size_t, len C.int) []C.size_t {
var value []C.size_t
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
return unsafe.Slice(data, int(len))
}

// fromCError returns go error and free c_err if need.
Expand Down

0 comments on commit b8d8d44

Please sign in to comment.