Skip to content

Provide CString/ByteString conversions for Bytes #26

@jac3km4

Description

@jac3km4

I think they'd be pretty useful when using stdio with other libraries, I needed to define them in order to use proto-lens.
Those seem to work for me:

import           Std.Foreign.PrimArray as F
import qualified Data.ByteString       as BS
import           Std.Data.CBytes        as CBytes

bytesToByteString :: Bytes -> IO ByteString
bytesToByteString bytes =
  F.withPrimVectorSafe bytes (\ptr len -> BS.packCStringLen (castPtr ptr, len))

bytesFromByteString :: ByteString -> IO Bytes
bytesFromByteString bs =
  CBytes.toBytes <$> BU.unsafeUseAsCString bs CBytes.fromCString

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions