uni-util-2.3.0.2: Utilities for the uniform workbench

Util.Binary

Description

Library for converting types to and from binary, so that they can be written to and from files, stored compactly in memory, and so on.

This is a preliminary version of the library, hence I have decided not to optimise heavily, beyond putting in strictness annotations in where they seem appropriate.

A good place to start optimising would probably be the separate Bytes libary.

See also BinaryInstances, which declares instances for the standard types (and one or two others), BinaryUtils, which contains (mostly) material for declaring new instances, BinaryExtras, which contains other miscellaneous utilities, and finally BinaryAll which just imports and reexports everything.

Synopsis

# Documentation

hWrite :: HasBinary a IO => Handle -> a -> IO () Source #

Write an (a) to a Handle

hRead :: HasBinary a IO => Handle -> IO a Source #

Read an (a) from a Handle

writeToBytes :: HasBinary a StateBinArea => a -> IO (Bytes, Int) Source #

Write an (a) to memory. The Int is the length of the area.

writeToBytes0 :: HasBinary a StateBinArea => Int -> a -> IO (Bytes, Int) Source #

Write an (a) to memory. The integer argument is an initial guess at the number of bytes that will be needed. This should be greater than 0. If it is too small, there will be unnecessary reallocations; if too large, too much memory will be used.

readFromBytes :: HasBinary a StateBinArea => (Bytes, Int) -> IO a Source #

Read a value from binary data in memory. The Int is the length, and there will be an error if this is either too small or too large.

class HasBinary a m where Source #

Minimal complete definition

Methods

Arguments

 :: WriteBinary m -> a -> m () Given a consumer of binary data, and an (a), write out the (a)

Arguments

 :: ReadBinary m -> m a Given a source of binary data, provide an (a)
Instances

data WriteBinary m Source #

A consumer of binary data

Constructors

 WriteBinary FieldswriteByte :: Byte -> m ()write one bytewriteBytes :: Bytes -> Int -> m ()write multiple bytes

A source of binary data

Constructors

data BinArea Source #

Somewhere to where you write binary data in memory.

a state monad containing the BinArea.

Create an empty BinArea, given the initial size.

A BinArea as somewhere to put binary data.

Return all the data currently in the BinArea

mkBinArea :: (Bytes, Int) -> BinArea Source #

Turn binary data in memory into a BinArea (so that you can read from it).

A BinArea as a source of binary data.

liftWriteBinary :: (forall a. m a -> n a) -> WriteBinary m -> WriteBinary n Source #

Transform the monad used by a WriteBinary

liftReadBinary :: (forall a. m a -> n a) -> ReadBinary m -> ReadBinary n Source #

Transform the monad used by a ReadBinary