wrench-0.0.6.3
Safe HaskellNone
LanguageHaskell2010

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

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.

Methods

unpack :: IsWord w => BitGet w a Source #

pack :: IsWord w => a -> BitPut w () Source #

Instances

Instances details
BitPack Scope Source # 
Instance details

Defined in Wrench.Isa.Wasm32.State

Methods

unpack :: IsWord w => BitGet w Scope Source #

pack :: IsWord w => Scope -> BitPut w () Source #

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

data BitGet w a Source #

Instances

Instances details
Applicative (BitGet w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

pure :: a -> BitGet w a #

(<*>) :: BitGet w (a -> b) -> BitGet w a -> BitGet w b #

liftA2 :: (a -> b -> c) -> BitGet w a -> BitGet w b -> BitGet w c #

(*>) :: BitGet w a -> BitGet w b -> BitGet w b #

(<*) :: BitGet w a -> BitGet w b -> BitGet w a #

Functor (BitGet w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

fmap :: (a -> b) -> BitGet w a -> BitGet w b #

(<$) :: a -> BitGet w b -> BitGet w a #

Monad (BitGet w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

(>>=) :: BitGet w a -> (a -> BitGet w b) -> BitGet w b #

(>>) :: BitGet w a -> BitGet w b -> BitGet w b #

return :: a -> BitGet w a #

IsWord w => Layout (BitGet w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

word :: BitGet w a -> BitGet w a Source #

label :: Text -> BitGet w a -> BitGet w a Source #

data BitPut w a Source #

Instances

Instances details
Applicative (BitPut w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

pure :: a -> BitPut w a #

(<*>) :: BitPut w (a -> b) -> BitPut w a -> BitPut w b #

liftA2 :: (a -> b -> c) -> BitPut w a -> BitPut w b -> BitPut w c #

(*>) :: BitPut w a -> BitPut w b -> BitPut w b #

(<*) :: BitPut w a -> BitPut w b -> BitPut w a #

Functor (BitPut w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

fmap :: (a -> b) -> BitPut w a -> BitPut w b #

(<$) :: a -> BitPut w b -> BitPut w a #

Monad (BitPut w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

(>>=) :: BitPut w a -> (a -> BitPut w b) -> BitPut w b #

(>>) :: BitPut w a -> BitPut w b -> BitPut w b #

return :: a -> BitPut w a #

IsWord w => Layout (BitPut w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

word :: BitPut w a -> BitPut w a Source #

label :: Text -> BitPut w a -> BitPut w a Source #

Fields

getField :: IsWord w => Int -> BitGet w Int Source #

The next n bits, unsigned.

putField :: IsWord w => Int -> Int -> BitPut w () Source #

Write value into the next n bits.

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.

getPad :: IsWord w => Int -> BitGet w () Source #

Skip n bits, any number of them.

putPad :: IsWord w => Int -> BitPut w () Source #

n zero bits, any number of them.

Structure

class Layout (f :: Type -> Type) where Source #

Combinators that work in both directions.

Methods

word :: f a -> f a Source #

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.

Instances

Instances details
IsWord w => Layout (BitGet w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

word :: BitGet w a -> BitGet w a Source #

label :: Text -> BitGet w a -> BitGet w a Source #

IsWord w => Layout (BitPut w) Source # 
Instance details

Defined in Wrench.Machine.BitPack

Methods

word :: BitPut w a -> BitPut w a Source #

label :: Text -> BitPut w a -> BitPut w a Source #

(<?>) :: Layout f => f a -> Text -> f a infixl 1 Source #

layout ? name is label with the name last, after the field it names.

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.

runBitGet :: IsWord w => BitGet w a -> [w] -> Either Text a Source #

Decode a list of words; fails if the layout runs past them.

runBitPut :: IsWord w => BitPut w () -> Either Text [w] Source #

Pack the fields into words; fails if a value doesn't fit its field or the fields don't fill whole words.