remove ugly cast
This commit is contained in:
parent
11d80282c1
commit
1717743ce7
|
@ -32,18 +32,18 @@ proc main() =
|
||||||
var writeOptions = rocksdb_writeoptions_create()
|
var writeOptions = rocksdb_writeoptions_create()
|
||||||
let key = "key"
|
let key = "key"
|
||||||
let put_value = "value"
|
let put_value = "value"
|
||||||
rocksdb_put(db, writeOptions, key.cstring, cast[csize_t](key.len), put_value.cstring, cast[csize_t](put_value.len), err.addr)
|
rocksdb_put(db, writeOptions, key.cstring, key.len.csize_t, put_value.cstring, put_value.len.csize_t, err.addr)
|
||||||
doAssert err.isNil, $err
|
doAssert err.isNil, $err
|
||||||
|
|
||||||
# Get value
|
# Get value
|
||||||
var readOptions = rocksdb_readoptions_create()
|
var readOptions = rocksdb_readoptions_create()
|
||||||
var len: csize_t
|
var len: csize_t
|
||||||
let raw_value = rocksdb_get(db, readOptions, key, cast[csize_t](key.len), addr len, err.addr) # Important: rocksdb_get is not null-terminated
|
let raw_value = rocksdb_get(db, readOptions, key, key.len.csize_t, addr len, err.addr) # Important: rocksdb_get is not null-terminated
|
||||||
doAssert err.isNil, $err
|
doAssert err.isNil, $err
|
||||||
|
|
||||||
# Copy it to a regular Nim string (copyMem workaround because raw value is NOT null-terminated)
|
# Copy it to a regular Nim string (copyMem workaround because raw value is NOT null-terminated)
|
||||||
var get_value = newString(cast[int](len))
|
var get_value = newString(len.int)
|
||||||
copyMem(addr get_value[0], unsafeAddr raw_value[0], cast[int](len) * sizeof(char))
|
copyMem(addr get_value[0], unsafeAddr raw_value[0], len.int * sizeof(char))
|
||||||
|
|
||||||
doAssert get_value == put_value
|
doAssert get_value == put_value
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue