generic-binary (empty) → 1.0.0
raw patch · 4 files changed
+110/−0 lines, 4 filesdep +basedep +binarydep +ghc-primsetup-changed
Dependencies added: base, binary, ghc-prim
Files
- LICENSE +28/−0
- Setup.lhs +6/−0
- generic-binary.cabal +24/−0
- src/Data/Binary/Generic.hs +52/−0
+ LICENSE view
@@ -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.+
+ Setup.lhs view
@@ -0,0 +1,6 @@+#! /usr/bin/env runhaskell++>import Distribution.Simple++>main = defaultMain+
+ generic-binary.cabal view
@@ -0,0 +1,24 @@+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+Cabal-Version: >= 1.6+Build-Type: Simple+Stability: provisional++Source-Repository head+ Type: git+ Location: https://github.com/sebastiaanvisser/generic-binary++Library+ GHC-Options: -Wall+ HS-Source-Dirs: src+ Exposed-modules: Data.Binary.Generic+ Build-Depends: base >= 4.0 && < 4.5+ , ghc-prim < 0.3+ , binary >= 0.5 && < 0.6
+ src/Data/Binary/Generic.hs view
@@ -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+