bytes-0.17.4: Sharing code for serialization between binary and cereal

Copyright | (c) Edward Kmett 2013-2015
License | BSD3
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | type-families
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Bytes.Get

Description

This module generalizes the `binary` `Get` and `cereal` `Get` monads in an ad hoc fashion to permit code to be written that is compatible across them.

Moreover, this class permits code to be written to be portable over various monad transformers applied to these as base monads.

Synopsis

- class (forall a b. Coercible a b => Coercible (m a) (m b), Integral (Remaining m), MonadFail m, Applicative m) => MonadGet (m :: Type -> Type) where
  - type Remaining (m :: Type -> Type)
  - type Bytes (m :: Type -> Type)
  - skip :: Int -> m ()
  - ensure :: Int -> m ByteString
  - lookAhead :: m a -> m a
  - lookAheadM :: m (Maybe a) -> m (Maybe a)
  - lookAheadE :: m (Either a b) -> m (Either a b)
  - getBytes :: Int -> m ByteString
  - remaining :: m (Remaining m)
  - isEmpty :: m Bool
  - getWord8 :: m Word8
  - getByteString :: Int -> m ByteString
  - getLazyByteString :: Int64 -> m ByteString
  - getWord16be :: m Word16
  - getWord16le :: m Word16
  - getWord16host :: m Word16
  - getWord32be :: m Word32
  - getWord32le :: m Word32
  - getWord32host :: m Word32
  - getWord64be :: m Word64
  - getWord64le :: m Word64
  - getWord64host :: m Word64
  - getWordhost :: m Word
- runGetL :: Get a -> ByteString -> a
- runGetS :: Get a -> ByteString -> Either String a

# Documentation

class (forall a b. Coercible a b => Coercible (m a) (m b), Integral (Remaining m), MonadFail m, Applicative m) => MonadGet (m :: Type -> Type) where Source #

Minimal complete definition

lookAhead, lookAheadM, lookAheadE

Associated Types

type Remaining (m :: Type -> Type) Source #

An `Integral` number type used for unchecked skips and counting.

type Bytes (m :: Type -> Type) Source #

The underlying ByteString type used by this instance

Methods

skip :: Int -> m () Source #

Skip ahead `n` bytes. Fails if fewer than `n` bytes are available.

default skip :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => Int -> m () Source #

ensure :: Int -> m ByteString Source #

If at least `n` bytes are available return at least that much of the current input. Otherwise fail.

default ensure :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => Int -> m ByteString Source #

lookAhead :: m a -> m a Source #

Run `ga`, but return without consuming its input. Fails if `ga` fails.

lookAheadM :: m (Maybe a) -> m (Maybe a) Source #

Like `lookAhead`, but consume the input if `gma` returns 'Just _'. Fails if `gma` fails.

lookAheadE :: m (Either a b) -> m (Either a b) Source #

Like `lookAhead`, but consume the input if `gea` returns 'Right _'. Fails if `gea` fails.

getBytes :: Int -> m ByteString Source #

Pull `n` bytes from the input, as a strict ByteString.

default getBytes :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => Int -> m ByteString Source #

remaining :: m (Remaining m) Source #

Get the number of remaining unparsed bytes. Useful for checking whether all input has been consumed. Note that this forces the rest of the input.

default remaining :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n, Remaining m ~ Remaining n) => m (Remaining m) Source #

isEmpty :: m Bool Source #

Test whether all input has been consumed, i.e. there are no remaining unparsed bytes.

default isEmpty :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Bool Source #

getWord8 :: m Word8 Source #

Read a Word8 from the monad state

default getWord8 :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word8 Source #

getByteString :: Int -> m ByteString Source #

An efficient `get` method for strict ByteStrings. Fails if fewer than `n` bytes are left in the input.

default getByteString :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => Int -> m ByteString Source #

getLazyByteString :: Int64 -> m ByteString Source #

An efficient `get` method for lazy ByteStrings. Does not fail if fewer than `n` bytes are left in the input.

default getLazyByteString :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => Int64 -> m ByteString Source #

getWord16be :: m Word16 Source #

Read a `Word16` in big endian format

default getWord16be :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word16 Source #

getWord16le :: m Word16 Source #

Read a `Word16` in little endian format

default getWord16le :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word16 Source #

getWord16host :: m Word16 Source #

O(1). Read a 2 byte `Word16` in native host order and host endianness.

default getWord16host :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word16 Source #

getWord32be :: m Word32 Source #

Read a `Word32` in big endian format

default getWord32be :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word32 Source #

getWord32le :: m Word32 Source #

Read a `Word32` in little endian format

default getWord32le :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word32 Source #

getWord32host :: m Word32 Source #

O(1). Read a `Word32` in native host order and host endianness.

default getWord32host :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word32 Source #

getWord64be :: m Word64 Source #

Read a `Word64` in big endian format

default getWord64be :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word64 Source #

getWord64le :: m Word64 Source #

Read a `Word64` in little endian format

default getWord64le :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word64 Source #

getWord64host :: m Word64 Source #

O(1). Read a `Word64` in native host order and host endianness.

default getWord64host :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word64 Source #

getWordhost :: m Word Source #

O(1). Read a single native machine word. The word is read in host order, host endian form, for the machine you're on. On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes.

default getWordhost :: forall (t :: (Type -> Type) -> Type -> Type) (n :: Type -> Type). (MonadTrans t, MonadGet n, m ~ t n) => m Word Source #

#### Instances

Instances details

MonadGet Get Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining Get |
Instance details Defined in Data.Bytes.Get type Remaining Get = Int64
type Bytes Get |
Instance details Defined in Data.Bytes.Get type Bytes Get = ByteString

Methods

skip :: Int -> Get () Source #

ensure :: Int -> Get ByteString Source #

lookAhead :: Get a -> Get a Source #

lookAheadM :: Get (Maybe a) -> Get (Maybe a) Source #

lookAheadE :: Get (Either a b) -> Get (Either a b) Source #

getBytes :: Int -> Get ByteString Source #

remaining :: Get (Remaining Get) Source #

isEmpty :: Get Bool Source #

getWord8 :: Get Word8 Source #

getByteString :: Int -> Get ByteString Source #

getLazyByteString :: Int64 -> Get ByteString Source #

getWord16be :: Get Word16 Source #

getWord16le :: Get Word16 Source #

getWord16host :: Get Word16 Source #

getWord32be :: Get Word32 Source #

getWord32le :: Get Word32 Source #

getWord32host :: Get Word32 Source #

getWord64be :: Get Word64 Source #

getWord64le :: Get Word64 Source #

getWord64host :: Get Word64 Source #

getWordhost :: Get Word Source #

MonadGet Get Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining Get |
Instance details Defined in Data.Bytes.Get type Remaining Get = Int
type Bytes Get |
Instance details Defined in Data.Bytes.Get type Bytes Get = ByteString

Methods

skip :: Int -> Get () Source #

ensure :: Int -> Get ByteString Source #

lookAhead :: Get a -> Get a Source #

lookAheadM :: Get (Maybe a) -> Get (Maybe a) Source #

lookAheadE :: Get (Either a b) -> Get (Either a b) Source #

getBytes :: Int -> Get ByteString Source #

remaining :: Get (Remaining Get) Source #

isEmpty :: Get Bool Source #

getWord8 :: Get Word8 Source #

getByteString :: Int -> Get ByteString Source #

getLazyByteString :: Int64 -> Get ByteString Source #

getWord16be :: Get Word16 Source #

getWord16le :: Get Word16 Source #

getWord16host :: Get Word16 Source #

getWord32be :: Get Word32 Source #

getWord32le :: Get Word32 Source #

getWord32host :: Get Word32 Source #

getWord64be :: Get Word64 Source #

getWord64le :: Get Word64 Source #

getWord64host :: Get Word64 Source #

getWordhost :: Get Word Source #

MonadGet m => MonadGet (ExceptT e m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (ExceptT e m) |
Instance details Defined in Data.Bytes.Get type Remaining (ExceptT e m) = Remaining m
type Bytes (ExceptT e m) |
Instance details Defined in Data.Bytes.Get type Bytes (ExceptT e m) = Bytes m

Methods

skip :: Int -> ExceptT e m () Source #

ensure :: Int -> ExceptT e m ByteString Source #

lookAhead :: ExceptT e m a -> ExceptT e m a Source #

lookAheadM :: ExceptT e m (Maybe a) -> ExceptT e m (Maybe a) Source #

lookAheadE :: ExceptT e m (Either a b) -> ExceptT e m (Either a b) Source #

getBytes :: Int -> ExceptT e m ByteString Source #

remaining :: ExceptT e m (Remaining (ExceptT e m)) Source #

isEmpty :: ExceptT e m Bool Source #

getWord8 :: ExceptT e m Word8 Source #

getByteString :: Int -> ExceptT e m ByteString Source #

getLazyByteString :: Int64 -> ExceptT e m ByteString Source #

getWord16be :: ExceptT e m Word16 Source #

getWord16le :: ExceptT e m Word16 Source #

getWord16host :: ExceptT e m Word16 Source #

getWord32be :: ExceptT e m Word32 Source #

getWord32le :: ExceptT e m Word32 Source #

getWord32host :: ExceptT e m Word32 Source #

getWord64be :: ExceptT e m Word64 Source #

getWord64le :: ExceptT e m Word64 Source #

getWord64host :: ExceptT e m Word64 Source #

getWordhost :: ExceptT e m Word Source #

MonadGet m => MonadGet (ReaderT e m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (ReaderT e m) |
Instance details Defined in Data.Bytes.Get type Remaining (ReaderT e m) = Remaining m
type Bytes (ReaderT e m) |
Instance details Defined in Data.Bytes.Get type Bytes (ReaderT e m) = Bytes m

Methods

skip :: Int -> ReaderT e m () Source #

ensure :: Int -> ReaderT e m ByteString Source #

lookAhead :: ReaderT e m a -> ReaderT e m a Source #

lookAheadM :: ReaderT e m (Maybe a) -> ReaderT e m (Maybe a) Source #

lookAheadE :: ReaderT e m (Either a b) -> ReaderT e m (Either a b) Source #

getBytes :: Int -> ReaderT e m ByteString Source #

remaining :: ReaderT e m (Remaining (ReaderT e m)) Source #

isEmpty :: ReaderT e m Bool Source #

getWord8 :: ReaderT e m Word8 Source #

getByteString :: Int -> ReaderT e m ByteString Source #

getLazyByteString :: Int64 -> ReaderT e m ByteString Source #

getWord16be :: ReaderT e m Word16 Source #

getWord16le :: ReaderT e m Word16 Source #

getWord16host :: ReaderT e m Word16 Source #

getWord32be :: ReaderT e m Word32 Source #

getWord32le :: ReaderT e m Word32 Source #

getWord32host :: ReaderT e m Word32 Source #

getWord64be :: ReaderT e m Word64 Source #

getWord64le :: ReaderT e m Word64 Source #

getWord64host :: ReaderT e m Word64 Source #

getWordhost :: ReaderT e m Word Source #

MonadGet m => MonadGet (StateT s m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (StateT s m) |
Instance details Defined in Data.Bytes.Get type Remaining (StateT s m) = Remaining m
type Bytes (StateT s m) |
Instance details Defined in Data.Bytes.Get type Bytes (StateT s m) = Bytes m

Methods

skip :: Int -> StateT s m () Source #

ensure :: Int -> StateT s m ByteString Source #

lookAhead :: StateT s m a -> StateT s m a Source #

lookAheadM :: StateT s m (Maybe a) -> StateT s m (Maybe a) Source #

lookAheadE :: StateT s m (Either a b) -> StateT s m (Either a b) Source #

getBytes :: Int -> StateT s m ByteString Source #

remaining :: StateT s m (Remaining (StateT s m)) Source #

isEmpty :: StateT s m Bool Source #

getWord8 :: StateT s m Word8 Source #

getByteString :: Int -> StateT s m ByteString Source #

getLazyByteString :: Int64 -> StateT s m ByteString Source #

getWord16be :: StateT s m Word16 Source #

getWord16le :: StateT s m Word16 Source #

getWord16host :: StateT s m Word16 Source #

getWord32be :: StateT s m Word32 Source #

getWord32le :: StateT s m Word32 Source #

getWord32host :: StateT s m Word32 Source #

getWord64be :: StateT s m Word64 Source #

getWord64le :: StateT s m Word64 Source #

getWord64host :: StateT s m Word64 Source #

getWordhost :: StateT s m Word Source #

MonadGet m => MonadGet (StateT s m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (StateT s m) |
Instance details Defined in Data.Bytes.Get type Remaining (StateT s m) = Remaining m
type Bytes (StateT s m) |
Instance details Defined in Data.Bytes.Get type Bytes (StateT s m) = Bytes m

Methods

skip :: Int -> StateT s m () Source #

ensure :: Int -> StateT s m ByteString Source #

lookAhead :: StateT s m a -> StateT s m a Source #

lookAheadM :: StateT s m (Maybe a) -> StateT s m (Maybe a) Source #

lookAheadE :: StateT s m (Either a b) -> StateT s m (Either a b) Source #

getBytes :: Int -> StateT s m ByteString Source #

remaining :: StateT s m (Remaining (StateT s m)) Source #

isEmpty :: StateT s m Bool Source #

getWord8 :: StateT s m Word8 Source #

getByteString :: Int -> StateT s m ByteString Source #

getLazyByteString :: Int64 -> StateT s m ByteString Source #

getWord16be :: StateT s m Word16 Source #

getWord16le :: StateT s m Word16 Source #

getWord16host :: StateT s m Word16 Source #

getWord32be :: StateT s m Word32 Source #

getWord32le :: StateT s m Word32 Source #

getWord32host :: StateT s m Word32 Source #

getWord64be :: StateT s m Word64 Source #

getWord64le :: StateT s m Word64 Source #

getWord64host :: StateT s m Word64 Source #

getWordhost :: StateT s m Word Source #

(MonadGet m, Monoid w) => MonadGet (WriterT w m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (WriterT w m) |
Instance details Defined in Data.Bytes.Get type Remaining (WriterT w m) = Remaining m
type Bytes (WriterT w m) |
Instance details Defined in Data.Bytes.Get type Bytes (WriterT w m) = Bytes m

Methods

skip :: Int -> WriterT w m () Source #

ensure :: Int -> WriterT w m ByteString Source #

lookAhead :: WriterT w m a -> WriterT w m a Source #

lookAheadM :: WriterT w m (Maybe a) -> WriterT w m (Maybe a) Source #

lookAheadE :: WriterT w m (Either a b) -> WriterT w m (Either a b) Source #

getBytes :: Int -> WriterT w m ByteString Source #

remaining :: WriterT w m (Remaining (WriterT w m)) Source #

isEmpty :: WriterT w m Bool Source #

getWord8 :: WriterT w m Word8 Source #

getByteString :: Int -> WriterT w m ByteString Source #

getLazyByteString :: Int64 -> WriterT w m ByteString Source #

getWord16be :: WriterT w m Word16 Source #

getWord16le :: WriterT w m Word16 Source #

getWord16host :: WriterT w m Word16 Source #

getWord32be :: WriterT w m Word32 Source #

getWord32le :: WriterT w m Word32 Source #

getWord32host :: WriterT w m Word32 Source #

getWord64be :: WriterT w m Word64 Source #

getWord64le :: WriterT w m Word64 Source #

getWord64host :: WriterT w m Word64 Source #

getWordhost :: WriterT w m Word Source #

(MonadGet m, Monoid w) => MonadGet (WriterT w m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (WriterT w m) |
Instance details Defined in Data.Bytes.Get type Remaining (WriterT w m) = Remaining m
type Bytes (WriterT w m) |
Instance details Defined in Data.Bytes.Get type Bytes (WriterT w m) = Bytes m

Methods

skip :: Int -> WriterT w m () Source #

ensure :: Int -> WriterT w m ByteString Source #

lookAhead :: WriterT w m a -> WriterT w m a Source #

lookAheadM :: WriterT w m (Maybe a) -> WriterT w m (Maybe a) Source #

lookAheadE :: WriterT w m (Either a b) -> WriterT w m (Either a b) Source #

getBytes :: Int -> WriterT w m ByteString Source #

remaining :: WriterT w m (Remaining (WriterT w m)) Source #

isEmpty :: WriterT w m Bool Source #

getWord8 :: WriterT w m Word8 Source #

getByteString :: Int -> WriterT w m ByteString Source #

getLazyByteString :: Int64 -> WriterT w m ByteString Source #

getWord16be :: WriterT w m Word16 Source #

getWord16le :: WriterT w m Word16 Source #

getWord16host :: WriterT w m Word16 Source #

getWord32be :: WriterT w m Word32 Source #

getWord32le :: WriterT w m Word32 Source #

getWord32host :: WriterT w m Word32 Source #

getWord64be :: WriterT w m Word64 Source #

getWord64le :: WriterT w m Word64 Source #

getWord64host :: WriterT w m Word64 Source #

getWordhost :: WriterT w m Word Source #

(MonadGet m, Monoid w) => MonadGet (RWST r w s m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (RWST r w s m) |
Instance details Defined in Data.Bytes.Get type Remaining (RWST r w s m) = Remaining m
type Bytes (RWST r w s m) |
Instance details Defined in Data.Bytes.Get type Bytes (RWST r w s m) = Bytes m

Methods

skip :: Int -> RWST r w s m () Source #

ensure :: Int -> RWST r w s m ByteString Source #

lookAhead :: RWST r w s m a -> RWST r w s m a Source #

lookAheadM :: RWST r w s m (Maybe a) -> RWST r w s m (Maybe a) Source #

lookAheadE :: RWST r w s m (Either a b) -> RWST r w s m (Either a b) Source #

getBytes :: Int -> RWST r w s m ByteString Source #

remaining :: RWST r w s m (Remaining (RWST r w s m)) Source #

isEmpty :: RWST r w s m Bool Source #

getWord8 :: RWST r w s m Word8 Source #

getByteString :: Int -> RWST r w s m ByteString Source #

getLazyByteString :: Int64 -> RWST r w s m ByteString Source #

getWord16be :: RWST r w s m Word16 Source #

getWord16le :: RWST r w s m Word16 Source #

getWord16host :: RWST r w s m Word16 Source #

getWord32be :: RWST r w s m Word32 Source #

getWord32le :: RWST r w s m Word32 Source #

getWord32host :: RWST r w s m Word32 Source #

getWord64be :: RWST r w s m Word64 Source #

getWord64le :: RWST r w s m Word64 Source #

getWord64host :: RWST r w s m Word64 Source #

getWordhost :: RWST r w s m Word Source #

(MonadGet m, Monoid w) => MonadGet (RWST r w s m) Source # |

Instance details

Defined in Data.Bytes.Get

Associated Types

type Remaining (RWST r w s m) |
Instance details Defined in Data.Bytes.Get type Remaining (RWST r w s m) = Remaining m
type Bytes (RWST r w s m) |
Instance details Defined in Data.Bytes.Get type Bytes (RWST r w s m) = Bytes m

Methods

skip :: Int -> RWST r w s m () Source #

ensure :: Int -> RWST r w s m ByteString Source #

lookAhead :: RWST r w s m a -> RWST r w s m a Source #

lookAheadM :: RWST r w s m (Maybe a) -> RWST r w s m (Maybe a) Source #

lookAheadE :: RWST r w s m (Either a b) -> RWST r w s m (Either a b) Source #

getBytes :: Int -> RWST r w s m ByteString Source #

remaining :: RWST r w s m (Remaining (RWST r w s m)) Source #

isEmpty :: RWST r w s m Bool Source #

getWord8 :: RWST r w s m Word8 Source #

getByteString :: Int -> RWST r w s m ByteString Source #

getLazyByteString :: Int64 -> RWST r w s m ByteString Source #

getWord16be :: RWST r w s m Word16 Source #

getWord16le :: RWST r w s m Word16 Source #

getWord16host :: RWST r w s m Word16 Source #

getWord32be :: RWST r w s m Word32 Source #

getWord32le :: RWST r w s m Word32 Source #

getWord32host :: RWST r w s m Word32 Source #

getWord64be :: RWST r w s m Word64 Source #

getWord64le :: RWST r w s m Word64 Source #

getWord64host :: RWST r w s m Word64 Source #

getWordhost :: RWST r w s m Word Source #

runGetL :: Get a -> ByteString -> a Source #

Get something from a lazy `ByteString` using `runGet`.

runGetS :: Get a -> ByteString -> Either String a Source #

Get something from a strict `ByteString` using `runGet`.