Skip to content

Commit

Permalink
oh it's so beautiful (more cleanup)
Browse files Browse the repository at this point in the history
  • Loading branch information
raehik committed Mar 16, 2024
1 parent 0cd67c2 commit b7f0352
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 111 deletions.
4 changes: 2 additions & 2 deletions binrep.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ library
Binrep.Get
Binrep.Put
Binrep.Type.Byte
Binrep.Type.Int
Binrep.Type.Magic
Binrep.Type.NullPadded
Binrep.Type.NullTerminated
Expand All @@ -62,8 +61,9 @@ library
Binrep.Util.Class
Binrep.Util.Generic
Binrep.Util.Prefix
Binrep.Via
Binrep.Via.Prim
Data.Aeson.Extra.SizedVector
Raehik.Compat.FlatParse.Basic.Prim
Util.TypeNats
other-modules:
Paths_binrep
Expand Down
44 changes: 35 additions & 9 deletions src/Binrep/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ module Binrep.Get
, eBase
, getEBase
-- , GetWith(..), runGetWith
, getPrim
, getGenericNonSum, getGenericSum
) where

import Data.Functor.Identity
import Binrep.Util.ByteOrder
import Binrep.Via.Prim ( ViaPrim(..) )
import Raehik.Compat.Data.Primitive.Types ( Prim', sizeOf )
import Raehik.Compat.Data.Primitive.Types.Endian ( ByteSwap )

import FlatParse.Basic qualified as FP
import Raehik.Compat.FlatParse.Basic.Prim qualified as FP

import Data.ByteString qualified as B

import Binrep.Util.Class
Expand All @@ -19,7 +28,6 @@ import GHC.TypeLits ( TypeError )
import Data.Void
import Data.Word
import Data.Int
import Bytezap

import Data.Text ( Text )

Expand Down Expand Up @@ -200,6 +208,8 @@ instance Get Write where
-}

instance Get a => Get (Identity a) where get = Identity <$> get

-- | Unit type parses nothing.
instance Get () where
{-# INLINE get #-}
Expand Down Expand Up @@ -235,16 +245,32 @@ instance Get B.ByteString where
{-# INLINE get #-}
get = B.copy <$> FP.takeRest

-- | Unsigned byte.
instance Get Word8 where get = getEBase FP.anyWord8 (ERanOut 1)
-- | 8-bit (1-byte) words do not require byte order in order to precisely
-- define their representation.
deriving via ViaPrim Word8 instance Get Word8

-- | Signed byte.
instance Get Int8 where get = getEBase FP.anyInt8 (ERanOut 1)
-- | 8-bit (1-byte) words do not require byte order in order to precisely
-- define their representation.
deriving via ViaPrim Int8 instance Get Int8

{-
Multi-byte machine integers require an endianness to use. A common wrapper is
defined in "Binrep.Type.Int".
-}
-- | Byte order is irrelevant for 8-bit (1-byte) words.
deriving via Identity Word8 instance Get (ByteOrdered end Word8)

-- | Byte order is irrelevant for 8-bit (1-byte) words.
deriving via Identity Int8 instance Get (ByteOrdered end Int8)

-- | Parse any 'Prim''.
getPrim :: forall a. Prim' a => Getter a
getPrim = getEBase FP.anyPrim (ERanOut (sizeOf (undefined :: a)))

instance Prim' a => Get (ViaPrim a) where get = ViaPrim <$> getPrim

-- ByteSwap is required on opposite endian platforms, but we're not checking
-- here, so make sure to keep it on both.
deriving via ViaPrim (ByteOrdered 'LittleEndian a)
instance (Prim' a, ByteSwap a) => Get (ByteOrdered 'LittleEndian a)
deriving via ViaPrim (ByteOrdered 'BigEndian a)
instance (Prim' a, ByteSwap a) => Get (ByteOrdered 'BigEndian a)

{-
Expand Down
25 changes: 13 additions & 12 deletions src/Binrep/Put.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ simple 'BLen' instance (and if not, it shouldn't be a binrep type).

module Binrep.Put where

import Data.Functor.Identity
import Bytezap.Write
import Raehik.Compat.Data.Primitive.Types ( Prim' )
import Raehik.Compat.Data.Primitive.Types.Endian ( ByteOrdered(..), ByteSwap )
import GHC.ByteOrder
import Binrep.Util.ByteOrder
import Raehik.Compat.Data.Primitive.Types.Endian ( ByteSwap )
import Binrep.Via.Prim ( ViaPrim(..) )

import Data.ByteString qualified as B

Expand Down Expand Up @@ -65,13 +67,13 @@ putGenericSum
=> (String -> Write') -> a -> Write'
putGenericSum = genericFoldMapSum @'SumOnly @asserts

-- | DerivingVia newtype for 'Put' types which can borrow from 'Prim''.
newtype PutViaPrim a = PutViaPrim { unPutViaPrim :: a }
instance Prim' a => Put (PutViaPrim a) where put = prim . unPutViaPrim
instance Prim' a => Put (ViaPrim a) where put = prim . unViaPrim

instance TypeError ENoEmpty => Put Void where put = undefined
instance TypeError ENoSum => Put (Either a b) where put = undefined

instance Put a => Put (Identity a) where put = put . runIdentity

instance Put Write' where put = id

-- | Unit type serializes to nothing. How zen.
Expand All @@ -93,22 +95,21 @@ instance Put B.ByteString where

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

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

-- TODO maybe via binreply for these two (but need to move instances then...?)
-- | Byte order is irrelevant for 8-bit (1-byte) words.
deriving via PutViaPrim Word8 instance Put (ByteOrdered end Word8)
deriving via Identity Word8 instance Put (ByteOrdered end Word8)

-- | Byte order is irrelevant for 8-bit (1-byte) words.
deriving via PutViaPrim Int8 instance Put (ByteOrdered end Int8)
deriving via Identity 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.
deriving via PutViaPrim (ByteOrdered 'LittleEndian a)
deriving via ViaPrim (ByteOrdered 'LittleEndian a)
instance (Prim' a, ByteSwap a) => Put (ByteOrdered 'LittleEndian a)
deriving via PutViaPrim (ByteOrdered 'BigEndian a)
deriving via ViaPrim (ByteOrdered 'BigEndian a)
instance (Prim' a, ByteSwap a) => Put (ByteOrdered 'BigEndian a)
77 changes: 0 additions & 77 deletions src/Binrep/Type/Int.hs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Binrep/Via.hs

This file was deleted.

4 changes: 4 additions & 0 deletions src/Binrep/Via/Prim.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Binrep.Via.Prim where

-- | DerivingVia newtype for types which can borrow from 'Prim''.
newtype ViaPrim a = ViaPrim { unViaPrim :: a }
11 changes: 11 additions & 0 deletions src/Raehik/Compat/FlatParse/Basic/Prim.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Raehik.Compat.FlatParse.Basic.Prim where

import Raehik.Compat.Data.Primitive.Types
import FlatParse.Basic
import GHC.Exts ( plusAddr# )

anyPrim :: forall a e st. Prim' a => ParserT st e a
anyPrim = withEnsure# size# $ ParserT $ \_fp _eob buf st ->
OK# st (indexWord8OffAddrAs# buf 0#) (buf `plusAddr#` size#)
where
size# = sizeOf# (undefined :: a)

0 comments on commit b7f0352

Please sign in to comment.