flat-0.4.4: Principled and efficient bit-oriented binary serialization.

Safe HaskellTrustworthy
LanguageHaskell2010

Flat.Class

Contents

Description

Generics-based generation of Flat instances

Synopsis

The Flat class

class Flat a where Source #

Class of types that can be encoded/decoded

Minimal complete definition

Nothing

Methods

encode :: a -> Encoding Source #

Return the encoding corrresponding to the value

encode :: (Generic a, GEncode (Rep a)) => a -> Encoding Source #

Return the encoding corrresponding to the value

decode :: Get a Source #

Decode a value

decode :: (Generic a, GDecode (Rep a)) => Get a Source #

Decode a value

size :: a -> NumBits -> NumBits Source #

Add maximum size in bits of the value to the total count

Used to calculated maximum buffer size before encoding

size :: (Generic a, GSize (Rep a)) => a -> NumBits -> NumBits Source #

Add maximum size in bits of the value to the total count

Used to calculated maximum buffer size before encoding

Instances
Flat Bool Source #

One bit is plenty for a Bool.

>>> test False
(True,1,"0")
>>> test True
(True,1,"1")
Instance details

Defined in Flat.Instances.Base

Flat Char Source #

Char's are mapped to Word32 and then encoded.

For ascii characters, the encoding is standard ascii.

>>> test 'a'
(True,8,"01100001")

For unicode characters, the encoding is non standard.

>>> test 'È'
(True,16,"11001000 00000001")
>>> test '不'
(True,24,"10001101 10011100 00000001")
>>> test "\x1F600"
(True,26,"11000000 01110110 00000011 10")
Instance details

Defined in Flat.Instances.Base

Flat Double Source #

Doubles are encoded as standard IEEE binary64 values:

IEEE_754_binary64 ≡ IEEE_754_binary64 {sign :: Sign,
                                        exponent :: MostSignificantFirst Bits11,
                                        fraction :: MostSignificantFirst Bits52}
Instance details

Defined in Flat.Instances.Base

Flat Float Source #

Floats are encoded as standard IEEE binary32 values:

IEEE_754_binary32 ≡ IEEE_754_binary32 {sign :: Sign,
                                        exponent :: MostSignificantFirst Bits8,
                                        fraction :: MostSignificantFirst Bits23}
>>> test (0::Float)
(True,32,"00000000 00000000 00000000 00000000")
>>> test (1.4012984643E-45::Float)
(True,32,"00000000 00000000 00000000 00000001")
>>> test (1.1754942107E-38::Float)
(True,32,"00000000 01111111 11111111 11111111")
Instance details

Defined in Flat.Instances.Base

Flat Int Source #

Integer, Int, Int16, Int32 and Int64 are defined as the ZigZag encoded version of the equivalent unsigned Word:

Int   ≡  Int   (ZigZag Word)

Int64 ≡  Int64 (ZigZag Word64)

Int32 ≡  Int32 (ZigZag Word32)

Int16 ≡  Int16 (ZigZag Word16)

Int8  ≡  Int8  (ZigZag Word8)

ZigZag a ≡ ZigZag a

ZigZag encoding alternates between positive and negative numbers, so that numbers whose absolute value is small can be encoded efficiently:

>>> test (0::Int)
(True,8,"00000000")
>>> test (-1::Int)
(True,8,"00000001")
>>> test (1::Int)
(True,8,"00000010")
>>> test (-2::Int)
(True,8,"00000011")
>>> test (2::Int)
(True,8,"00000100")
Instance details

Defined in Flat.Instances.Base

Flat Int8 Source #
>>> test (0::Int8)
(True,8,"00000000")
>>> test (127::Int8)
(True,8,"11111110")
>>> test (-128::Int8)
(True,8,"11111111")
Instance details

Defined in Flat.Instances.Base

Flat Int16 Source #
>>> test (0::Int16)
(True,8,"00000000")
>>> test (1::Int16)
(True,8,"00000010")
>>> test (-1::Int16)
(True,8,"00000001")
>>> test (minBound::Int16)
(True,24,"11111111 11111111 00000011")

equivalent to 0b1111111111111111

>>> test (maxBound::Int16)
(True,24,"11111110 11111111 00000011")

equivalent to 0b1111111111111110

Instance details

Defined in Flat.Instances.Base

Flat Int32 Source #
>>> test (0::Int32)
(True,8,"00000000")
>>> test (minBound::Int32)
(True,40,"11111111 11111111 11111111 11111111 00001111")
>>> test (maxBound::Int32)
(True,40,"11111110 11111111 11111111 11111111 00001111")
Instance details

Defined in Flat.Instances.Base

Flat Int64 Source #
>>> test (0::Int64)
(True,8,"00000000")
>>> test (minBound::Int64)
(True,80,"11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001")
>>> test (maxBound::Int64)
(True,80,"11111110 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001")
Instance details

Defined in Flat.Instances.Base

Flat Integer Source #

Integers are encoded just as the fixed size Ints.

>>> test (0::Integer)
(True,8,"00000000")
>>> test (-1::Integer)
(True,8,"00000001")
>>> test (1::Integer)
(True,8,"00000010")
>>> test (-(2^4)::Integer)
(True,8,"00011111")
>>> test (2^4::Integer)
(True,8,"00100000")
>>> test (-(2^120)::Integer)
(True,144,"11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000011")
>>> test (2^120::Integer)
(True,144,"10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000100")
Instance details

Defined in Flat.Instances.Base

Flat Natural Source #

Naturals are encoded just as the fixed size Words.

>>> test (0::Natural)
(True,8,"00000000")
>>> test (2^120::Natural)
(True,144,"10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000010")
Instance details

Defined in Flat.Instances.Base

Flat Word Source #

Natural, Word, Word16, Word32 and Word64 are encoded as a non empty list of 7 bits chunks (least significant chunk first and most significant bit first in every chunk).

Words are always encoded in a whole number of bytes, as every chunk is 8 bits long (1 bit for the List constructor, plus 7 bits for the value).

The actual definition is:

Word64 ≡   Word64 Word

Word32 ≡   Word32 Word

Word16 ≡   Word16 Word

Word ≡   Word (LeastSignificantFirst (NonEmptyList (MostSignificantFirst Word7)))

LeastSignificantFirst a ≡   LeastSignificantFirst a

NonEmptyList a ≡   Elem a
                 | Cons a (NonEmptyList a)

MostSignificantFirst a ≡   MostSignificantFirst a

Word7 ≡   V0
        | V1
        | V2
        ...
        | V127

Values between as 0 and 127 fit in a single byte.

127 (0b1111111) is represented as Elem V127 and encoded as: Elem=0 127=1111111

>>> test (127::Word)
(True,8,"01111111")

254 (0b11111110) is represented as Cons V126 (Elem V1) (254=128+126) and encoded as: Cons=1 V126=1111110 (Elem=0 V1=0000001):

>>> test (254::Word)
(True,16,"11111110 00000001")

Another example, 32768 (Ob1000000000000000 = 0000010 0000000 0000000):

>>> test (32768::Word32)
(True,24,"10000000 10000000 00000010")

As this is a variable length encoding, values are encoded in the same way, whatever their type:

>>> all (test (3::Word) ==) [test (3::Word16),test (3::Word32),test (3::Word64)]
True
Instance details

Defined in Flat.Instances.Base

Flat Word8 Source #

Word8 always take 8 bits.

>>> test (0::Word8)
(True,8,"00000000")
>>> test (255::Word8)
(True,8,"11111111")
Instance details

Defined in Flat.Instances.Base

Flat Word16 Source # 
Instance details

Defined in Flat.Instances.Base

Flat Word32 Source # 
Instance details

Defined in Flat.Instances.Base

Flat Word64 Source # 
Instance details

Defined in Flat.Instances.Base

Flat () Source #

`()`, as all data types with a single constructor, has a zero-length encoding.

>>> test ()
(True,0,"")
Instance details

Defined in Flat.Instances.Base

Methods

encode :: () -> Encoding Source #

decode :: Get () Source #

size :: () -> NumBits -> NumBits Source #

Flat All Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat Any Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat ShortByteString Source #
>>> tst ((False,True,False,SBS.pack [11,22,33]))
(True,51,[65,3,11,22,33,0])
Instance details

Defined in Flat.Instances.ByteString

Flat ByteString Source #
>>> tst ((False,True,False,L.pack [11,22,33]))
(True,51,[65,3,11,22,33,0])
Instance details

Defined in Flat.Instances.ByteString

Flat ByteString Source #

ByteString, ByteString.Lazy and ByteString.Short are all encoded as Prealigned Arrays:

PreAligned a ≡ PreAligned {preFiller :: Filler, preValue :: a}

Filler ≡   FillerBit Filler
          | FillerEnd

Array v = A0
        | A1 v (Array v)
        | A2 v v (Array v)
        ...
        | A255 ... (Array v)

That's to say as a byte-aligned sequence of blocks of up to 255 elements, with every block preceded by the count of the elements in the block and a final 0-length block.

>>> tst (B.pack [11,22,33])
(True,48,[1,3,11,22,33,0])

where:

1= PreAlignment (takes a byte if we are already on a byte boundary)

3= Number of bytes in ByteString

11,22,33= Bytes

0= End of Array

>>> tst (B.pack [])
(True,16,[1,0])

Pre-alignment ensures that a ByteString always starts at a byte boundary:

>>> tst ((False,True,False,B.pack [11,22,33]))
(True,51,[65,3,11,22,33,0])

All ByteStrings are encoded in the same way:

>>> all (tst (B.pack [55]) ==) [tst (L.pack [55]),tst (SBS.pack [55])]
True
Instance details

Defined in Flat.Instances.ByteString

Flat Text Source # 
Instance details

Defined in Flat.Instances.Text

Flat Text Source #

Text (and Data.Text.Lazy) is encoded as a byte aligned array of bytes corresponding to its UTF8 encoding.

>>> tst $ T.pack ""
(True,16,[1,0])
>>> tst $ T.pack "aaa"
(True,120,[1,3,97,97,97,0])
>>> tst $ T.pack "¢¢¢"
(True,120,[1,6,194,162,194,162,194,162,0])
>>> tst $ T.pack "日日日"
(True,120,[1,9,230,151,165,230,151,165,230,151,165,0])
>>> tst $ T.pack "𐍈𐍈𐍈"
(True,120,[1,12,240,144,141,136,240,144,141,136,240,144,141,136,0])

Strict and Lazy Text has the same encoding:

>>> tst (T.pack "abc") == tst (TL.pack "abc")
True
Instance details

Defined in Flat.Instances.Text

Flat UTF16Text Source # 
Instance details

Defined in Flat.Instances.Text

Flat UTF8Text Source # 
Instance details

Defined in Flat.Instances.Text

Flat Filler Source #

Use a special encoding for the filler

Instance details

Defined in Flat.Filler

Flat [Char] Source #

For better encoding/decoding performance, it is useful to declare instances of concrete list types, such as [Char].

>>> test ""
(True,1,"0")
>>> test "aaa"
(True,28,"10110000 11011000 01101100 0010")
Instance details

Defined in Flat.Instances.Base

Flat a => Flat [a] Source #
>>> test ([]::[Bool])
(True,1,"0")
>>> test [False,False]
(True,5,"10100")
Instance details

Defined in Flat.Instances.Base

Methods

encode :: [a] -> Encoding Source #

decode :: Get [a] Source #

size :: [a] -> NumBits -> NumBits Source #

Flat a => Flat (Maybe a) Source #
>>> test (Nothing::Maybe Bool)
(True,1,"0")
>>> test (Just False::Maybe Bool)
(True,2,"10")
Instance details

Defined in Flat.Instances.Base

(Integral a, Flat a) => Flat (Ratio a) Source #

Ratios are encoded as tuples of (numerator,denominator)

>>> test (3%4::Ratio Word8)
(True,16,"00000011 00000100")
Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Complex a) Source #
>>> test (4 :+ 2 :: Complex Word8)
(True,16,"00000100 00000010")
Instance details

Defined in Flat.Instances.Base

Flat (Fixed a) Source #
>>> test (MkFixed 123 :: Fixed E0)
(True,16,"11110110 00000001")
>>> test (MkFixed 123 :: Fixed E0) == test (MkFixed 123 :: Fixed E2)
True
Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Min a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Max a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (First a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Last a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Option a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Identity a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Dual a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Sum a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (Product a) Source #

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Flat a => Flat (NonEmpty a) Source #
>>> test (B.fromList [True])
(True,2,"10")
>>> test (B.fromList [False,False])
(True,4,"0100")
Instance details

Defined in Flat.Instances.Base

Flat a => Flat (IntMap a) Source #

Maps are defined as a list of (Key,Value) tuples:

Map = List (Key,Value)

List a = Nil | Cons a (List a)
>>> tst (Data.IntMap.empty :: IntMap ())
(True,1,[0])
>>> asList Data.IntMap.fromList [(1,"a"),(2,"b")]
True
Instance details

Defined in Flat.Instances.Containers

Flat a => Flat (Tree a) Source #
>>> tst (Node (1::Word8) [Node 2 [Node 3 []], Node 4 []])
(True,39,[1,129,64,200,32])
Instance details

Defined in Flat.Instances.Containers

Flat a => Flat (Seq a) Source #

Data.Sequence.Seq is encoded as a list.

>>> asList Data.Sequence.fromList [3::Word8,4,7]
True

In flat <0.4, it was encoded as an Array.

If you want to restore the previous behaviour, use AsArray:

>>> tst $ AsArray (Data.Sequence.fromList [11::Word8,22,33])
(True,40,[3,11,22,33,0])
>>> tst $ Data.Sequence.fromList [11::Word8,22,33]
(True,28,[133,197,164,32])
Instance details

Defined in Flat.Instances.Containers

(Flat a, Ord a) => Flat (Set a) Source #

Data.Set is encoded as a list

>>> asList Data.Set.fromList [3::Word8,4,7]
True
Instance details

Defined in Flat.Instances.Containers

Flat a => Flat (DList a) Source #
>>> test (Data.DList.fromList [7::Word,7])
(True,19,"10000011 11000001 110")
>>> let l = [7::Word,7] in flat (Data.DList.fromList l) == flat l
True
Instance details

Defined in Flat.Instances.DList

(Hashable a, Eq a, Flat a) => Flat (HashSet a) Source #
>>> test (Data.HashSet.fromList [1..3::Word])
(True,28,"10000000 11000000 10100000 0110")
Instance details

Defined in Flat.Instances.Unordered

(Unbox a, Flat a) => Flat (Vector a) Source # 
Instance details

Defined in Flat.Instances.Vector

(Storable a, Flat a) => Flat (Vector a) Source # 
Instance details

Defined in Flat.Instances.Vector

Flat a => Flat (Vector a) Source #

Vectors are encoded as arrays.

>>> tst (V.fromList [11::Word8,22,33])
(True,40,[3,11,22,33,0])

All Vectors are encoded in the same way:

>>> let l = [11::Word8,22,33] in all (tst (V.fromList l) ==) [tst (U.fromList l),tst (S.fromList l)]
True
Instance details

Defined in Flat.Instances.Vector

(IsMap map, Flat (ContainerKey map), Flat (MapValue map)) => Flat (AsMap map) Source # 
Instance details

Defined in Flat.Instances.Mono

Methods

encode :: AsMap map -> Encoding Source #

decode :: Get (AsMap map) Source #

size :: AsMap map -> NumBits -> NumBits Source #

(IsSet set, Flat (Element set)) => Flat (AsSet set) Source # 
Instance details

Defined in Flat.Instances.Mono

Methods

encode :: AsSet set -> Encoding Source #

decode :: Get (AsSet set) Source #

size :: AsSet set -> NumBits -> NumBits Source #

(IsSequence l, Flat (Element l)) => Flat (AsList l) Source # 
Instance details

Defined in Flat.Instances.Mono

(IsSequence r, Flat (Element r)) => Flat (AsArray r) Source # 
Instance details

Defined in Flat.Instances.Mono

Flat a => Flat (PreAligned a) Source # 
Instance details

Defined in Flat.Filler

Flat a => Flat (PostAligned a) Source # 
Instance details

Defined in Flat.Filler

(Flat a, Flat b) => Flat (Either a b) Source #
>>> test (Left False::Either Bool ())
(True,2,"00")
>>> test (Right ()::Either Bool ())
(True,1,"1")
Instance details

Defined in Flat.Instances.Base

(Flat a, Flat b) => Flat (a, b) Source #

Tuples are supported up to 7 elements.

>>> test (False,())
(True,1,"0")
>>> test ((),())
(True,0,"")

"7 elements tuples ought to be enough for anybody" (Bill Gates - apocryphal)

>>> test (False,True,True,True,False,True,True)
(True,7,"0111011")

tst (1::Int,"2","3","4","5","6","7","8") ...error

Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b) -> Encoding Source #

decode :: Get (a, b) Source #

size :: (a, b) -> NumBits -> NumBits Source #

(Flat i, Flat e, Ix i, IArray UArray e) => Flat (UArray i e) Source # 
Instance details

Defined in Flat.Instances.Array

(Flat i, Flat e, Ix i) => Flat (Array i e) Source #

Array is encoded as (lowBound,highBound,AsArray (elems array)):

>>> let arr = A.array ((1::Word,4::Word),(2,5)) [((1,4),11::Word),((1,5),22),((2,4),33),((2,5),44)] in tst (bounds arr,AsArray(elems arr)) == tst arr
True

As it's easy to see:

>>> tst $ A.array ((1::Word,4::Word),(2,5)) [((1,4),11::Word),((1,5),22),((2,4),33),((2,5),44)]
(True,80,[1,4,2,5,4,11,22,33,44,0])
>>> tst $ A.array ((1,4),(2,5)) [((1,4),"1.4"),((1,5),"1.5"),((2,4),"2.4"),((2,5),"2.5")]
(True,160,[2,8,4,10,4,152,203,166,137,140,186,106,153,75,166,137,148,186,106,0])

Arrays and Unboxed Arrays are encoded in the same way:

>>> let bounds = ((1::Word,4::Word),(2,5));elems=[11::Word,22,33,44] in tst (U.listArray bounds elems :: U.UArray (Word,Word) Word) == tst (A.listArray bounds elems)
True
Instance details

Defined in Flat.Instances.Array

Methods

encode :: Array i e -> Encoding Source #

decode :: Get (Array i e) Source #

size :: Array i e -> NumBits -> NumBits Source #

(Flat a, Flat b, Ord a) => Flat (Map a b) Source #

Maps are encoded as lists:

>>> tst (Data.Map.empty :: Map () ())
(True,1,[0])
>>> asList Data.Map.fromList [("a","aa"),("b","bb")]
True

Key/Values are encoded in order:

>>> let l = [("a","aa"),("b","bb")] in tst (Data.Map.fromList l) == tst (Data.Map.fromList $ Prelude.reverse l)
True

IntMap and Map are encoded in the same way:

>>> let l = [(2::Int,"b"),(1,"a")] in tst (Data.IntMap.fromList l) == tst (Data.Map.fromList l)
True
Instance details

Defined in Flat.Instances.Containers

Methods

encode :: Map a b -> Encoding Source #

decode :: Get (Map a b) Source #

size :: Map a b -> NumBits -> NumBits Source #

(Hashable k, Eq k, Flat k, Flat v) => Flat (HashMap k v) Source #
>>> test (Data.HashMap.Strict.fromList [(1,11),(2,22)])
(True,35,"10000001 00001011 01000001 00001011 000")
>>> test (Data.HashMap.Lazy.fromList [(1,11),(2,22)])
(True,35,"10000001 00001011 01000001 00001011 000")
Instance details

Defined in Flat.Instances.Unordered

(Flat a, Flat b, Flat c) => Flat (a, b, c) Source # 
Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b, c) -> Encoding Source #

decode :: Get (a, b, c) Source #

size :: (a, b, c) -> NumBits -> NumBits Source #

Flat (f a) => Flat (Alt f a) Source #
>>> let w = Just (11::Word8); a = Alt w <> Alt (Just 24) in tst a == tst w
True
>>> let w = Just (11::Word8); a = Alt Nothing <> Alt w in tst a == tst w
True

Since: 0.4.4

Instance details

Defined in Flat.Instances.Base

Methods

encode :: Alt f a -> Encoding Source #

decode :: Get (Alt f a) Source #

size :: Alt f a -> NumBits -> NumBits Source #

(Flat a, Flat b, Flat c, Flat d) => Flat (a, b, c, d) Source # 
Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b, c, d) -> Encoding Source #

decode :: Get (a, b, c, d) Source #

size :: (a, b, c, d) -> NumBits -> NumBits Source #

(Flat a, Flat b, Flat c, Flat d, Flat e) => Flat (a, b, c, d, e) Source # 
Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b, c, d, e) -> Encoding Source #

decode :: Get (a, b, c, d, e) Source #

size :: (a, b, c, d, e) -> NumBits -> NumBits Source #

(Flat a, Flat b, Flat c, Flat d, Flat e, Flat f) => Flat (a, b, c, d, e, f) Source # 
Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b, c, d, e, f) -> Encoding Source #

decode :: Get (a, b, c, d, e, f) Source #

size :: (a, b, c, d, e, f) -> NumBits -> NumBits Source #

(Flat a, Flat b, Flat c, Flat d, Flat e, Flat f, Flat g) => Flat (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Flat.Instances.Base

Methods

encode :: (a, b, c, d, e, f, g) -> Encoding Source #

decode :: Get (a, b, c, d, e, f, g) Source #

size :: (a, b, c, d, e, f, g) -> NumBits -> NumBits Source #

getSize :: Flat a => a -> NumBits Source #

Calculate the maximum size in bits of the serialisation of the value