diff --git a/CHANGELOG.md b/CHANGELOG.md index bc31c21..1630ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -## 1.1.0 (unreleased) -* fix ByteOrdered orphan strongweak instances to follow strongweak pattern -- - for original behaviour, use `WeakenN 2 (ByteOrdered end a)` -* update rerefined, strongweak +## 1.1.0 (2025-03-11) +* remove strongweak integration + * to be released in separate package binrep-instances +* update rerefined ## 1.0.0 (2024-10-03) * fix `NullTerminate` check being inverted (OOPS LOL) diff --git a/LICENSE b/LICENSE index c970cd0..237644d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2022-2024 Ben Orchard (@raehik) +Copyright (c) 2022-2025 Ben Orchard (@raehik) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/TODO.md b/TODO.md index 777581c..74be65b 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,18 @@ # binrep to-dos +## Separate from strongweak +I don't use strongweak here, only write a few useful instances. +I could have a separate package for orphan instances instead. +This would make me much happier about: + +* the `WeakenN` instances, which are the only "through" newtype currently + permitted in binrep -- these would be over there +* the `ByteOrdered` orphans -- they would be over there + +But I feel comfortable with the `Magic` weakening being here. And strongweak is +a small package. But still... + +Yes, I think I should do this. No, I don't think it's very interesting or fun. + ## Discrepancy in generic sum interface In `Get`, I require a `Getter pt`. But in `Put`/`BLen`, you have to pass the `Putter`/`BLen` manually. I'm not sure which one is correct. Probably `Get`. diff --git a/binrep.cabal b/binrep.cabal index d68d29e..ead3d68 100644 --- a/binrep.cabal +++ b/binrep.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: binrep -version: 1.0.0 +version: 1.1.0 synopsis: Encode precise binary representations directly in types description: Please see README.md. category: Data, Serialization, Generics @@ -100,7 +100,6 @@ library , ghc-bignum ==1.3.* , parser-combinators >=1.3.0 && <1.4 , rerefined >=0.8.0 && <0.9 - , strongweak >=0.11.0 && <0.12 , text >=2.0 && <2.2 , text-builder-linear >=0.1.3 && <0.2 , type-level-bytestrings >=0.1.0 && <0.3 @@ -153,7 +152,6 @@ test-suite spec , parser-combinators >=1.3.0 && <1.4 , quickcheck-instances >=0.3.26 && <0.4 , rerefined >=0.8.0 && <0.9 - , strongweak >=0.11.0 && <0.12 , text >=2.0 && <2.2 , text-builder-linear >=0.1.3 && <0.2 , type-level-bytestrings >=0.1.0 && <0.3 @@ -198,7 +196,6 @@ benchmark bench , ghc-bignum ==1.3.* , parser-combinators >=1.3.0 && <1.4 , rerefined >=0.8.0 && <0.9 - , strongweak >=0.11.0 && <0.12 , text >=2.0 && <2.2 , text-builder-linear >=0.1.3 && <0.2 , type-level-bytestrings >=0.1.0 && <0.3 diff --git a/flake.lock b/flake.lock index 515cb30..671d957 100644 --- a/flake.lock +++ b/flake.lock @@ -1,31 +1,15 @@ { "nodes": { - "bytezap": { - "flake": false, - "locked": { - "lastModified": 1727486916, - "narHash": "sha256-C7Lo/8stL35tVAFNN1Ab2Hpyp8qoxJNf84BNLtZOGWE=", - "owner": "raehik", - "repo": "bytezap", - "rev": "0970bfe3a6e660544c73e76ac377a4a836a8ab28", - "type": "github" - }, - "original": { - "owner": "raehik", - "repo": "bytezap", - "type": "github" - } - }, "flake-parts": { "inputs": { "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1726153070, - "narHash": "sha256-HO4zgY0ekfwO5bX0QH/3kJ/h4KvUDFZg8YpkNwIbg1U=", + "lastModified": 1741352980, + "narHash": "sha256-+u2UunDA4Cl5Fci3m7S643HzKmIDAe+fiXrLqYsR2fs=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "bcef6817a8b2aa20a5a6dbb19b43e63c5bf8619a", + "rev": "f4330d22f1c5d2ba72d3d22df5597d123fdb60a9", "type": "github" }, "original": { @@ -36,11 +20,11 @@ }, "haskell-flake": { "locked": { - "lastModified": 1725811060, - "narHash": "sha256-lmPoWYNT8kI8LSTa0Z3id38SoR05+05IEZ3fxeIwOzE=", + "lastModified": 1741528687, + "narHash": "sha256-NPxvw+FaxMPgwGGGiTbmNpfjFhR1HwPRWTBJJufWQ6Q=", "owner": "srid", "repo": "haskell-flake", - "rev": "9b490fe01ec8f6858f43d3cc81bfbbd0000eb29e", + "rev": "f66fe097c01b4ac20a28b4f26e760dffd5891967", "type": "github" }, "original": { @@ -51,11 +35,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1726042813, - "narHash": "sha256-LnNKCCxnwgF+575y0pxUdlGZBO/ru1CtGHIqQVfvjlA=", + "lastModified": 1741494726, + "narHash": "sha256-OGQeoY72/881aar76xEn4j1BP9s5Ezdi4BxLgYVlqv0=", "owner": "nixos", "repo": "nixpkgs", - "rev": "159be5db480d1df880a0135ca0bfed84c2f88353", + "rev": "9004cb4a05a42b15a6a2eef84acc72152e45ff33", "type": "github" }, "original": { @@ -67,56 +51,24 @@ }, "nixpkgs-lib": { "locked": { - "lastModified": 1725233747, - "narHash": "sha256-Ss8QWLXdr2JCBPcYChJhz4xJm+h/xjl4G0c0XlP6a74=", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/356624c12086a18f2ea2825fed34523d60ccc4e3.tar.gz" - }, - "original": { - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/356624c12086a18f2ea2825fed34523d60ccc4e3.tar.gz" - } - }, - "rerefined": { - "flake": false, - "locked": { - "lastModified": 1728746060, - "narHash": "sha256-hF3egnah1V7h59U6PLjSGu+fPyzDmqGWLorhxaBRgzU=", - "owner": "raehik", - "repo": "rerefined", - "rev": "43c3c5f058f0a7213e957dd54bc73b7fecd1d47e", + "lastModified": 1740877520, + "narHash": "sha256-oiwv/ZK/2FhGxrCkQkB83i7GnWXPPLzoqFHpDD3uYpk=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "147dee35aab2193b174e4c0868bd80ead5ce755c", "type": "github" }, "original": { - "owner": "raehik", - "repo": "rerefined", + "owner": "nix-community", + "repo": "nixpkgs.lib", "type": "github" } }, "root": { "inputs": { - "bytezap": "bytezap", "flake-parts": "flake-parts", "haskell-flake": "haskell-flake", - "nixpkgs": "nixpkgs", - "rerefined": "rerefined", - "strongweak": "strongweak" - } - }, - "strongweak": { - "flake": false, - "locked": { - "lastModified": 1729135838, - "narHash": "sha256-0a2d62gu3s+j1ArBba1SZju2nnc+xF+DXCRe/fvBtU4=", - "owner": "raehik", - "repo": "strongweak", - "rev": "35831d7524cbfd4e9881b413911e66dc8fccb06a", - "type": "github" - }, - "original": { - "owner": "raehik", - "repo": "strongweak", - "type": "github" + "nixpkgs": "nixpkgs" } } }, diff --git a/flake.nix b/flake.nix index 7cdc785..f5f3d44 100644 --- a/flake.nix +++ b/flake.nix @@ -3,12 +3,6 @@ nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; flake-parts.url = "github:hercules-ci/flake-parts"; haskell-flake.url = "github:srid/haskell-flake"; - rerefined.url = "github:raehik/rerefined"; - rerefined.flake = false; - bytezap.url = "github:raehik/bytezap"; - bytezap.flake = false; - strongweak.url = "github:raehik/strongweak"; - strongweak.flake = false; }; outputs = inputs: @@ -39,34 +33,16 @@ # https://github.com/phadej/defun/pull/5 settings.defun-core.jailbreak = true; - - # waiting on nixpkgs update - packages.rerefined.source = inputs.rerefined; - settings.strongweak.broken = false; - packages.strongweak.source = inputs.strongweak; - packages.bytezap.source = inputs.bytezap; }; haskellProjects.ghc98 = { basePackages = pkgs.haskell.packages.ghc98; devShell = defDevShell "ghc98"; - - # waiting on nixpkgs update - packages.rerefined.source = inputs.rerefined; - settings.strongweak.broken = false; - packages.strongweak.source = inputs.strongweak; - packages.bytezap.source = inputs.bytezap; }; haskellProjects.ghc96 = { basePackages = pkgs.haskell.packages.ghc96; devShell = defDevShell "ghc96"; - - # waiting on nixpkgs update - packages.rerefined.source = inputs.rerefined; - settings.strongweak.broken = false; - packages.strongweak.source = inputs.strongweak; - packages.bytezap.source = inputs.bytezap; }; }; }; diff --git a/package.yaml b/package.yaml index 56e4c3b..3d1718a 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: binrep -version: 1.0.0 +version: 1.1.0 synopsis: Encode precise binary representations directly in types description: Please see README.md. extra-source-files: @@ -68,7 +68,6 @@ dependencies: - generic-data-functions ^>= 0.6.0 - generic-type-asserts ^>= 0.3.0 - generic-type-functions ^>= 0.1.0 -- strongweak ^>= 0.11.0 - defun-core ^>= 0.1 # core instances diff --git a/src/Binrep/BLen.hs b/src/Binrep/BLen.hs index d15fcd6..ded5ce9 100644 --- a/src/Binrep/BLen.hs +++ b/src/Binrep/BLen.hs @@ -45,8 +45,6 @@ import Binrep.Common.Via.Generically.NonSum import Rerefined.Refine import Rerefined.Predicate.Logical.And -import Strongweak.WeakenN - -- | Class for types with easily-calculated length in bytes. -- -- If it appears hard to calculate byte length for a given type (e.g. without @@ -130,13 +128,11 @@ deriving via ViaCBLen Word32 instance BLen Word32 deriving via ViaCBLen Int32 instance BLen Int32 deriving via ViaCBLen Word64 instance BLen Word64 deriving via ViaCBLen Int64 instance BLen Int64 + +-- | Explicitness does not alter length. deriving via ViaCBLen (ByteOrdered end a) instance KnownNat (CBLen a) => BLen (ByteOrdered end a) --- | Unwrap strongweak wrapper. -instance BLen a => BLen (WeakenN n a) where - blen = blen . unWeakenN - -------------------------------------------------------------------------------- -- | DerivingVia wrapper for types which may derive a 'BLen' instance through diff --git a/src/Binrep/CBLen.hs b/src/Binrep/CBLen.hs index de745ad..249ba43 100644 --- a/src/Binrep/CBLen.hs +++ b/src/Binrep/CBLen.hs @@ -29,8 +29,6 @@ import Bytezap.Common.Generic ( type GTFoldMapCAddition ) import Binrep.Common.Via.Generically.NonSum -import Strongweak.WeakenN - class IsCBLen a where type CBLen a :: Natural -- | Deriving via this instance necessitates @UndecidableInstances@. @@ -55,12 +53,8 @@ instance IsCBLen Int32 where type CBLen Int32 = 2^2 instance IsCBLen Word64 where type CBLen Word64 = 2^3 instance IsCBLen Int64 where type CBLen Int64 = 2^3 -instance IsCBLen a => IsCBLen (ByteOrdered end a) where - type CBLen (ByteOrdered end a) = CBLen a - --- | Unwrap strongweak wrapper. -instance IsCBLen a => IsCBLen (WeakenN n a) where - type CBLen (WeakenN n a) = CBLen a +-- | Endianness does not alter constant length. +deriving via (a :: Type) instance IsCBLen a => IsCBLen (ByteOrdered end a) -- | Reify a type's constant byte length to the term level. cblen :: forall a. KnownNat (CBLen a) => Int diff --git a/src/Binrep/Example/Tga.hs b/src/Binrep/Example/Tga.hs index 81374b1..05360c6 100644 --- a/src/Binrep/Example/Tga.hs +++ b/src/Binrep/Example/Tga.hs @@ -1,5 +1,7 @@ module Binrep.Example.Tga where +{- + {- import Binrep import Binrep.Type.Derived.NullTermPadded @@ -26,3 +28,5 @@ data ImageType | RLEColorMapped | RLETrueColor | RLEBW + +-} diff --git a/src/Binrep/Get.hs b/src/Binrep/Get.hs index 4509a40..db5a4e2 100644 --- a/src/Binrep/Get.hs +++ b/src/Binrep/Get.hs @@ -43,7 +43,6 @@ import Rerefined.Predicate.Logical.And import Data.Word import Data.Int import Data.Void -import Data.Functor.Identity import Binrep.Common.Via.Generically.NonSum import Generic.Data.FOnCstr @@ -52,8 +51,6 @@ import GHC.Exts ( Proxy# ) import Data.Typeable ( Typeable, TypeRep, typeRep, Proxy(Proxy) ) -import Strongweak.WeakenN - type Getter = FP.Parser (ParseError FP.Pos TBL.Builder) class Get a where @@ -222,8 +219,6 @@ instance Get Write where -} -instance Get a => Get (Identity a) where get = Identity <$> get - -- | Unit type parses nothing. instance Get () where {-# INLINE get #-} @@ -324,7 +319,3 @@ runGetWith runGetWith r bs = runGetter (getWith r) bs -} - --- | Unwrap strongweak wrapper. -instance Get a => Get (WeakenN n a) where - get = WeakenN <$> get diff --git a/src/Binrep/Get/Struct.hs b/src/Binrep/Get/Struct.hs index 7ac462c..69df191 100644 --- a/src/Binrep/Get/Struct.hs +++ b/src/Binrep/Get/Struct.hs @@ -25,7 +25,6 @@ import Raehik.Compat.Data.Primitive.Types ( Prim' ) import Data.Word ( Word8 ) import Data.Int ( Int8 ) import Binrep.Util.ByteOrder -import Data.Functor.Identity import Raehik.Compat.Data.Primitive.Types.Endian ( ByteSwap ) import Data.ByteString qualified as B @@ -37,8 +36,6 @@ import Binrep.Common.Via.Generically.NonSum import Rerefined.Refine import Rerefined.Predicate.Logical.And -import Strongweak.WeakenN - type GetterC = Parser (ParseError Int TBL.Builder) -- | constant size parser @@ -129,8 +126,6 @@ instance Prim' a => GetC (ViaPrim a) where getC = ViaPrim <$> prim {-# INLINE getC #-} -instance GetC a => GetC (Identity a) where getC = Identity <$> getC - deriving via ViaPrim Word8 instance GetC Word8 deriving via ViaPrim Int8 instance GetC Int8 deriving via Word8 instance GetC (ByteOrdered end Word8) @@ -142,23 +137,3 @@ deriving via ViaPrim (ByteOrdered LittleEndian a) instance (Prim' a, ByteSwap a) => GetC (ByteOrdered LittleEndian a) deriving via ViaPrim (ByteOrdered BigEndian a) instance (Prim' a, ByteSwap a) => GetC (ByteOrdered BigEndian a) - -{- - -instance TypeError ENoEmpty => PutC Void where putC = undefined -instance TypeError ENoSum => PutC (Either a b) where putC = undefined - -instance PutC a => PutC (Identity a) where putC = putC . runIdentity - -instance PutC PutterC where putC = id - --- | Look weird? Yeah. But it's correct :) -instance (PutC l, KnownNat (CBLen l), PutC r) => PutC (l, r) where - {-# INLINE putC #-} - putC (l, r) = sequencePokes (putC l) (cblen @l) (putC r) - --} - --- | Unwrap strongweak wrapper. -instance GetC a => GetC (WeakenN n a) where - getC = WeakenN <$> getC diff --git a/src/Binrep/Put.hs b/src/Binrep/Put.hs index caa290f..043afcb 100644 --- a/src/Binrep/Put.hs +++ b/src/Binrep/Put.hs @@ -31,11 +31,8 @@ import Rerefined.Predicate.Logical.And import Data.Word import Data.Int import Data.Void -import Data.Functor.Identity import Binrep.Common.Via.Generically.NonSum -import Strongweak.WeakenN - type Putter = Poke RealWorld class Put a where put :: a -> Putter @@ -92,8 +89,6 @@ instance Prim' a => Put (ViaPrim a) where 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 Putter where put = id -- | Unit type serializes to nothing. How zen. @@ -139,7 +134,3 @@ deriving via ViaPrim (ByteOrdered BigEndian a) -- predicate with the right. LOL REALLY? instance Put (Refined pr (Refined pl a)) => Put (Refined (pl `And` pr) a) where put = put . unsafeRefine @_ @pr . unsafeRefine @_ @pl . unrefine - --- | Unwrap strongweak wrapper. -instance Put a => Put (WeakenN n a) where - put = put . unWeakenN diff --git a/src/Binrep/Put/Struct.hs b/src/Binrep/Put/Struct.hs index e0ba3e3..13d8242 100644 --- a/src/Binrep/Put/Struct.hs +++ b/src/Binrep/Put/Struct.hs @@ -15,7 +15,6 @@ import Raehik.Compat.Data.Primitive.Types ( Prim' ) import Data.Word import Data.Int import Binrep.Util.ByteOrder -import Data.Functor.Identity import Raehik.Compat.Data.Primitive.Types.Endian ( ByteSwap ) import Binrep.Common.Class.TypeErrors ( ENoSum, ENoEmpty ) @@ -29,8 +28,6 @@ import Binrep.Common.Via.Generically.NonSum import Rerefined.Refine import Rerefined.Predicate.Logical.And -import Strongweak.WeakenN - type PutterC = Struct.Poke RealWorld -- | constant size putter @@ -76,8 +73,6 @@ instance Prim' a => PutC (ViaPrim a) where instance TypeError ENoEmpty => PutC Void where putC = undefined instance TypeError ENoSum => PutC (Either a b) where putC = undefined -instance PutC a => PutC (Identity a) where putC = putC . runIdentity - instance PutC PutterC where putC = id -- | Unit type serializes to nothing. How zen. @@ -110,7 +105,3 @@ deriving via ViaPrim (ByteOrdered LittleEndian a) instance (Prim' a, ByteSwap a) => PutC (ByteOrdered LittleEndian a) deriving via ViaPrim (ByteOrdered BigEndian a) instance (Prim' a, ByteSwap a) => PutC (ByteOrdered BigEndian a) - --- | Unwrap strongweak wrapper. -instance PutC a => PutC (WeakenN n a) where - putC = putC . unWeakenN diff --git a/src/Binrep/Type/Magic.hs b/src/Binrep/Type/Magic.hs index 1f768fe..fafe836 100644 --- a/src/Binrep/Type/Magic.hs +++ b/src/Binrep/Type/Magic.hs @@ -26,7 +26,6 @@ import GHC.TypeLits ( type Natural, type Symbol, type KnownNat, type (+) ) import GHC.Generics ( Generic ) import Data.Data ( Data ) -import Strongweak import Binrep import Bytezap.Struct.TypeLits.Bytes ( ReifyBytesW64(reifyBytesW64) ) @@ -49,14 +48,6 @@ applications). That kind is always evident from the type, so it's just nicer. data Magic a where Magic :: forall {k} (a :: k). Magic a deriving stock (Generic, Data, Show, Eq) --- | Weaken a @'Magic' a@ to the unit '()'. -instance Weaken (Magic a) where - type Weakened (Magic a) = () - weaken Magic = () - --- | Strengthen the unit '()' to some @'Magic' a@. -instance Strengthen (Magic a) where strengthen () = Right Magic - -- | The byte length of a magic is known at compile time. instance IsCBLen (Magic a) where type CBLen (Magic a) = Length (MagicBytes a) diff --git a/src/Binrep/Type/Thin.hs b/src/Binrep/Type/Thin.hs index 6ea444c..8e95355 100644 --- a/src/Binrep/Type/Thin.hs +++ b/src/Binrep/Type/Thin.hs @@ -15,11 +15,8 @@ instance Copy B.ByteString where copy = B.copy @ But this just doesn't fly, because it would invert the behaviour. - -} -{-# LANGUAGE UndecidableInstances #-} -- for strongweak derivingvia - module Binrep.Type.Thin where import Binrep @@ -31,8 +28,6 @@ import Data.Data ( Data ) import GHC.Exts ( IsList ) import Data.String import Control.DeepSeq -import Data.Functor.Identity -import Strongweak import Data.ByteString qualified as B @@ -45,7 +40,4 @@ newtype Thin a = Thin { unThin :: a } , BLen, Put -- binrep ) via a - -- at the end of the day, we are the identity functor - deriving (Weaken, Strengthen) via Identity a - instance Get (Thin B.ByteString) where get = Thin <$> FP.takeRest diff --git a/src/Binrep/Util/ByteOrder.hs b/src/Binrep/Util/ByteOrder.hs index eaa99d6..24142d9 100644 --- a/src/Binrep/Util/ByteOrder.hs +++ b/src/Binrep/Util/ByteOrder.hs @@ -1,5 +1,3 @@ -{-# OPTIONS_GHC -fno-warn-orphans #-} - module Binrep.Util.ByteOrder ( ByteOrder(..), ByteOrdered(..), type EndianSuffix , type LE, type BE, type Endian @@ -7,12 +5,8 @@ module Binrep.Util.ByteOrder import Raehik.Compat.Data.Primitive.Types.Endian ( ByteOrdered(..) ) import GHC.ByteOrder ( ByteOrder(..) ) -import Strongweak import GHC.TypeLits ( type Symbol ) -deriving via SWCoercibly a instance Weaken (ByteOrdered end a) -deriving via SWCoercibly a instance Strengthen (ByteOrdered end a) - -- shorter names I originally used type LE = LittleEndian type BE = BigEndian