small-bytearray-builder-0.1.0.0: Serialize to a small byte arrays

Safe HaskellNone
LanguageHaskell2010

Data.ByteArray.Builder.Small

Contents

Synopsis

Unsafe Primitives

newtype Builder Source #

An unmaterialized sequence of bytes that may be pasted into a mutable byte array.

Constructors

Builder (forall s. MutableByteArray# s -> Int# -> Int# -> State# s -> (#State# s, Int##)) 

construct :: (forall s. MutableBytes s -> ST s (Maybe Int)) -> Builder Source #

Constructor for Builder that works on a function with lifted arguments instead of unlifted ones. This is just as unsafe as the actual constructor.

Evaluation

run Source #

Arguments

:: Int

Hint for upper bound on size

-> Builder

Builder

-> ByteArray 

Run a builder. An accurate size hint is important for good performance. The size hint should be slightly larger than the actual size.

pasteST :: Builder -> MutableBytes s -> ST s (Maybe Int) Source #

Execute the builder, pasting its contents into a buffer. If the buffer is not large enough, this returns Nothing. Otherwise, it returns the index in the buffer that follows the payload just written.

pasteIO :: Builder -> MutableBytes RealWorld -> IO (Maybe Int) Source #

Variant of pasteST that runs in IO.

pasteGrowST Source #

Arguments

:: Int

How many bytes to grow by at a time

-> Builder 
-> MutableByteArray s

Initial buffer, used linearly. Do not reuse this argument.

-> ST s (MutableByteArrayOffset s)

Final buffer that accomodated the builder.

Paste the builder into the byte array starting at offset zero. This repeatedly reallocates the byte array if it cannot accomodate the builder, replaying the builder each time.

pasteGrowIO Source #

Arguments

:: Int

How many bytes to grow by at a time

-> Builder 
-> MutableByteArray RealWorld

Initial buffer, used linearly. Do not reuse this argument.

-> IO (MutableByteArrayOffset RealWorld)

Final buffer that accomodated the builder.

Variant of pasteGrowST that runs in IO.

pasteArrayST Source #

Arguments

:: MutableBytes s

Buffer

-> (a -> Builder)

Builder

-> Vector a

Elements to serialize

-> ST s (Vector a, MutableBytes s)

Shifted vector, shifted buffer

Fold over a vector, applying the builder to each element until the buffer cannot accomodate any more.

pasteArrayIO Source #

Arguments

:: MutableBytes RealWorld

Buffer

-> (a -> Builder)

Builder

-> Vector a

Elements to serialize

-> IO (Vector a, MutableBytes RealWorld)

Shifted vector, shifted buffer

Variant of pasteArrayST that runs in IO.

Materialized Byte Sequences

bytes :: Bytes -> Builder Source #

Create a builder from a sliced byte sequence.

bytearray :: ByteArray -> Builder Source #

Create a builder from an unsliced byte sequence.

Numbers

word64Dec :: Word64 -> Builder Source #

Encodes an unsigned 64-bit integer as decimal. This encoding never starts with a zero unless the argument was zero.

word64PaddedUpperHex :: Word64 -> Builder Source #

Encode a 64-bit unsigned integer as hexadecimal, zero-padding the encoding to 16 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 00000000000003FE.

word32PaddedUpperHex :: Word32 -> Builder Source #

Encode a 32-bit unsigned integer as hexadecimal, zero-padding the encoding to 8 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 000003FE.