Skip to content

Commit

Permalink
move type-level bytes to bytezap; more PutC; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raehik committed Apr 5, 2024
1 parent d973fae commit 9531a1f
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 341 deletions.
27 changes: 6 additions & 21 deletions binrep.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ library
Binrep.CBLen.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.Test
Binrep.Type.Magic
Binrep.Type.NullPadded
Binrep.Type.NullTerminated
Expand All @@ -63,7 +62,6 @@ library
Binrep.Util.ByteOrder
Binrep.Util.Generic
Binrep.Util.Prefix
Data.Aeson.Extra.SizedVector
Raehik.Compat.FlatParse.Basic.Prim
Raehik.Compat.FlatParse.Basic.WithLength
Util.TypeNats
Expand All @@ -82,24 +80,20 @@ library
TypeFamilies
DataKinds
MagicHash
ghc-options: -Wall
ghc-options: -Wall -ddump-simpl -ddump-stg-final -ddump-cmm -dsuppress-all -dno-suppress-type-signatures -ddump-to-file
build-depends:
aeson >=2.0 && <2.2
, base >=4.14 && <5
base >=4.14 && <5
, bytestring >=0.11 && <0.13
, bytezap
, deepseq >=1.4.6.1 && <1.6
, flatparse >=0.5.0.2 && <0.6
, generic-data-asserts >=0.1.0 && <0.2
, generic-data-functions >=0.4.0 && <0.5
, megaparsec >=9.2.0 && <9.7
, parser-combinators >=1.3.0 && <1.4
, primitive >=0.8.0.0 && <0.10.0.0
, refined1 ==0.9.*
, strongweak >=0.6.0 && <0.7
, text >=1.2.5.0 && <2.1
, vector >=0.12.3.1 && <0.14
, vector-sized >=1.5.0 && <1.6
default-language: GHC2021
if flag(icu)
cpp-options: -DHAVE_ICU
Expand All @@ -111,7 +105,6 @@ test-suite spec
main-is: Spec.hs
other-modules:
ArbitraryOrphans
Binrep.Extra.HexByteStringSpec
Binrep.LawsSpec
Paths_binrep
hs-source-dirs:
Expand All @@ -127,12 +120,11 @@ test-suite spec
TypeFamilies
DataKinds
MagicHash
ghc-options: -Wall
ghc-options: -Wall -ddump-simpl -ddump-stg-final -ddump-cmm -dsuppress-all -dno-suppress-type-signatures -ddump-to-file
build-tool-depends:
hspec-discover:hspec-discover >=2.7 && <2.12
build-depends:
QuickCheck >=2.14.2 && <2.15
, aeson >=2.0 && <2.2
, base >=4.14 && <5
, binrep
, bytestring >=0.11 && <0.13
Expand All @@ -143,15 +135,12 @@ test-suite spec
, generic-data-functions >=0.4.0 && <0.5
, generic-random >=1.5.0.1 && <1.6
, hspec >=2.7 && <2.12
, megaparsec >=9.2.0 && <9.7
, parser-combinators >=1.3.0 && <1.4
, primitive >=0.8.0.0 && <0.10.0.0
, quickcheck-instances >=0.3.26 && <0.4
, refined1 ==0.9.*
, strongweak >=0.6.0 && <0.7
, text >=1.2.5.0 && <2.1
, vector >=0.12.3.1 && <0.14
, vector-sized >=1.5.0 && <1.6
default-language: GHC2021
if flag(icu)
cpp-options: -DHAVE_ICU
Expand All @@ -176,10 +165,9 @@ benchmark bench
TypeFamilies
DataKinds
MagicHash
ghc-options: -Wall
ghc-options: -Wall -ddump-simpl -ddump-stg-final -ddump-cmm -dsuppress-all -dno-suppress-type-signatures -ddump-to-file
build-depends:
aeson >=2.0 && <2.2
, base >=4.14 && <5
base >=4.14 && <5
, binrep
, bytestring >=0.11 && <0.13
, bytezap
Expand All @@ -188,14 +176,11 @@ benchmark bench
, gauge
, generic-data-asserts >=0.1.0 && <0.2
, generic-data-functions >=0.4.0 && <0.5
, megaparsec >=9.2.0 && <9.7
, parser-combinators >=1.3.0 && <1.4
, primitive >=0.8.0.0 && <0.10.0.0
, refined1 ==0.9.*
, strongweak >=0.6.0 && <0.7
, text >=1.2.5.0 && <2.1
, vector >=0.12.3.1 && <0.14
, vector-sized >=1.5.0 && <1.6
default-language: GHC2021
if flag(icu)
cpp-options: -DHAVE_ICU
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ default-extensions:
ghc-options:
- -Wall

# TODO tmp core dumps
- -ddump-simpl
- -ddump-stg-final
- -ddump-cmm
- -dsuppress-all
- -dno-suppress-type-signatures
- -ddump-to-file

dependencies:
- base >= 4.14 && < 5
- bytezap
Expand All @@ -63,13 +71,8 @@ dependencies:
- strongweak ^>= 0.6.0
- refined1 ^>= 0.9
- text >= 1.2.5.0 && < 2.1
- vector-sized ^>= 1.5.0
- vector >= 0.12.3.1 && < 0.14
- deepseq >= 1.4.6.1 && < 1.6

- megaparsec >= 9.2.0 && < 9.7 # for HexByteString
- aeson >= 2.0 && < 2.2 # because we use refined which uses it (also for HexByteString)

# - containers (only for Haskpatch lol)

library:
Expand Down
2 changes: 1 addition & 1 deletion src/Binrep/CBLen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ instance IsCBLen a => IsCBLen (ByteOrdered end a) where
type CBLen (ByteOrdered end a) = CBLen a

-- | Reify a type's constant byte length to the term level.
cblen :: forall a n. KnownNat (CBLen a) => Int
cblen :: forall a. KnownNat (CBLen a) => Int
cblen = natValInt @(CBLen a)

cblen# :: forall a. KnownNat (CBLen a) => Int#
Expand Down
2 changes: 2 additions & 0 deletions src/Binrep/CBLen/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import Data.Kind
import Data.Type.Equality
import Data.Type.Bool

-- TODO provide non-sum version

type CBLenGeneric w a = GCBLen w (Rep a)

type family GCBLen w (f :: k -> Type) :: Natural where
Expand Down
14 changes: 11 additions & 3 deletions src/Binrep/Put.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Binrep.Put where

import Binrep.BLen ( BLen(blen) )
import Binrep.CBLen ( IsCBLen(CBLen), cblen )
import Data.Functor.Identity
import Bytezap.Poke
import Raehik.Compat.Data.Primitive.Types ( Prim', sizeOf )
Expand All @@ -13,7 +14,7 @@ import Binrep.Common.Via.Prim ( ViaPrim(..) )
import Data.ByteString qualified as B

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

import Data.Void
import Data.Word
Expand Down Expand Up @@ -59,6 +60,12 @@ putGenericSum
) => (String -> Putter) -> a -> Putter
putGenericSum = genericFoldMapSum @Put @'SumOnly

newtype ViaPutC a = ViaPutC { unViaPutC :: a }
instance (PutC a, KnownNat (CBLen a)) => Put (ViaPutC a) where
{-# INLINE put #-}
put = fromStructPoke (cblen @a) . putC . unViaPutC

-- use ViaPutC over this, but should be semantically identical
instance Prim' a => Put (ViaPrim a) where
put = fromStructPoke (sizeOf (undefined :: a)) . putC
{-# INLINE put #-}
Expand Down Expand Up @@ -89,11 +96,11 @@ instance Put B.ByteString where

-- | 8-bit (1-byte) words do not require byte order in order to precisely
-- define their representation.
deriving via ViaPrim Word8 instance Put Word8
deriving via ViaPutC Word8 instance Put Word8

-- | 8-bit (1-byte) words do not require byte order in order to precisely
-- define their representation.
deriving via ViaPrim Int8 instance Put Int8
deriving via ViaPutC Int8 instance Put Int8

-- | Byte order is irrelevant for 8-bit (1-byte) words.
deriving via Word8 instance Put (ByteOrdered end Word8)
Expand All @@ -103,6 +110,7 @@ deriving via Int8 instance Put (ByteOrdered end Int8)

-- ByteSwap is required on opposite endian platforms, but we're not checking
-- here, so make sure to keep it on both.
-- Stick with ViaPrim here because ByteOrdered is connected to it.
deriving via ViaPrim (ByteOrdered 'LittleEndian a)
instance (Prim' a, ByteSwap a) => Put (ByteOrdered 'LittleEndian a)
deriving via ViaPrim (ByteOrdered 'BigEndian a)
Expand Down
15 changes: 15 additions & 0 deletions src/Binrep/Test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# LANGUAGE UndecidableInstances #-}

module Binrep.Test where

import Binrep
import Binrep.Type.Magic
import Binrep.CBLen.Generic
import GHC.Generics ( Generic )

data DMagic = DMagic
{ dMagic1_8b :: Magic '[0xFF, 0, 1, 0, 1, 0, 1, 0xFF]
} deriving stock Generic

instance IsCBLen DMagic where type CBLen DMagic = CBLenGeneric () DMagic
instance PutC DMagic where putC = putGenericStruct
Loading

0 comments on commit 9531a1f

Please sign in to comment.