[Haskell-cafe] ByteString FFI

Donald Bruce Stewart dons at cse.unsw.edu.au
Sun Nov 12 20:47:31 EST 2006


donn:
> How do people like to set up their foreign I/O functions to return
> ByteStrings?  I was a little stumped over this yesterday evening,
> while trying to write ` recv :: Socket -> Int -> Int -> ByteString '
> 
> Doc says `Byte vectors are encoded as strict Word8 arrays of bytes,
> held in a ForeignPtr, and can be passed between C and Haskell with
> little effort.'  Which sounds perfect - I'm always up for `little effort'!
> 
> CString doesn't seem like the right thing for socket results, because
> the data shouldn't be NUL-terminated, and I might want to realloc when
> the returned data doesn't fill the buffer.  I don't see any other
> Ptr-related function or constructor in the documentation - am I missing
> something there?

And for custom data (not just C strings), if the withCString* functions
don't quite fit, you can always pack the foreign Ptr into a ByteString
by stepping inside the ByteString constructor:

    http://www.haskell.org/haskellwiki/Wc#Going_via_C

-- Don


More information about the Haskell-Cafe mailing list