-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
D.Compat.Binary: backport binary generics to binary-0.5
GHC generics are used to derive binary instances for types appearing in the persistent build config, which requires GHC >= 7.2 and binary >= 0.7. Unfortunately, GHC < 7.8 ships with binary == 0.5.*. The missing module is Data.Binary.Generics, which we have copied from binary-0.7.2.3 to Distribution.Compat.Binary.Generics. To provide generic implementations for the Binary class, we also have to provide our own implementation, which is copied from binary-0.7.2.3 to Distribution.Compat.Binary.Class. The interface required by Cabal is exported from Distribution.Compat.Binary. This is only done if bootstrapping Cabal with GHC < 7.8 or if binary >= 0.7 is not available, otherwise Distribution.Compat.Binary simply re-exports Data.Binary.
- Loading branch information
Showing
23 changed files
with
754 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE CPP #-} | ||
|
||
#ifndef MIN_VERSION_binary | ||
#define MIN_VERSION_binary(x, y, z) 0 | ||
#endif | ||
|
||
module Distribution.Compat.Binary | ||
( decodeOrFailIO | ||
#if __GLASGOW_HASKELL__ >= 708 || MIN_VERSION_binary(0,7,0) | ||
, module Data.Binary | ||
#else | ||
, Binary(..) | ||
, decode, encode | ||
#endif | ||
) where | ||
|
||
import Data.ByteString.Lazy (ByteString) | ||
|
||
#if __GLASGOW_HASKELL__ >= 708 || MIN_VERSION_binary(0,7,0) | ||
|
||
import Data.Binary | ||
|
||
decodeOrFailIO :: Binary a => ByteString -> IO (Either String a) | ||
decodeOrFailIO bs = | ||
return $ case decodeOrFail bs of | ||
Left (_, _, msg) -> Left msg | ||
Right (_, _, a) -> Right a | ||
|
||
#else | ||
|
||
import Control.Exception (ErrorCall(..), catch, evaluate) | ||
import Data.Binary.Get | ||
import Data.Binary.Put | ||
#if __GLASGOW_HASKELL__ < 706 | ||
import Prelude hiding (catch) | ||
#endif | ||
|
||
import Distribution.Compat.Binary.Class | ||
import Distribution.Compat.Binary.Generic () | ||
|
||
-- | Decode a value from a lazy ByteString, reconstructing the original structure. | ||
-- | ||
decode :: Binary a => ByteString -> a | ||
decode = runGet get | ||
|
||
-- | Encode a value using binary serialisation to a lazy ByteString. | ||
-- | ||
encode :: Binary a => a -> ByteString | ||
encode = runPut . put | ||
{-# INLINE encode #-} | ||
|
||
decodeOrFailIO :: Binary a => ByteString -> IO (Either String a) | ||
decodeOrFailIO bs = | ||
catch (evaluate (decode bs) >>= return . Right) | ||
$ \(ErrorCall str) -> return $ Left str | ||
|
||
#endif |
Oops, something went wrong.