Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Wrapper type to decode a value to its flat serialisation.
See ../test/Big.hs for an example of use.
See also listTDecoder
and Flat.AsSize for other ways to handle large decoded values.
In 0.5.X this type was called Repr
.
Since: 0.6
Documentation
When the flat serialisation of a value takes a lot less memory than the value itself, it can be convenient to keep the value in its encoded representation and decode it on demand.
To do so, just decode a value a
as a `AsBin a`.
Examples:
Encode a list of Ints and then decode it to a list of AsBin Int:
>>>
unflat (flat [1::Int .. 3]) :: Decoded ([AsBin Int])
Right [AsBin {repr = "\129A", offsetBits = 1},AsBin {repr = "A ", offsetBits = 2},AsBin {repr = " \193", offsetBits = 3}]
To decode an `AsBin a` to an a
, use unbin
:
>>>
unbin <$> (unflat (flat 'a') :: Decoded (AsBin Char))
Right 'a'
Keep the values of a list of Ints encoded and decode just one on demand:
>>>
let Right l :: Decoded [AsBin Int] = unflat (flat [1..5]) in unbin (l !! 2)
3
Show exactly how values are encoded:
>>>
let Right t :: Decoded (AsBin Bool,AsBin Word8,AsBin Bool) = unflat (flat (False,3:: Word64,True)) in prettyShow t
"(0, _0000001 1, _1)"
Ten bits in total spread over two bytes:
0 _0000001 1 _1 = 00000001 11
Tests:
>>>
unflat (flat ()) :: Decoded (AsBin ())
Right (AsBin {repr = "", offsetBits = 0})
>>>
unflat (flat (False,True)) :: Decoded (Bool,AsBin Bool)
Right (False,AsBin {repr = "A", offsetBits = 1})
>>>
unflat (flat (False,False,255 :: Word8)) :: Decoded (Bool,Bool,AsBin Word8)
Right (False,False,AsBin {repr = "?\193", offsetBits = 2})
>>>
let Right (b0,b1,rw,b3) :: Decoded (Bool,Bool,AsBin Word8,Bool) = unflat (flat (False,False,255 :: Word8,True)) in (b0,b1,unbin rw,b3)
(False,False,255,True)