Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TODO: fix since annotations
- Add `Data.Binary.Get.getShortByteString`
- Don't reexport `Data.Word` from `Data.Binary`
- Add `Binary (Proxy a)` instance
- Add `Binary (Solo a)` instance (requires `base >= 4.16`)
- Add binary instance for `GHC.Generics.Generically`. `Binary T` can
be derived via `Generically T` for a suitable generic type `T`.
- Correctness and safety fixes:
Expand Down
22 changes: 22 additions & 0 deletions src/Data/Binary/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#if MIN_VERSION_base(4,16,0)
#define HAS_TYPELITS_CHAR
#define HAS_SOLO
#endif

#if MIN_VERSION_base(4,17,0)
Expand Down Expand Up @@ -71,6 +72,9 @@ import Data.Bits
import Data.Int
import Data.Complex (Complex(..))
import Data.Proxy (Proxy(..))
#ifdef HAS_SOLO
import Data.Tuple (Solo(..))
#endif
import Data.Void

import Data.Binary.Put
Expand Down Expand Up @@ -773,6 +777,24 @@ getChr i = pure $! chr i
------------------------------------------------------------------------
-- Instances for the first few tuples

#ifdef HAS_SOLO
-- | Encoded the same way as the wrapped value.
--
-- @since x.x.x.x
instance Binary a => Binary (Solo a) where
#if MIN_VERSION_base(4,18,0)
{-# INLINE put #-}
put (MkSolo a) = put a
{-# INLINE get #-}
get = MkSolo <$> get
#else
{-# INLINE put #-}
put (Solo a) = put a
{-# INLINE get #-}
get = Solo <$> get
#endif
#endif

instance (Binary a, Binary b) => Binary (a,b) where
{-# INLINE put #-}
put (a,b) = put a <> put b
Expand Down
8 changes: 8 additions & 0 deletions tests/QC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import qualified Data.Semigroup as Semigroup
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Tree as Tree
#if MIN_VERSION_base(4,16,0)
import Data.Tuple (Solo(..))
#endif
import Data.Typeable
import Data.Word
import Foreign (Ptr, castPtr, free, mallocBytes, peek, peekByteOff, poke)
Expand Down Expand Up @@ -1468,6 +1471,11 @@ tests =
, test' "Float" (test :: T Float ) test
, test' "Double" (test :: T Double) test

#if MIN_VERSION_base(4,18,0)
, testWithGen "Solo Int" (MkSolo <$> (arbitrary :: Gen Int))
#elif MIN_VERSION_base(4,16,0)
, testWithGen "Solo Int" (Solo <$> (arbitrary :: Gen Int))
#endif
Comment on lines +1474 to +1478

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use testWithGen here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QuickCheck only has an Arbitrary (Solo a) instance since 2.17 and the test suite still allows QuickCheck >= 2.9, so I used an explicit generator like the Identity and Monoid.Sum tests below instead of adding an orphan instance or CPP on the QuickCheck version.

, test' "((), ())" (test :: T ((), ()) ) test
, test' "(Word8, Word32)" (test :: T (Word8, Word32) ) test
, test' "(Int8, Int32)" (test :: T (Int8, Int32) ) test
Expand Down
8 changes: 8 additions & 0 deletions tests/WireFormatGolden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import qualified Data.Map as Map
import Data.Ratio ((%))
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
#if MIN_VERSION_base(4,16,0)
import Data.Tuple (Solo(..))
#endif
import Data.Word
import GHC.Fingerprint (Fingerprint(..))
import GHC.Generics (Generic)
Expand Down Expand Up @@ -173,6 +176,11 @@ wireFormatGoldenFailures =
, check "Maybe Just 7" (Just 7 :: Maybe Word8) [0x01, 0x07]
, check "Either Left" (Left 7 :: Either Word8 Word16) [0x00, 0x07]
, check "Either Right" (Right 0x0102 :: Either Word8 Word16) [0x01, 0x01, 0x02]
#if MIN_VERSION_base(4,18,0)
, check "Solo Word16" (MkSolo 0x0102 :: Solo Word16) [0x01, 0x02]
#elif MIN_VERSION_base(4,16,0)
, check "Solo Word16" (Solo 0x0102 :: Solo Word16) [0x01, 0x02]
#endif
, check "Ratio Int" (3 % 4 :: Rational)
([0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x04])
, check "Fixed E2" (1.23 :: Fixed.Fixed Fixed.E2)
Expand Down
Loading