[commit: packages/binary] master: Add Binary Void instance (57cf1f0)

git at git.haskell.org git at git.haskell.org
Wed Dec 16 09:43:19 UTC 2015


Repository : ssh://git@git.haskell.org/binary

On branch  : master
Link       : http://git.haskell.org/packages/binary.git/commitdiff/57cf1f0460fdc27b8f5783749c90201503a7a87a

>---------------------------------------------------------------

commit 57cf1f0460fdc27b8f5783749c90201503a7a87a
Author: Brian McKenna <bmckenna at atlassian.com>
Date:   Fri Oct 23 14:44:31 2015 +1100

    Add Binary Void instance
    
    This is so that we can write and read things like [Void], or (Expr Void)
    - something I'm actually doing in Morte.
    
    https://github.com/Gabriel439/Haskell-Morte-Library/pull/26
    
    The reader always fails if you actually try to get an instance of it.
    Trying to access a void value should not happen for well-formed
    instances, for example:
    
        *Data.Binary Data.Void> decode $ encode ([] :: [Void]) :: [Void]
        []


>---------------------------------------------------------------

57cf1f0460fdc27b8f5783749c90201503a7a87a
 binary.cabal             | 4 ++++
 src/Data/Binary/Class.hs | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/binary.cabal b/binary.cabal
index 37ed51c..503e588 100644
--- a/binary.cabal
+++ b/binary.cabal
@@ -50,6 +50,10 @@ library
       -- prior to ghc-7.4 generics lived in ghc-prim
       build-depends: ghc-prim
 
+  if impl(ghc <= 7.8)
+    -- Data.Void was moved to base for 7.10
+    build-depends: void
+
   ghc-options:     -O2 -Wall -fliberate-case-threshold=1000
 
 -- Due to circular dependency, we cannot make any of the test-suites or
diff --git a/src/Data/Binary/Class.hs b/src/Data/Binary/Class.hs
index b0f7529..9984a93 100644
--- a/src/Data/Binary/Class.hs
+++ b/src/Data/Binary/Class.hs
@@ -43,6 +43,7 @@ module Data.Binary.Class (
 import Data.Word
 import Data.Bits
 import Data.Int
+import Data.Void
 
 import Data.Binary.Put
 import Data.Binary.Get
@@ -128,6 +129,12 @@ class Binary t where
 ------------------------------------------------------------------------
 -- Simple instances
 
+-- Void never gets written nor reconstructed since it's impossible to have a
+-- value of that type
+instance Binary Void where
+    put     = absurd
+    get     = mzero
+
 -- The () type need never be written to disk: values of singleton type
 -- can be reconstructed from the type alone
 instance Binary () where



More information about the ghc-commits mailing list