| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Store.Internal
Description
Internal API for the store package. The functions here which are not re-exported by Data.Store are less likely to have stable APIs.
This module also defines most of the included Store instances, for
types from the base package and other commonly used packages
(bytestring, containers, text, time, etc).
Synopsis
- encode :: Store a => a -> ByteString
- decode :: Store a => ByteString -> Either PeekException a
- decodeWith :: Peek a -> ByteString -> Either PeekException a
- decodeEx :: Store a => ByteString -> a
- decodeExWith :: Peek a -> ByteString -> a
- decodeExPortionWith :: Peek a -> ByteString -> (Offset, a)
- decodeIO :: Store a => ByteString -> IO a
- decodeIOWith :: Peek a -> ByteString -> IO a
- decodeIOPortionWith :: Peek a -> ByteString -> IO (Offset, a)
- class Store a where
- data Poke a
- data Peek a
- runPeek :: Peek a -> PeekState -> Ptr Word8 -> IO (PeekResult a)
- data PokeException = PokeException {
- pokeExByteIndex :: Offset
- pokeExMessage :: Text
- pokeException :: Text -> Poke a
- data PeekException = PeekException {
- peekExBytesFromEnd :: Offset
- peekExMessage :: Text
- peekException :: Text -> Peek a
- tooManyBytes :: Int -> Int -> String -> IO void
- data Size a
- getSize :: Store a => a -> Int
- getSizeWith :: Size a -> a -> Int
- combineSize :: (Store a, Store b) => (c -> a) -> (c -> b) -> Size c
- combineSizeWith :: forall a b c. (c -> a) -> (c -> b) -> Size a -> Size b -> Size c
- addSize :: Int -> Size a -> Size a
- sizeSequence :: (IsSequence t, Store (Element t)) => Size t
- pokeSequence :: (IsSequence t, Store (Element t)) => t -> Poke ()
- peekSequence :: (IsSequence t, Store (Element t), Index t ~ Int) => Peek t
- sizeSet :: (IsSet t, Store (Element t)) => Size t
- pokeSet :: (IsSet t, Store (Element t)) => t -> Poke ()
- peekSet :: (IsSet t, Store (Element t)) => Peek t
- sizeMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Size t
- pokeMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => t -> Poke ()
- peekMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Peek t
- sizeOrdMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Size t
- pokeOrdMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => t -> Poke ()
- peekOrdMapWith :: (Store (ContainerKey t), Store (MapValue t)) => ([(ContainerKey t, MapValue t)] -> t) -> Peek t
- sizeArray :: (Ix i, IArray a e, Store i, Store e) => Size (a i e)
- pokeArray :: (Ix i, IArray a e, Store i, Store e) => a i e -> Poke ()
- peekArray :: (Ix i, IArray a e, Store i, Store e) => Peek (a i e)
- class GStoreSize (f :: Type -> Type)
- genericSize :: (Generic a, GStoreSize (Rep a)) => Size a
- class GStorePoke (f :: Type -> Type)
- genericPoke :: (Generic a, GStorePoke (Rep a)) => a -> Poke ()
- class GStorePeek (f :: Type -> Type)
- genericPeek :: (Generic a, GStorePeek (Rep a)) => Peek a
- skip :: Int -> Peek ()
- isolate :: Int -> Peek a -> Peek a
- peekMagic :: (Eq a, Show a, Store a) => String -> a -> Peek ()
- class KnownNat n => IsStaticSize (n :: Nat) a where
- toStaticSize :: a -> Maybe (StaticSize n a)
- newtype StaticSize (n :: Nat) a = StaticSize {
- unStaticSize :: a
- toStaticSizeEx :: forall (n :: Nat) a. IsStaticSize n a => a -> StaticSize n a
- liftStaticSize :: forall (n :: Nat) a. (KnownNat n, Lift a) => TypeQ -> StaticSize n a -> ExpQ
- staticByteStringExp :: Quote m => ByteString -> m Exp
Encoding and decoding strict ByteStrings.
encode :: Store a => a -> ByteString Source #
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.
decode :: Store a => ByteString -> Either PeekException a Source #
Decodes a value from a ByteString. Returns an exception if
there's an error while decoding, or if decoding undershoots /
overshoots the end of the buffer.
decodeWith :: Peek a -> ByteString -> Either PeekException a #
decodeEx :: Store a => ByteString -> a Source #
Decodes a value from a ByteString, potentially throwing
exceptions. It is an exception to not consume all input.
decodeExWith :: Peek a -> ByteString -> a #
decodeExPortionWith :: Peek a -> ByteString -> (Offset, a) #
decodeIO :: Store a => ByteString -> IO a Source #
Decodes a value from a ByteString, potentially throwing
exceptions. It is an exception to not consume all input.
decodeIOWith :: Peek a -> ByteString -> IO a #
decodeIOPortionWith :: Peek a -> ByteString -> IO (Offset, a) #
Store class and related types.
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 .
Minimal complete definition
Nothing
Methods
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.
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.
Serialized a value from bytes, throwing exceptions if it encounters invalid data or runs out of input bytes.
Instances
Exceptions thrown by Poke
data PokeException #
Constructors
| PokeException | |
Fields
| |
Instances
| Exception PokeException | |
Defined in Data.Store.Core Methods toException :: PokeException -> SomeException Source # fromException :: SomeException -> Maybe PokeException Source # displayException :: PokeException -> String Source # backtraceDesired :: PokeException -> Bool Source # | |
| Show PokeException | |
Defined in Data.Store.Core | |
| Eq PokeException | |
Defined in Data.Store.Core Methods (==) :: PokeException -> PokeException -> Bool Source # (/=) :: PokeException -> PokeException -> Bool Source # | |
pokeException :: Text -> Poke a #
Exceptions thrown by Peek
data PeekException #
Constructors
| PeekException | |
Fields
| |
Instances
| Exception PeekException | |
Defined in Data.Store.Core Methods toException :: PeekException -> SomeException Source # fromException :: SomeException -> Maybe PeekException Source # displayException :: PeekException -> String Source # backtraceDesired :: PeekException -> Bool Source # | |
| Show PeekException | |
Defined in Data.Store.Core | |
| Eq PeekException | |
Defined in Data.Store.Core Methods (==) :: PeekException -> PeekException -> Bool Source # (/=) :: PeekException -> PeekException -> Bool Source # | |
peekException :: Text -> Peek a #
Size type
Info about a type's serialized length. Either the length is known independently of the value, or the length depends on the value.
getSize :: Store a => a -> Int Source #
Get the number of bytes needed to store the given value. See
size.
getSizeWith :: Size a -> a -> Int Source #
Store instances in terms of IsSequence
sizeSequence :: (IsSequence t, Store (Element t)) => Size t Source #
Implement size for an IsSequence of Store instances.
Note that many monomorphic containers have more efficient implementations (for example, via memcpy).
pokeSequence :: (IsSequence t, Store (Element t)) => t -> Poke () Source #
Implement poke for an IsSequence of Store instances.
Note that many monomorphic containers have more efficient implementations (for example, via memcpy).
peekSequence :: (IsSequence t, Store (Element t), Index t ~ Int) => Peek t Source #
Implement peek for an IsSequence of Store instances.
Note that many monomorphic containers have more efficient implementations (for example, via memcpy).
Store instances in terms of IsSet
pokeSet :: (IsSet t, Store (Element t)) => t -> Poke () Source #
Implement poke for an IsSequence of Store instances.
peekSet :: (IsSet t, Store (Element t)) => Peek t Source #
Implement peek for an IsSequence of Store instances.
Store instances in terms of IsMap
sizeMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Size t Source #
Implement size for an IsMap of where both ContainerKey and
MapValue are Store instances.
pokeMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => t -> Poke () Source #
Implement poke for an IsMap of where both ContainerKey and
MapValue are Store instances.
peekMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Peek t Source #
Implement peek for an IsMap of where both ContainerKey and
MapValue are Store instances.
Utilities for ordered maps
sizeOrdMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => Size t Source #
pokeOrdMap :: (Store (ContainerKey t), Store (MapValue t), IsMap t) => t -> Poke () Source #
Arguments
| :: (Store (ContainerKey t), Store (MapValue t)) | |
| => ([(ContainerKey t, MapValue t)] -> t) | A function to construct the map from an ascending list such as
|
| -> Peek t |
Decode the results of pokeOrdMap using a given function to construct
the map.
Store instances in terms of IArray
Store instances in terms of Generic
class GStoreSize (f :: Type -> Type) Source #
Minimal complete definition
gsize
Instances
| GStoreSize (U1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStoreSize (V1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| (GStoreSize a, GStoreSize b) => GStoreSize (a :*: b) Source # | |
Defined in Data.Store.Impl | |
| (FitsInByte (SumArity (a :+: b)), GStoreSizeSum 0 (a :+: b)) => GStoreSize (a :+: b) Source # | |
Defined in Data.Store.Impl | |
| Store a => GStoreSize (K1 i a :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStoreSize f => GStoreSize (M1 i c f) Source # | |
Defined in Data.Store.Impl | |
genericSize :: (Generic a, GStoreSize (Rep a)) => Size a Source #
class GStorePoke (f :: Type -> Type) Source #
Minimal complete definition
gpoke
Instances
| GStorePoke (U1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStorePoke (V1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| (GStorePoke a, GStorePoke b) => GStorePoke (a :*: b) Source # | |
Defined in Data.Store.Impl | |
| (FitsInByte (SumArity (a :+: b)), GStorePokeSum 0 (a :+: b)) => GStorePoke (a :+: b) Source # | |
Defined in Data.Store.Impl | |
| Store a => GStorePoke (K1 i a :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStorePoke f => GStorePoke (M1 i c f) Source # | |
Defined in Data.Store.Impl | |
genericPoke :: (Generic a, GStorePoke (Rep a)) => a -> Poke () Source #
class GStorePeek (f :: Type -> Type) Source #
Minimal complete definition
gpeek
Instances
| GStorePeek (U1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStorePeek (V1 :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| (GStorePeek a, GStorePeek b) => GStorePeek (a :*: b) Source # | |
Defined in Data.Store.Impl | |
| (FitsInByte (SumArity (a :+: b)), GStorePeekSum 0 (a :+: b)) => GStorePeek (a :+: b) Source # | |
Defined in Data.Store.Impl | |
| Store a => GStorePeek (K1 i a :: Type -> Type) Source # | |
Defined in Data.Store.Impl | |
| GStorePeek f => GStorePeek (M1 i c f) Source # | |
Defined in Data.Store.Impl | |
genericPeek :: (Generic a, GStorePeek (Rep a)) => Peek a Source #
Peek utilities
isolate :: Int -> Peek a -> Peek a Source #
Isolate the input to n bytes, skipping n bytes forward. Fails if m
advances the offset beyond the isolated region.
peekMagic :: (Eq a, Show a, Store a) => String -> a -> Peek () Source #
Ensure the presence of a given magic value.
Throws a PeekException if the value isn't present.
Static Size type
class KnownNat n => IsStaticSize (n :: Nat) a where Source #
Methods
toStaticSize :: a -> Maybe (StaticSize n a) Source #
Instances
| KnownNat n => IsStaticSize n ByteString Source # | |
Defined in Data.Store.Internal Methods toStaticSize :: ByteString -> Maybe (StaticSize n ByteString) Source # | |
newtype StaticSize (n :: Nat) a Source #
Constructors
| StaticSize | |
Fields
| |
Instances
| NFData a => NFData (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal Methods rnf :: StaticSize n a -> () Source # | |||||
| (KnownNat n, Data a) => Data (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> StaticSize n a -> c (StaticSize n a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (StaticSize n a) Source # toConstr :: StaticSize n a -> Constr Source # dataTypeOf :: StaticSize n a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (StaticSize n a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (StaticSize n a)) Source # gmapT :: (forall b. Data b => b -> b) -> StaticSize n a -> StaticSize n a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> StaticSize n a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> StaticSize n a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> StaticSize n a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> StaticSize n a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> StaticSize n a -> m (StaticSize n a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> StaticSize n a -> m (StaticSize n a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> StaticSize n a -> m (StaticSize n a) Source # | |||||
| Generic (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal Associated Types
Methods from :: StaticSize n a -> Rep (StaticSize n a) x Source # to :: Rep (StaticSize n a) x -> StaticSize n a Source # | |||||
| Show a => Show (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal | |||||
| Eq a => Eq (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal Methods (==) :: StaticSize n a -> StaticSize n a -> Bool Source # (/=) :: StaticSize n a -> StaticSize n a -> Bool Source # | |||||
| Ord a => Ord (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal Methods compare :: StaticSize n a -> StaticSize n a -> Ordering Source # (<) :: StaticSize n a -> StaticSize n a -> Bool Source # (<=) :: StaticSize n a -> StaticSize n a -> Bool Source # (>) :: StaticSize n a -> StaticSize n a -> Bool Source # (>=) :: StaticSize n a -> StaticSize n a -> Bool Source # max :: StaticSize n a -> StaticSize n a -> StaticSize n a Source # min :: StaticSize n a -> StaticSize n a -> StaticSize n a Source # | |||||
| KnownNat n => Store (StaticSize n ByteString) Source # | |||||
Defined in Data.Store.Internal Methods size :: Size (StaticSize n ByteString) Source # poke :: StaticSize n ByteString -> Poke () Source # peek :: Peek (StaticSize n ByteString) Source # | |||||
| type Rep (StaticSize n a) Source # | |||||
Defined in Data.Store.Internal type Rep (StaticSize n a) = D1 ('MetaData "StaticSize" "Data.Store.Internal" "store-0.7.20-9ePWelnBqsfACPE7fnE1Lj" 'True) (C1 ('MetaCons "StaticSize" 'PrefixI 'True) (S1 ('MetaSel ('Just "unStaticSize") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))) | |||||
toStaticSizeEx :: forall (n :: Nat) a. IsStaticSize n a => a -> StaticSize n a Source #
liftStaticSize :: forall (n :: Nat) a. (KnownNat n, Lift a) => TypeQ -> StaticSize n a -> ExpQ Source #
staticByteStringExp :: Quote m => ByteString -> m Exp Source #