Copyright | Aleksandr Krupenkin 2016-2021 |
---|---|
License | Apache-2.0 |
Maintainer | mail@akru.me |
Stability | experimental |
Portability | unportable |
Safe Haskell | None |
Language | Haskell2010 |
The SCALE (Simple Concatenated Aggregate Little-Endian) Codec is a lightweight, efficient, binary serialization and deserialization codec.
It is designed for high-performance, copy-free encoding and decoding of data in resource-constrained execution contexts, like the Substrate runtime. It is not self-describing in any way and assumes the decoding context has all type knowledge about the encoded data.
Synopsis
- encode :: (Encode a, ByteArray ba) => a -> ba
- decode :: (ByteArrayAccess ba, Decode a) => ba -> Either String a
- encode' :: (Generic a, Rep a ~ rep, GEncode rep, ByteArray ba) => a -> ba
- decode' :: (Generic a, Rep a ~ rep, GDecode rep, ByteArrayAccess ba) => ba -> Either String a
- class Encode a
- class Decode a
- class All (SListI :: [Type] -> Constraint) (Code a) => Generic a
- module Codec.Scale.Core
Documentation
decode :: (ByteArrayAccess ba, Decode a) => ba -> Either String a Source #
Decode datatype from SCALE format.
encode' :: (Generic a, Rep a ~ rep, GEncode rep, ByteArray ba) => a -> ba Source #
Generic driven version of encode
decode' :: (Generic a, Rep a ~ rep, GDecode rep, ByteArrayAccess ba) => ba -> Either String a Source #
Generic driven version of decode
A class for encoding datatypes to SCALE format.
If your compiler has support for the DeriveGeneric
and
DefaultSignatures
language extensions (ghc >= 7.2.1
),
the put
method will have default generic implementations.
To use this option, simply add a deriving
clause
to your datatype and declare a Generic
Encode
instance for it without
giving a definition for put
.
Instances
Encode Bool Source # | |
Encode Int8 Source # | |
Encode Int16 Source # | |
Encode Int32 Source # | |
Encode Int64 Source # | |
Encode Word8 Source # | |
Encode Word16 Source # | |
Encode Word32 Source # | |
Encode Word64 Source # | |
Encode () Source # | |
Defined in Codec.Scale.Core | |
Encode ByteString Source # | |
Defined in Codec.Scale.Core put :: Putter ByteString Source # | |
Encode Text Source # | |
Encode a => Encode [a] Source # | |
Defined in Codec.Scale.Core | |
Encode (Maybe Bool) Source # | |
Encode a => Encode (Maybe a) Source # | |
(Encode a, Unbox a) => Encode (Vector a) Source # | |
Encode (Vector Bit) Source # | |
Integral a => Encode (Compact a) Source # | |
Encode a => Encode (SingletonEnum a) Source # | |
Defined in Codec.Scale.SingletonEnum put :: Putter (SingletonEnum a) Source # | |
Encode (Skip a) Source # | |
(Encode a, Encode b) => Encode (Either a b) Source # | |
(Encode a1, Encode a2) => Encode (a1, a2) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3) => Encode (a1, a2, a3) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4) => Encode (a1, a2, a3, a4) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5) => Encode (a1, a2, a3, a4, a5) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6) => Encode (a1, a2, a3, a4, a5, a6) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7) => Encode (a1, a2, a3, a4, a5, a6, a7) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8) => Encode (a1, a2, a3, a4, a5, a6, a7, a8) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15, Encode a16) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15, Encode a16, Encode a17) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15, Encode a16, Encode a17, Encode a18) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15, Encode a16, Encode a17, Encode a18, Encode a19) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) Source # | |
Defined in Codec.Scale.Core | |
(Encode a1, Encode a2, Encode a3, Encode a4, Encode a5, Encode a6, Encode a7, Encode a8, Encode a9, Encode a10, Encode a11, Encode a12, Encode a13, Encode a14, Encode a15, Encode a16, Encode a17, Encode a18, Encode a19, Encode a20) => Encode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) Source # | |
Defined in Codec.Scale.Core |
A class for decoding datatypes from SCALE format.
If your compiler has support for the DeriveGeneric
and
DefaultSignatures
language extensions (ghc >= 7.2.1
),
the get
method will have default generic implementations.
To use this option, simply add a deriving
clause
to your datatype and declare a Generic
Decode
instance for it without
giving a definition for get
.
Instances
Decode Bool Source # | |
Decode Int8 Source # | |
Decode Int16 Source # | |
Decode Int32 Source # | |
Decode Int64 Source # | |
Decode Word8 Source # | |
Decode Word16 Source # | |
Decode Word32 Source # | |
Decode Word64 Source # | |
Decode () Source # | |
Defined in Codec.Scale.Core | |
Decode ByteString Source # | |
Defined in Codec.Scale.Core get :: Get ByteString Source # | |
Decode Text Source # | |
Decode a => Decode [a] Source # | |
Defined in Codec.Scale.Core | |
Decode (Maybe Bool) Source # | |
Decode a => Decode (Maybe a) Source # | |
(Decode a, Unbox a) => Decode (Vector a) Source # | |
Decode (Vector Bit) Source # | |
Integral a => Decode (Compact a) Source # | |
Decode a => Decode (SingletonEnum a) Source # | |
Defined in Codec.Scale.SingletonEnum get :: Get (SingletonEnum a) Source # | |
Default a => Decode (Skip a) Source # | |
(Decode a, Decode b) => Decode (Either a b) Source # | |
(Decode a1, Decode a2) => Decode (a1, a2) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3) => Decode (a1, a2, a3) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4) => Decode (a1, a2, a3, a4) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5) => Decode (a1, a2, a3, a4, a5) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6) => Decode (a1, a2, a3, a4, a5, a6) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7) => Decode (a1, a2, a3, a4, a5, a6, a7) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8) => Decode (a1, a2, a3, a4, a5, a6, a7, a8) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15, Decode a16) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15, Decode a16, Decode a17) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15, Decode a16, Decode a17, Decode a18) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15, Decode a16, Decode a17, Decode a18, Decode a19) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) Source # | |
Defined in Codec.Scale.Core | |
(Decode a1, Decode a2, Decode a3, Decode a4, Decode a5, Decode a6, Decode a7, Decode a8, Decode a9, Decode a10, Decode a11, Decode a12, Decode a13, Decode a14, Decode a15, Decode a16, Decode a17, Decode a18, Decode a19, Decode a20) => Decode (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) Source # | |
Defined in Codec.Scale.Core |
class All (SListI :: [Type] -> Constraint) (Code a) => Generic a #
The class of representable datatypes.
The SOP approach to generic programming is based on viewing
datatypes as a representation (Rep
) built from the sum of
products of its components. The components of a datatype
are specified using the Code
type family.
The isomorphism between the original Haskell datatype and its
representation is witnessed by the methods of this class,
from
and to
. So for instances of this class, the following
laws should (in general) hold:
to
.
from
===id
:: a -> afrom
.
to
===id
::Rep
a ->Rep
a
You typically don't define instances of this class by hand, but rather derive the class instance automatically.
Option 1: Derive via the built-in GHC-generics. For this, you
need to use the DeriveGeneric
extension to first derive an
instance of the Generic
class from module GHC.Generics.
With this, you can then give an empty instance for Generic
, and
the default definitions will just work. The pattern looks as
follows:
import qualified GHC.Generics as GHC import Generics.SOP ... data T = ... deriving (GHC.Generic
, ...) instanceGeneric
T -- empty instanceHasDatatypeInfo
T -- empty, if you want/need metadata
Option 2: Derive via Template Haskell. For this, you need to
enable the TemplateHaskell
extension. You can then use
deriveGeneric
from module Generics.SOP.TH
to have the instance generated for you. The pattern looks as
follows:
import Generics.SOP import Generics.SOP.TH ... data T = ...deriveGeneric
''T -- derivesHasDatatypeInfo
as well
Tradeoffs: Whether to use Option 1 or 2 is mainly a matter of personal taste. The version based on Template Haskell probably has less run-time overhead.
Non-standard instances:
It is possible to give Generic
instances manually that deviate
from the standard scheme, as long as at least
to
.
from
===id
:: a -> a
still holds.
module Codec.Scale.Core