{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances ,StandaloneDeriving #-}
module Flat.Instances.Base() where
import Data.Bool
import Data.Char
import Data.Fixed
import Flat.Instances.Util
import Data.Complex(Complex(..))
import Data.Ratio
import Prelude hiding ( mempty )
import Control.Monad ( liftM2 )
import qualified Data.List.NonEmpty as B
#if ! MIN_VERSION_base(4,8,0)
import Control.Applicative
import Data.Monoid (mempty)
#endif
#if MIN_VERSION_base(4,9,0)
import qualified Data.Semigroup as Semigroup
#endif
import qualified Data.Monoid as Monoid
#if !MIN_VERSION_base(4,11,0)
import Data.Monoid ((<>))
#endif
#if MIN_VERSION_base(4,8,0)
import Data.Functor.Identity (Identity (..))
#endif
#if !MIN_VERSION_base(4,9,0)
deriving instance Generic (Complex a)
#endif
#if MIN_VERSION_base(4,8,0)
instance Flat a => Flat (Identity a) where
encode :: Identity a -> Encoding
encode (Identity a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Identity a -> NumBits -> NumBits
size (Identity a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Identity a)
decode = a -> Identity a
forall a. a -> Identity a
Identity (a -> Identity a) -> Get a -> Get (Identity a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
#endif
instance Flat a => Flat (Monoid.Dual a) where
encode :: Dual a -> Encoding
encode (Monoid.Dual a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Dual a -> NumBits -> NumBits
size (Monoid.Dual a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Dual a)
decode = a -> Dual a
forall a. a -> Dual a
Monoid.Dual (a -> Dual a) -> Get a -> Get (Dual a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
instance Flat Monoid.All where
encode :: All -> Encoding
encode (Monoid.All Bool
a) = Bool -> Encoding
forall a. Flat a => a -> Encoding
encode Bool
a
size :: All -> NumBits -> NumBits
size (Monoid.All Bool
a) = Bool -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size Bool
a
decode :: Get All
decode = Bool -> All
Monoid.All (Bool -> All) -> Get Bool -> Get All
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Bool
forall a. Flat a => Get a
decode
instance Flat Monoid.Any where
encode :: Any -> Encoding
encode (Monoid.Any Bool
a) = Bool -> Encoding
forall a. Flat a => a -> Encoding
encode Bool
a
size :: Any -> NumBits -> NumBits
size (Monoid.Any Bool
a) = Bool -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size Bool
a
decode :: Get Any
decode = Bool -> Any
Monoid.Any (Bool -> Any) -> Get Bool -> Get Any
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Bool
forall a. Flat a => Get a
decode
instance Flat a => Flat (Monoid.Sum a) where
encode :: Sum a -> Encoding
encode (Monoid.Sum a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Sum a -> NumBits -> NumBits
size (Monoid.Sum a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Sum a)
decode = a -> Sum a
forall a. a -> Sum a
Monoid.Sum (a -> Sum a) -> Get a -> Get (Sum a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
instance Flat a => Flat (Monoid.Product a) where
encode :: Product a -> Encoding
encode (Monoid.Product a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Product a -> NumBits -> NumBits
size (Monoid.Product a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Product a)
decode = a -> Product a
forall a. a -> Product a
Monoid.Product (a -> Product a) -> Get a -> Get (Product a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
#if MIN_VERSION_base(4,9,0)
instance Flat (f a) => Flat (Monoid.Alt f a) where
encode :: Alt f a -> Encoding
encode (Monoid.Alt f a
a) = f a -> Encoding
forall a. Flat a => a -> Encoding
encode f a
a
size :: Alt f a -> NumBits -> NumBits
size (Monoid.Alt f a
a) = f a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size f a
a
decode :: Get (Alt f a)
decode = f a -> Alt f a
forall k (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt (f a -> Alt f a) -> Get (f a) -> Get (Alt f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (f a)
forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.Min a) where
encode :: Min a -> Encoding
encode (Semigroup.Min a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Min a -> NumBits -> NumBits
size (Semigroup.Min a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Min a)
decode = a -> Min a
forall a. a -> Min a
Semigroup.Min (a -> Min a) -> Get a -> Get (Min a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.Max a) where
encode :: Max a -> Encoding
encode (Semigroup.Max a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Max a -> NumBits -> NumBits
size (Semigroup.Max a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Max a)
decode = a -> Max a
forall a. a -> Max a
Semigroup.Max (a -> Max a) -> Get a -> Get (Max a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.First a) where
encode :: First a -> Encoding
encode (Semigroup.First a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: First a -> NumBits -> NumBits
size (Semigroup.First a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (First a)
decode = a -> First a
forall a. a -> First a
Semigroup.First (a -> First a) -> Get a -> Get (First a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.Last a) where
encode :: Last a -> Encoding
encode (Semigroup.Last a
a) = a -> Encoding
forall a. Flat a => a -> Encoding
encode a
a
size :: Last a -> NumBits -> NumBits
size (Semigroup.Last a
a) = a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Last a)
decode = a -> Last a
forall a. a -> Last a
Semigroup.Last (a -> Last a) -> Get a -> Get (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
forall a. Flat a => Get a
decode
#endif
instance Flat () where
encode :: () -> Encoding
encode ()
_ = Encoding
forall a. Monoid a => a
mempty
size :: () -> NumBits -> NumBits
size ()
_ = NumBits -> NumBits
forall a. a -> a
id
decode :: Get ()
decode = () -> Get ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
instance Flat Bool where
encode :: Bool -> Encoding
encode = Bool -> Encoding
eBool
size :: Bool -> NumBits -> NumBits
size = Bool -> NumBits -> NumBits
sBool
decode :: Get Bool
decode = Get Bool
dBool
instance Flat Char where
size :: Char -> NumBits -> NumBits
size = Char -> NumBits -> NumBits
sChar
encode :: Char -> Encoding
encode = Char -> Encoding
eChar
decode :: Get Char
decode = Get Char
dChar
instance Flat a => Flat (Maybe a)
instance ( Flat a, Flat b ) => Flat (Either a b)
instance Flat (Fixed a) where
encode :: Fixed a -> Encoding
encode (MkFixed Integer
n) = Integer -> Encoding
forall a. Flat a => a -> Encoding
encode Integer
n
size :: Fixed a -> NumBits -> NumBits
size (MkFixed Integer
n) = Integer -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size Integer
n
decode :: Get (Fixed a)
decode = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed (Integer -> Fixed a) -> Get Integer -> Get (Fixed a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Integer
forall a. Flat a => Get a
decode
instance Flat Word8 where
encode :: Word8 -> Encoding
encode = Word8 -> Encoding
eWord8
decode :: Get Word8
decode = Get Word8
dWord8
size :: Word8 -> NumBits -> NumBits
size = Word8 -> NumBits -> NumBits
sWord8
instance Flat Word where
size :: Word -> NumBits -> NumBits
size = Word -> NumBits -> NumBits
sWord
encode :: Word -> Encoding
encode = Word -> Encoding
eWord
decode :: Get Word
decode = Get Word
dWord
instance Flat Natural where
size :: Natural -> NumBits -> NumBits
size = Natural -> NumBits -> NumBits
sNatural
encode :: Natural -> Encoding
encode = Natural -> Encoding
eNatural
decode :: Get Natural
decode = Get Natural
dNatural
instance Flat Word16 where
encode :: Word16 -> Encoding
encode = Word16 -> Encoding
eWord16
decode :: Get Word16
decode = Get Word16
dWord16
size :: Word16 -> NumBits -> NumBits
size = Word16 -> NumBits -> NumBits
sWord16
instance Flat Word32 where
encode :: Word32 -> Encoding
encode = Word32 -> Encoding
eWord32
decode :: Get Word32
decode = Get Word32
dWord32
size :: Word32 -> NumBits -> NumBits
size = Word32 -> NumBits -> NumBits
sWord32
instance Flat Word64 where
encode :: Word64 -> Encoding
encode = Word64 -> Encoding
eWord64
decode :: Get Word64
decode = Get Word64
dWord64
size :: Word64 -> NumBits -> NumBits
size = Word64 -> NumBits -> NumBits
sWord64
instance Flat Int where
size :: NumBits -> NumBits -> NumBits
size = NumBits -> NumBits -> NumBits
sInt
encode :: NumBits -> Encoding
encode = NumBits -> Encoding
eInt
decode :: Get NumBits
decode = Get NumBits
dInt
instance Flat Integer where
size :: Integer -> NumBits -> NumBits
size = Integer -> NumBits -> NumBits
sInteger
encode :: Integer -> Encoding
encode = Integer -> Encoding
eInteger
decode :: Get Integer
decode = Get Integer
dInteger
instance Flat Int8 where
encode :: Int8 -> Encoding
encode = Int8 -> Encoding
eInt8
decode :: Get Int8
decode = Get Int8
dInt8
size :: Int8 -> NumBits -> NumBits
size = Int8 -> NumBits -> NumBits
sInt8
instance Flat Int16 where
size :: Int16 -> NumBits -> NumBits
size = Int16 -> NumBits -> NumBits
sInt16
encode :: Int16 -> Encoding
encode = Int16 -> Encoding
eInt16
decode :: Get Int16
decode = Get Int16
dInt16
instance Flat Int32 where
size :: Int32 -> NumBits -> NumBits
size = Int32 -> NumBits -> NumBits
sInt32
encode :: Int32 -> Encoding
encode = Int32 -> Encoding
eInt32
decode :: Get Int32
decode = Get Int32
dInt32
instance Flat Int64 where
size :: Int64 -> NumBits -> NumBits
size = Int64 -> NumBits -> NumBits
sInt64
encode :: Int64 -> Encoding
encode = Int64 -> Encoding
eInt64
decode :: Get Int64
decode = Get Int64
dInt64
instance Flat Float where
size :: Float -> NumBits -> NumBits
size = Float -> NumBits -> NumBits
sFloat
encode :: Float -> Encoding
encode = Float -> Encoding
eFloat
decode :: Get Float
decode = Get Float
dFloat
instance Flat Double where
size :: Double -> NumBits -> NumBits
size = Double -> NumBits -> NumBits
sDouble
encode :: Double -> Encoding
encode = Double -> Encoding
eDouble
decode :: Get Double
decode = Get Double
dDouble
instance Flat a => Flat (Complex a)
instance ( Integral a, Flat a ) => Flat (Ratio a) where
size :: Ratio a -> NumBits -> NumBits
size Ratio a
a = (a, a) -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size ( Ratio a -> a
forall a. Ratio a -> a
numerator Ratio a
a, Ratio a -> a
forall a. Ratio a -> a
denominator Ratio a
a )
encode :: Ratio a -> Encoding
encode Ratio a
a = (a, a) -> Encoding
forall a. Flat a => a -> Encoding
encode ( Ratio a -> a
forall a. Ratio a -> a
numerator Ratio a
a, Ratio a -> a
forall a. Ratio a -> a
denominator Ratio a
a )
decode :: Get (Ratio a)
decode = (a -> a -> Ratio a) -> Get a -> Get a -> Get (Ratio a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
(%) Get a
forall a. Flat a => Get a
decode Get a
forall a. Flat a => Get a
decode
instance {-# OVERLAPPABLE #-}Flat a => Flat [ a ]
instance {-# OVERLAPPABLE #-}Flat a => Flat (B.NonEmpty a)
instance {-# OVERLAPPABLE #-}( Flat a, Flat b ) => Flat ( a, b )
instance {-# OVERLAPPABLE #-}( Flat a, Flat b, Flat c ) => Flat ( a, b, c )
instance {-# OVERLAPPABLE #-}( Flat a, Flat b, Flat c, Flat d )
=> Flat ( a, b, c, d )
instance {-# OVERLAPPABLE #-}( Flat a, Flat b, Flat c, Flat d, Flat e )
=> Flat ( a, b, c, d, e )
instance {-# OVERLAPPABLE #-}( Flat a, Flat b, Flat c, Flat d, Flat e, Flat f )
=> Flat ( a, b, c, d, e, f )
instance {-# OVERLAPPABLE #-}( Flat a
, Flat b
, Flat c
, Flat d
, Flat e
, Flat f
, Flat g
) => Flat ( a, b, c, d, e, f, g )