Skip to content
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

replace shallowCopy with sink parameters #67

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions msgpack4nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ proc init*(x: typedesc[MsgStream], cap: int = 0, encodingMode = MSGPACK_OBJ_TO_D
result.pos = 0
result.encodingMode = encodingMode

proc init*(x: typedesc[MsgStream], data: string, encodingMode = MSGPACK_OBJ_TO_DEFAULT): MsgStream =
proc init*(x: typedesc[MsgStream], data: sink string, encodingMode = MSGPACK_OBJ_TO_DEFAULT): MsgStream =
result = new(x)
shallowCopy(result.data, data)
result.data = data
result.pos = 0
result.encodingMode = encodingMode

Expand All @@ -73,9 +73,8 @@ proc writeData(s: MsgStream, buffer: pointer, bufLen: int) =
copyMem(addr(s.data[s.pos]), buffer, bufLen)
inc(s.pos, bufLen)

proc write*[T](s: MsgStream, val: T) =
var y: T
shallowCopy(y, val)
proc write*[T](s: MsgStream, val: sink T) =
var y = val
writeData(s, addr(y), sizeof(y))

proc write*(s: MsgStream, val: string) =
Expand Down Expand Up @@ -794,14 +793,9 @@ proc unpack_string*[ByteStream](s: ByteStream): int =
result = int(s.unstore32())

proc unpack_type*[ByteStream](s: ByteStream, val: var string) =
when compiles(isNil(val)):
if s.peekChar == pack_value_nil:
val = nil
return
else:
if s.peekChar == pack_value_nil:
val = ""
return
if s.peekChar == pack_value_nil:
val = ""
return

let len = s.unpack_string()
if len < 0: raise conversionError("string")
Expand Down