HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulestore-0.7.20Haskell2010

Data.Store

This is the main public API of the store package. The functions exported here are more likely to be stable between versions.

Usually you won't need to write your own Store instances, and instead can rely on either using the Generic deriving approach or Data.Store.TH for defining Store instances for your datatypes. There are some tradeoffs here - the generics instances do not require -XTemplateHaskell, but they do not optimize as well for sum types that only require a constant number of bytes.

If you need streaming encode / decode of multiple store encoded messages, take a look at the store-streaming package.

Gotchas

Store is best used for communication between trusted processes and local caches. It can certainly be used for other purposes, but the builtin set of instances have some gotchas to be aware of:

  • Store's builtin instances serialize in a format which depends on machine endianness.

  • Store's builtin instances trust the data when deserializing. For example, the deserialization of Vector will read the vector's link from the first 8 bytes. It will then allocate enough memory to store all the elements. Malicious or malformed input could cause allocation of large amounts of memory. See https://github.com/fpco/store/issues/122

  • 4 types
  • 4 classes
  • 10 values
  • Packagestore-0.7.20
  • Exports18
  • LanguageHaskell2010
  • LicenceMIT
  • SourceStore.hs

Encoding and decoding strict ByteStrings.

9 declarations
valueencode :: Store a => a -> ByteString
#

Serializes a value to a ByteString. In order to do this, it first allocates a ByteString of the correct size (based on size), and then uses poke to fill it.

Safety of this function depends on correctness of the Store instance. If size returns a. The good news is that this isn't an issue if you use well-tested manual instances (such as those from this package) combined with auomatic definition of instances.

Store class and related types.

7 declarations
classclass Store a where
#

The Store typeclass provides efficient serialization and deserialization to raw pointer addresses.

The peek and poke methods should be defined such that decodeEx (encode x) == x .

Methods

  • size :: Size a

    Yields the Size of the buffer, in bytes, required to store the encoded representation of the type.

    Note that the correctness of this function is crucial for the safety of poke, as it does not do any bounds checking. It is the responsibility of the invoker of poke (encode and similar functions) to ensure that there's enough space in the output buffer. If poke writes beyond, then arbitrary memory can be overwritten, causing undefined behavior and segmentation faults.

  • poke :: a -> Poke ()

    Serializes a value to bytes. It is the responsibility of the caller to ensure that at least the number of bytes required by size are available. These details are handled by encode and similar utilities.

  • peek :: Peek a

    Serialized a value from bytes, throwing exceptions if it encounters invalid data or runs out of input bytes.

Instances310Deriver, Store, …
datadata Size a
#

Info about a type's serialized length. Either the length is known independently of the value, or the length depends on the value.

Constructors

Instances1Contravariant
newtypenewtype Poke a
#

Poke actions are useful for building sequential serializers.

They are actions which write values to bytes into memory specified by a Ptr base. The Applicative and Monad instances make it easy to write serializations, by keeping track of the Offset of the current byte. They allow you to chain Poke action such that subsequent Pokes write into subsequent portions of the output.

Instances5Monad, Functor, MonadFail, Applicative, MonadIO
  • Monad PokeDefined in store-core-0.4.4.7 · Data.Store.Core
  • Functor PokeDefined in store-core-0.4.4.7 · Data.Store.Core
  • MonadFail PokeDefined in store-core-0.4.4.7 · Data.Store.Core
  • Applicative PokeDefined in store-core-0.4.4.7 · Data.Store.Core
  • MonadIO PokeDefined in store-core-0.4.4.7 · Data.Store.Core
newtypenewtype Peek a
#

Peek actions are useful for building sequential deserializers.

They are actions which read from memory and construct values from it. The Applicative and Monad instances make it easy to chain these together to get more complicated deserializers. This machinery keeps track of the current Ptr and end-of-buffer Ptr.

Instances7Monad, Functor, MonadFail, Applicative, MonadIO, PrimMonad, …
classclass GStoreSize (f :: Type -> Type) where
#
Instances6GStoreSize
classclass GStorePoke (f :: Type -> Type) where
#
Instances6GStorePoke
classclass GStorePeek (f :: Type -> Type) where
#
Instances6GStorePeek

Exceptions thrown by Peek

datadata PeekException
#

Exception thrown while running peek. Note that other types of exceptions can also be thrown. Invocations of fail in the Poke monad causes this exception to be thrown.

PeekException is thrown when the data being decoded is invalid.

Instances3Eq, Show, Exception