binrep-0.3.1: Encode precise binary representations directly in types
Safe HaskellSafe-Inferred
LanguageHaskell2010

Binrep.Generic

Description

Derive BLen, Put, Get and CBLen instances generically.

Synopsis

Documentation

data Cfg a Source #

Constructors

Cfg 

Fields

cfg :: (Eq a, Show a) => (String -> a) -> Cfg a Source #

cSumTagHex :: forall a. Integral a => (String -> String) -> String -> a Source #

Obtain the tag for a sum type value by applying a function to the constructor name, and reading the result as a hexadecimal number.

cSumTagNullTerm :: String -> AsByteString 'C Source #

Obtain the tag for a sum type value using the constructor name directly (with a null terminator).

This is probably not what you want in a binary representation, but it's safe and may be useful for debugging.

The refine force is safe under the assumption that Haskell constructor names are UTF-8 with no null bytes allowed. I haven't confirmed that, but I'm fairly certain.

cDef :: Cfg (AsByteString 'C) Source #

Default generic derivation configuration, using cSumTagNullTerm.

cNoSum :: Cfg Void Source #

Special generic derivation configuration you may use for non-sum data types.

When generically deriving binrep instances for a non-sum type, you may like to ignore sum tag handling. You could use cDef, but this will silently change behaviour if your type becomes a sum type. This configuration will generate clear runtime errors when used with a sum type.

By selecting Void for the sum tag type, consumption actions (serializing, getting length in bytes) will runtime error, while generation actions (parsing) will hit the Void instance first and always safely error out.

blenGeneric :: (Generic a, GBLen (Rep a), BLen w) => Cfg w -> a -> BLenT Source #

putGeneric :: (Generic a, GPut (Rep a), Put w) => Cfg w -> a -> Builder Source #

getGeneric :: (Generic a, GGetD (Rep a), Get w) => Cfg w -> Getter a Source #

type CBLenGeneric w a = GCBLen w (Rep a) Source #