Skip to content

Commit

Permalink
place PutC (struct putting) in own module
Browse files Browse the repository at this point in the history
  • Loading branch information
raehik committed Apr 3, 2024
1 parent 0fed4d4 commit 15b68cc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 62 deletions.
2 changes: 1 addition & 1 deletion binrep.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ library
Binrep.BLen
Binrep.CBLen
Binrep.CBLen.Generic
Binrep.Common.Class.Generic
Binrep.Common.Class.TypeErrors
Binrep.Common.Via.Prim
Binrep.Extra.HexByteString
Binrep.Generic
Binrep.Get
Binrep.Put
Binrep.Put.Struct
Binrep.Type.Byte
Binrep.Type.Magic
Binrep.Type.NullPadded
Expand Down
25 changes: 0 additions & 25 deletions bytezap/TODO.md

This file was deleted.

6 changes: 0 additions & 6 deletions src/Binrep/Common/Class/Generic.hs

This file was deleted.

33 changes: 3 additions & 30 deletions src/Binrep/Put.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{-# LANGUAGE UndecidableInstances #-} -- required below GHC 9.6
{-# OPTIONS_GHC -fno-warn-orphans #-} -- for generic data op instance
{-# LANGUAGE AllowAmbiguousTypes #-} -- TODO tmp

module Binrep.Put where

import Binrep.BLen ( BLen(blen) )
import Data.Functor.Identity
import Bytezap.Poke
import Bytezap.Struct qualified as Struct
import Bytezap.Struct.Generic qualified as Struct
import Raehik.Compat.Data.Primitive.Types ( Prim', sizeOf )
import Binrep.Util.ByteOrder
import Raehik.Compat.Data.Primitive.Types.Endian ( ByteSwap )
Expand All @@ -17,7 +13,7 @@ import Binrep.Common.Via.Prim ( ViaPrim(..) )
import Data.ByteString qualified as B

import Binrep.Common.Class.TypeErrors ( ENoSum, ENoEmpty )
import GHC.TypeLits ( TypeError, KnownNat )
import GHC.TypeLits ( TypeError )

import Data.Void
import Data.Word
Expand All @@ -30,17 +26,11 @@ import Generic.Data.Rep.Assert

import Control.Monad.ST ( RealWorld )

import Binrep.Common.Class.Generic ( BinrepG )
import Binrep.CBLen

type Putter = Poke RealWorld
type PutterC = Struct.Poke RealWorld
import Binrep.Put.Struct ( PutC(putC) )

type Putter = Poke RealWorld
class Put a where put :: a -> Putter

-- | constant size putter
class PutC a where putC :: a -> PutterC

runPut :: (BLen a, Put a) => a -> B.ByteString
runPut a = unsafeRunPokeBS (blen a) (put a)

Expand Down Expand Up @@ -69,23 +59,6 @@ putGenericSum
=> (String -> Putter) -> a -> Putter
putGenericSum = genericFoldMapSum @'SumOnly @asserts @Put

instance Struct.GPokeBase BinrepG where
type GPokeBaseSt BinrepG = RealWorld
type GPokeBaseC BinrepG a = PutC a
gPokeBase = Struct.unPoke . putC
type KnownSizeOf' BinrepG a = KnownNat (CBLen a)
sizeOf' = reifyCBLenProxy#

putGenericStruct
:: forall a
. ( Generic a, Struct.GPoke BinrepG (Rep a) )
=> a -> PutterC
putGenericStruct = Struct.Poke . Struct.gPoke @BinrepG . from

instance Prim' a => PutC (ViaPrim a) where
putC = Struct.prim . unViaPrim
{-# INLINE putC #-}

instance Prim' a => Put (ViaPrim a) where
put = fromStructPoke (sizeOf (undefined :: a)) . putC
{-# INLINE put #-}
Expand Down
33 changes: 33 additions & 0 deletions src/Binrep/Put/Struct.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Binrep.Put.Struct where

import Bytezap.Struct qualified as Struct
import Bytezap.Struct.Generic qualified as Struct
import Control.Monad.ST ( RealWorld )
import Raehik.Compat.Data.Primitive.Types ( Prim' )
import GHC.Generics
import Binrep.CBLen
import Binrep.Common.Via.Prim ( ViaPrim(..) )
import GHC.TypeLits ( KnownNat )

type PutterC = Struct.Poke RealWorld

-- | constant size putter
class PutC a where putC :: a -> PutterC

instance Struct.GPokeBase PutC where
type GPokeBaseSt PutC = RealWorld
type GPokeBaseC PutC a = PutC a
gPokeBase = Struct.unPoke . putC
type KnownSizeOf' PutC a = KnownNat (CBLen a)
sizeOf' = reifyCBLenProxy#

-- | Serialize a term of the struct-like type @a@ via its 'Generic' instance.
putGenericStruct
:: forall a
. ( Generic a, Struct.GPoke PutC (Rep a) )
=> a -> PutterC
putGenericStruct = Struct.Poke . Struct.gPoke @PutC . from

instance Prim' a => PutC (ViaPrim a) where
putC = Struct.prim . unViaPrim
{-# INLINE putC #-}

0 comments on commit 15b68cc

Please sign in to comment.