memory-0.18.0: memory and related abstraction stuff

License | BSD-Style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | experimental
Portability | unknown
Safe Haskell | None
Language | Haskell2010

Data.ByteArray.Pack

Contents

- Operations
  - put
  - skip

Description

Simple Byte Array packer

Simple example:

    > flip pack 20 $ putWord8 0x41 >> putByteString "BCD" >> putWord8 0x20 >> putStorable (42 :: Word32)
    Right (ABCD *\NUL\NUL\NUL")

Original code from https://hackage.haskell.org/package/bspack generalized and adapted to run on `memory`, and spellchecked / tweaked. (2015-05) Copyright (c) 2014 Nicolas DI PRIMA

Synopsis

- data Packer a
- data Result a
  - = PackerMore a MemView
  - | PackerFail String
- fill :: ByteArray byteArray => Int -> Packer a -> Either String byteArray
- pack :: ByteArray byteArray => Packer a -> Int -> Either String byteArray
- putWord8 :: Word8 -> Packer ()
- putWord16 :: Word16 -> Packer ()
- putWord32 :: Word32 -> Packer ()
- putStorable :: Storable storable => storable -> Packer ()
- putBytes :: ByteArrayAccess ba => ba -> Packer ()
- fillList :: Storable storable => [storable] -> Packer ()
- fillUpWith :: Storable storable => storable -> Packer ()
- skip :: Int -> Packer ()
- skipStorable :: Storable storable => storable -> Packer ()

# Documentation

data Packer a Source #

Simple ByteArray Packer

#### Instances

Instances details

Applicative Packer Source # |
Instance details Defined in Data.ByteArray.Pack.Internal Methods pure :: a -> Packer a Source # (<*>) :: Packer (a -> b) -> Packer a -> Packer b Source # liftA2 :: (a -> b -> c) -> Packer a -> Packer b -> Packer c Source # (*>) :: Packer a -> Packer b -> Packer b Source # (<*) :: Packer a -> Packer b -> Packer a Source #
Functor Packer Source # |
Instance details Defined in Data.ByteArray.Pack.Internal Methods fmap :: (a -> b) -> Packer a -> Packer b Source # (<$) :: a -> Packer b -> Packer a Source #
Monad Packer Source # |
Instance details Defined in Data.ByteArray.Pack.Internal Methods (>>=) :: Packer a -> (a -> Packer b) -> Packer b Source # (>>) :: Packer a -> Packer b -> Packer b Source # return :: a -> Packer a Source #

data Result a Source #

Packing result:

- PackerMore: the next state of Packing with an arbitrary value
- PackerFail: an error happened

Constructors

PackerMore a MemView |
PackerFail String |

#### Instances

Instances details

Show a => Show (Result a) Source # |
Instance details Defined in Data.ByteArray.Pack.Internal Methods showsPrec :: Int -> Result a -> ShowS Source # show :: Result a -> String Source # showList :: [Result a] -> ShowS Source #

fill :: ByteArray byteArray => Int -> Packer a -> Either String byteArray Source #

Fill a given sized buffer with the result of the Packer action

pack :: ByteArray byteArray => Packer a -> Int -> Either String byteArray Source #

Deprecated: use fill instead

Pack the given packer into the given bytestring

# Operations

## put

putWord8 :: Word8 -> Packer () Source #

put Word8 in the current position in the stream

putWord16 :: Word16 -> Packer () Source #

put Word16 in the current position in the stream /! use Host Endianness

putWord32 :: Word32 -> Packer () Source #

put Word32 in the current position in the stream /! use Host Endianness

putStorable :: Storable storable => storable -> Packer () Source #

Put a storable from the current position in the stream

putBytes :: ByteArrayAccess ba => ba -> Packer () Source #

Put a Byte Array from the current position in the stream

If the ByteArray is null, then do nothing

fillList :: Storable storable => [storable] -> Packer () Source #

Will put the given storable list from the current position in the stream to the end.

This function will fail with not enough storage if the given storable can't be written (not enough space)

Example:

    > pack (fillList $ [1..] :: Word8) 9
    "\1\2\3\4\5\6\7\8\9"
    > pack (fillList $ [1..] :: Word32) 4
    "\1\0\0\0"
    > pack (fillList $ [1..] :: Word32) 64
    .. <..succesful..>
    > pack (fillList $ [1..] :: Word32) 1
    .. <.. not enough space ..>
    > pack (fillList $ [1..] :: Word32) 131
    .. <.. not enough space ..>

fillUpWith :: Storable storable => storable -> Packer () Source #

Fill up from the current position in the stream to the end

It is equivalent to:

    fillUpWith s == fillList (repeat s)

## skip

skip :: Int -> Packer () Source #

Skip some bytes from the current position in the stream

skipStorable :: Storable storable => storable -> Packer () Source #

Skip the size of a storable from the current position in the stream