I am trying to use nanomsg from Nim. I am trying to read a message, and in order to do that, nanomsg requires me to provide a buffer as a pointer, and then writes into this buffer (and has its own freemsg function).
The example does something like
var buf: cstring
let count = socket.recv(addr(buf), MSG, 0)
At this point I have thing of type cstring which holds the bytes of the message. Note that the message itself could well contain nulls, so it is not a cstring literally - the correct length is returned by socket.recv.
What I would like to have is a seq[char] or something like that. Is there a way to do that without copying and without messing with Nim GC mechanism? For instance, I guess that blindly writing into the pointer to the seq data will cause havoc, since nanomsg does its own realloc behind the scenes (at least I guess).
It would at first even be ok to perform a copy, but my first attempt
var s = newSeq[char](length)
copyMem(addr(s[0]), addr(buf), length)
echo s
apparently fails.
Well, you need to do it properly ;-)
var s = newSeq[char](length)
copyMem(addr(s[0]), buf, length)
echo s