Safe Haskell | None |
---|---|
Language | Haskell2010 |
Tag a Binary instance with the stack version number to ensure we're reading a compatible format.
- taggedDecodeOrLoad :: (BinarySchema a, MonadIO m, MonadLogger m) => Path Abs File -> m a -> m a
- taggedEncodeFile :: (BinarySchema a, MonadIO m) => Path Abs File -> a -> m ()
- class Binary t where
- type BinarySchema a = (Binary a, NFData a, HasStructuralInfo a, HasSemanticVersion a)
- class HasStructuralInfo a
- class KnownNat (SemanticVersion a) => HasSemanticVersion a
- decodeFileOrFailDeep :: (BinarySchema a, MonadIO m, MonadThrow n) => Path loc File -> m (n a)
- class NFData a where
- rnf :: a -> ()
Documentation
taggedDecodeOrLoad :: (BinarySchema a, MonadIO m, MonadLogger m) => Path Abs File -> m a -> m a Source
Read from the given file. If the read fails, run the given action and write that back to the file. Always starts the file off with the version tag.
taggedEncodeFile :: (BinarySchema a, MonadIO m) => Path Abs File -> a -> m () Source
Write to the given file, with a binary-tagged tag.
class Binary t where
The Binary
class provides put
and get
, methods to encode and
decode a Haskell value to a lazy ByteString
. It mirrors the Read
and
Show
classes for textual representation of Haskell types, and is
suitable for serialising Haskell values to disk, over the network.
For decoding and generating simple external binary formats (e.g. C
structures), Binary may be used, but in general is not suitable
for complex protocols. Instead use the Put
and Get
primitives
directly.
Instances of Binary should satisfy the following property:
decode . encode == id
That is, the get
and put
methods should be the inverse of each
other. A range of instances are provided for basic Haskell types.
Nothing
type BinarySchema a = (Binary a, NFData a, HasStructuralInfo a, HasSemanticVersion a) Source
class HasStructuralInfo a
Type class providing StructuralInfo
for each data type.
For regular non-recursive ADTs HasStructuralInfo
can be derived generically.
data Record = Record { a :: Int, b :: Bool, c :: [Char] } deriving (Generic) instance hasStructuralInfo Record
For stable types, you can provide only type name
instance HasStructuralInfo Int where structuralInfo = ghcNominalType -- infer name from Generic information instance HasStructuralInfo Integer where structuralInfo _ = NominalType "Integer"
Recursive type story is a bit sad atm. If the type structure is stable, you can do:
instance HasStructuralInfo a => HasStructuralInfo [a] where structuralInfo = ghcStructuralInfo1
class KnownNat (SemanticVersion a) => HasSemanticVersion a
A helper type family for encodeTaggedFile
and decodeTaggedFile
.
The default definition is SemanticVersion
a = 0
decodeFileOrFailDeep :: (BinarySchema a, MonadIO m, MonadThrow n) => Path loc File -> m (n a) Source
Ensure that there are no lurking exceptions deep inside the parsed value... because that happens unfortunately. See https://github.com/commercialhaskell/stack/issues/554
class NFData a where
A class of types that can be fully evaluated.
Since: 1.1.0.0
Nothing
rnf :: a -> ()
rnf
should reduce its argument to normal form (that is, fully
evaluate all sub-components), and then return '()'.
Generic
NFData
deriving
Starting with GHC 7.2, you can automatically derive instances
for types possessing a Generic
instance.
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic) instance NFData a => NFData (Foo a) data Colour = Red | Green | Blue deriving Generic instance NFData Colour
Starting with GHC 7.10, the example above can be written more
concisely by enabling the new DeriveAnyClass
extension:
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, NFData) data Colour = Red | Green | Blue deriving (Generic, NFData)
Compatibility with previous deepseq
versions
Prior to version 1.4.0.0, the default implementation of the rnf
method was defined as
rnf
a =seq
a ()
However, starting with deepseq-1.4.0.0
, the default
implementation is based on DefaultSignatures
allowing for
more accurate auto-derived NFData
instances. If you need the
previously used exact default rnf
method implementation
semantics, use
instance NFData Colour where rnf x = seq x ()
or alternatively
{-# LANGUAGE BangPatterns #-} instance NFData Colour where rnf !_ = ()