| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Wrench.Machine.BitPack
Description
Packing values into bits of a stream of machine words.
A layout is a sequence of fields, each a number of bits, read and written
most significant bit first; a field may cross a word boundary, and word
groups the fields that make up exactly one word. BitGet asks for the next
word only when a field reaches it, so the layout itself decides how many words
are read. A failure names where it happened through label / <?>.
Synopsis
- class BitPack a where
- packWords :: forall w a. (BitPack a, IsWord w) => a -> Either Text [w]
- unpackWords :: (BitPack a, IsWord w) => [w] -> Either Text a
- data BitGet w a
- data BitPut w a
- getField :: IsWord w => Int -> BitGet w Int
- putField :: IsWord w => Int -> Int -> BitPut w ()
- getEnum :: (Bounded e, Enum e, IsWord w) => Int -> BitGet w e
- putEnum :: (Enum e, IsWord w) => Int -> e -> BitPut w ()
- getPad :: IsWord w => Int -> BitGet w ()
- putPad :: IsWord w => Int -> BitPut w ()
- class Layout (f :: Type -> Type) where
- (<?>) :: Layout f => f a -> Text -> f a
- runBitGetWith :: (IsWord w, Monad m) => (Int -> m w) -> BitGet w a -> m (Either Text a)
- runBitGet :: IsWord w => BitGet w a -> [w] -> Either Text a
- runBitPut :: IsWord w => BitPut w () -> Either Text [w]
Packing
class BitPack a where Source #
A type with a bit layout of its own, so layouts compose by type:
tag <- unpack inside a larger layout.
packWords :: forall w a. (BitPack a, IsWord w) => a -> Either Text [w] Source #
Pack a value into whole words.
unpackWords :: (BitPack a, IsWord w) => [w] -> Either Text a Source #
Unpack a value from exactly these words; fails if words are left over.
Monads
Fields
getEnum :: (Bounded e, Enum e, IsWord w) => Int -> BitGet w e Source #
The next n bits as a constructor of e; fails on a value no
constructor has.
putEnum :: (Enum e, IsWord w) => Int -> e -> BitPut w () Source #
Write a constructor of e into the next n bits.
Structure
class Layout (f :: Type -> Type) where Source #
Combinators that work in both directions.
Methods
Run a layout that must cover exactly one word, starting on a word boundary.
label :: Text -> f a -> f a Source #
Prefix a failure with where it happened; nested labels read outermost first.
Running
runBitGetWith :: (IsWord w, Monad m) => (Int -> m w) -> BitGet w a -> m (Either Text a) Source #
Decode, fetching word k of the stream with fetch k when the layout
reaches it.