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
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,40 @@ the ``Get`` and ``Put`` monads.

More information in the haddock documentation.

## Deriving binary instances using GHC's Generic ##
## Deriving binary instances, ``Generically`` ##

Beginning with GHC 7.2, it is possible to use binary serialization without
writing any instance boilerplate code.
Beginning with GHC 9.4 it is possible to derive binary serialization
using the ``Generically`` newtype.

This is achieved by deriving an instance of ``Generic`` and then
deriving the appropriate ``Binary T`` instance via ``Generically T``.

```haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}

import Data.Binary
import GHC.Generics (Generic)
import GHC.Generics (Generic, Generically(..))

data Foo = Foo
deriving stock Generic
deriving Binary via Generically Foo
```

data Foo = Foo deriving (Generic)
Beginning with GHC 7.2, a generic definition has been a part of the
``Binary`` typeclass. This could also be derived using the
``anyclass`` strategy:

-- GHC will automatically fill out the instance
instance Binary Foo
```haskell
data Foo = Foo
deriving stock Generic
deriving anyclass Binary
```

Which means the same as an empty class declaration: ``instance
Binary Foo``.

## Contributors ##

* Lennart Kolmodin
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ binary-x.x.x.x

- Don't reexport `Data.Word` from `Data.Binary`
- Add `Binary (Proxy a)` instance
- Add binary instance for `GHC.Generics.Generically`. `Binary T` can
be derived via `Generically T` for a suitable generic type `T`.

binary-0.8.9.2
--------------
Expand Down
32 changes: 22 additions & 10 deletions src/Data/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,31 @@ decodeFileOrFail f =
------------------------------------------------------------------------
-- $generics
--
-- Beginning with GHC 7.2, it is possible to use binary serialization
-- without writing any instance boilerplate code.
-- Beginning with GHC 9.4 it is possible to derive binary
-- serialization using the 'GHC.Generics.Generically' newtype.
--
-- > {-# LANGUAGE DeriveGeneric #-}
-- This is achieved by deriving an instance of 'GHC.Generics.Generic'
-- and then deriving the appropriate @'Binary' T@ instance via
-- @Generically T@.
--
-- > {-# LANGUAGE DeriveGeneric #-}
-- > {-# LANGUAGE DerivingStrategies #-}
-- > {-# LANGUAGE DerivingVia #-}
-- >
-- > import Data.Binary
-- > import GHC.Generics (Generic)
-- > import GHC.Generics (Generic, Generically(..))
-- >
-- > data Foo = Foo
-- > deriving (Generic)
-- >
-- > -- GHC will automatically fill out the instance
-- > instance Binary Foo
-- > deriving stock Generic
-- > deriving Binary via Generically Foo
--
-- This mechanism makes use of GHC's efficient built-in generics
-- support.
-- Beginning with GHC 7.2, a generic definition has been a part of
-- the 'Binary' typeclass. This could also be derived using the
-- @anyclass@ strategy:
--
-- > data Foo = Foo
-- > deriving stock Generic
-- > deriving anyclass Binary
--
-- Which means the same as an empty class declaration: @instance
-- Binary Foo@.
15 changes: 15 additions & 0 deletions src/Data/Binary/Class.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{-# LANGUAGE CPP, FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE Trustworthy #-}

Check warning on line 9 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (8.0.2)

‘Data.Binary.Class’ is marked as Trustworthy but has been inferred as safe!

Check warning on line 9 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (8.0.2)

‘Data.Binary.Class’ is marked as Trustworthy but has been inferred as safe!

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

#if MIN_VERSION_base(4,17,0)
#define HAS_GENERICALLY
#endif

#if MIN_VERSION_base(4,8,0)
#define HAS_NATURAL
#define HAS_VOID
Expand Down Expand Up @@ -176,6 +182,15 @@
defaultPutList :: Binary a => [a] -> Put
defaultPutList xs = put (length xs) <> mapM_ put xs

#ifdef HAS_GENERICALLY
instance (Generic a, GBinaryPut (Rep a), GBinaryGet (Rep a)) => Binary (Generically a) where
put :: Generically a -> Put
put (Generically a) = gput (from a)

get :: Get (Generically a)
get = Generically . to <$> gget
#endif

------------------------------------------------------------------------
-- Simple instances

Expand Down Expand Up @@ -829,9 +844,9 @@

#if __GLASGOW_HASKELL__ < 901
-- | @since 0.8.4.0
instance Binary a => Binary (Semigroup.Option a) where

Check warning on line 847 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of type constructor or class ‘Option’

Check warning on line 847 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of type constructor or class ‘Option’
get = fmap Semigroup.Option get

Check warning on line 848 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of data constructor ‘Option’

Check warning on line 848 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of data constructor ‘Option’
put = put . Semigroup.getOption

Check warning on line 849 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of ‘getOption’ (imported from Data.Semigroup):

Check warning on line 849 in src/Data/Binary/Class.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

In the use of ‘getOption’ (imported from Data.Semigroup):
#endif

-- | @since 0.8.4.0
Expand Down
Loading