Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial fork of regular-binary.

  • Loading branch information...
commit 242e67c100fa62584224d5126df46265704489ed 0 parents
@sebastiaanvisser authored
1  .gitignore
@@ -0,0 +1 @@
+dist/
28 LICENSE
@@ -0,0 +1,28 @@
+Copyright (c) Sebastiaan Visser 2011
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the author nor the names of his contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
8 README
@@ -0,0 +1,8 @@
+Use the Haskell cabal-install to install this library:
+
+ cabal configure &&
+ cabal build &&
+ cabal install
+
+Please report problems to: haskell@fvisser.nl
+
6 Setup.lhs
@@ -0,0 +1,6 @@
+#! /usr/bin/env runhaskell
+
+>import Distribution.Simple
+
+>main = defaultMain
+
18 generic-binary.cabal
@@ -0,0 +1,18 @@
+Name: generic-binary
+Version: 1.0.0
+Description: Generic Data.Binary derivation using GHC generics.
+Synopsis: Generic Data.Binary derivation using GHC generics.
+Category: Generics, Data
+License: BSD3
+License-file: LICENSE
+Author: Sebastiaan Visser
+Maintainer: haskell@fvisser.nl
+Build-Type: Simple
+Build-Depends: base >= 4.0 && < 4.5
+ , ghc-prim < 0.3
+ , binary >= 0.5 && < 0.6
+
+GHC-Options: -Wall
+HS-Source-Dirs: src
+Exposed-modules: Data.Binary.Generic
+
52 src/Data/Binary/Generic.hs
@@ -0,0 +1,52 @@
+{-# LANGUAGE
+ FlexibleContexts
+ , FlexibleInstances
+ , TypeOperators
+ #-}
+module Data.Binary.Generic (put, get) where
+
+import Control.Applicative
+import GHC.Generics
+import Data.Binary hiding (put, get)
+
+import qualified Data.Binary as B
+
+put :: (Generic a, GBinary (Rep a)) => a -> Put
+put = gput . from
+
+get :: (Generic b, GBinary (Rep b)) => Get b
+get = to <$> gget
+
+class GBinary f where
+ gput :: f a -> Put
+ gget :: Get (f a)
+
+instance GBinary U1 where
+ gput _ = return ()
+ gget = pure U1
+
+instance (GBinary a, GBinary b) => GBinary (a :*: b) where
+ gput (x :*: y) = do gput x; gput y
+ gget = (:*:) <$> gget <*> gget
+
+instance (GBinary a, GBinary b) => GBinary (a :+: b) where
+ gput (L1 l) = do B.put False; gput l
+ gput (R1 r) = do B.put True; gput r
+ gget = B.get >>= \v -> if v then L1 <$> gget else R1 <$> gget
+
+instance GBinary a => GBinary (M1 D c a) where
+ gput = gput . unM1
+ gget = M1 <$> gget
+
+instance GBinary a => GBinary (M1 C c a) where
+ gput = gput . unM1
+ gget = M1 <$> gget
+
+instance GBinary a => GBinary (M1 S s a) where
+ gput = gput . unM1
+ gget = M1 <$> gget
+
+instance Binary a => GBinary (K1 i a) where
+ gput = B.put . unK1
+ gget = K1 <$> B.get
+
Please sign in to comment.
Something went wrong with that request. Please try again.