{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Binrep.Put.Bytezap where
import Bytezap
import Bytezap.Poke.Bytes
import Bytezap.Poke.Int
import Data.ByteString qualified as B
import Binrep.BLen.Simple
import Binrep.Util.Class
import GHC.TypeLits ( TypeError )
import Data.Void
import Data.Word
import Data.Int
import GHC.Generics
import Generic.Data.Function.FoldMap
import Generic.Data.Function.Common
import Generic.Data.Rep.Assert
class Put a where put :: a -> Poke
runPut :: (BLen a, Put a) => a -> B.ByteString
runPut :: forall a. (BLen a, Put a) => a -> ByteString
runPut a
a = Int -> Poke -> ByteString
runPoke (a -> Int
forall a. BLen a => a -> Int
blen a
a) (a -> Poke
forall a. Put a => a -> Poke
put a
a)
{-# INLINE runPut #-}
instance GenericFoldMap Poke where
type GenericFoldMapC Poke a = Put a
genericFoldMapF :: forall a. GenericFoldMapC Poke a => a -> Poke
genericFoldMapF = a -> Poke
forall a. Put a => a -> Poke
put
putGenericNonSum
:: forall {cd} {f} {asserts} a
. ( Generic a, Rep a ~ D1 cd f, GFoldMapNonSum Poke f
, asserts ~ '[ 'NoEmpty, 'NoSum], ApplyGCAsserts asserts f)
=> a -> Poke
putGenericNonSum :: forall {cd :: Meta} {f :: Type -> Type} {asserts :: [GCAssert]} a.
(Generic a, Rep a ~ D1 cd f, GFoldMapNonSum Poke f,
asserts ~ '[ 'NoEmpty, 'NoSum], ApplyGCAsserts asserts f) =>
a -> Poke
putGenericNonSum = forall (asserts :: [GCAssert]) m a.
(Generic a, Rep a ~ D1 cd f, GFoldMapNonSum m f,
ApplyGCAsserts asserts f) =>
a -> m
forall {cd :: Meta} {f :: Type -> Type} (asserts :: [GCAssert]) m
a.
(Generic a, Rep a ~ D1 cd f, GFoldMapNonSum m f,
ApplyGCAsserts asserts f) =>
a -> m
genericFoldMapNonSum @asserts
putGenericSum
:: forall {cd} {f} {asserts} a
. (Generic a, Rep a ~ D1 cd f, GFoldMapSum 'SumOnly Poke f
, asserts ~ '[ 'NoEmpty, 'NeedSum], ApplyGCAsserts asserts f)
=> (String -> Poke) -> a -> Poke
putGenericSum :: forall {cd :: Meta} {f :: Type -> Type} {asserts :: [GCAssert]} a.
(Generic a, Rep a ~ D1 cd f, GFoldMapSum 'SumOnly Poke f,
asserts ~ '[ 'NoEmpty, 'NeedSum], ApplyGCAsserts asserts f) =>
(String -> Poke) -> a -> Poke
putGenericSum = forall {cd :: Meta} {f :: Type -> Type} (opts :: SumOpts)
(asserts :: [GCAssert]) m a.
(Generic a, Rep a ~ D1 cd f, GFoldMapSum opts m f,
ApplyGCAsserts asserts f) =>
(String -> m) -> a -> m
forall (opts :: SumOpts) (asserts :: [GCAssert]) m a.
(Generic a, Rep a ~ D1 cd f, GFoldMapSum opts m f,
ApplyGCAsserts asserts f) =>
(String -> m) -> a -> m
genericFoldMapSum @'SumOnly @asserts
instance TypeError ENoEmpty => Put Void where put :: Void -> Poke
put = Void -> Poke
forall a. HasCallStack => a
undefined
instance TypeError ENoSum => Put (Either a b) where put :: Either a b -> Poke
put = Either a b -> Poke
forall a. HasCallStack => a
undefined
instance Put Write where
{-# INLINE put #-}
put :: Write -> Poke
put = Write -> Poke
writePoke
instance Put Poke where
{-# INLINE put #-}
put :: Poke -> Poke
put = Poke -> Poke
forall a. a -> a
id
instance Put () where
{-# INLINE put #-}
put :: () -> Poke
put = () -> Poke
forall a. Monoid a => a
mempty
instance (Put l, Put r) => Put (l, r) where
{-# INLINE put #-}
put :: (l, r) -> Poke
put (l
l, r
r) = l -> Poke
forall a. Put a => a -> Poke
put l
l Poke -> Poke -> Poke
forall a. Semigroup a => a -> a -> a
<> r -> Poke
forall a. Put a => a -> Poke
put r
r
instance Put a => Put [a] where
{-# INLINE put #-}
put :: [a] -> Poke
put = [Poke] -> Poke
forall a. Monoid a => [a] -> a
mconcat ([Poke] -> Poke) -> ([a] -> [Poke]) -> [a] -> Poke
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Poke) -> [a] -> [Poke]
forall a b. (a -> b) -> [a] -> [b]
map a -> Poke
forall a. Put a => a -> Poke
put
instance Put B.ByteString where
{-# INLINE put #-}
put :: ByteString -> Poke
put = ByteString -> Poke
byteString
instance Put Word8 where
{-# INLINE put #-}
put :: Word8 -> Poke
put = Word8 -> Poke
w8
instance Put Int8 where
{-# INLINE put #-}
put :: Int8 -> Poke
put = Int8 -> Poke
i8