Commit 0b40accf authored by Oleg Grenrus's avatar Oleg Grenrus
Browse files

Add Binary (Data.Fixed a) instance

parent db201353
......@@ -11,10 +11,18 @@
#define HAS_VOID
#endif
#if MIN_VERSION_base(4,7,0)
#define HAS_FIXED_CONSTRUCTOR
#endif
#if __GLASGOW_HASKELL__ >= 704
#define HAS_GHC_FINGERPRINT
#endif
#ifndef HAS_FIXED_CONSTRUCTOR
{-# LANGUAGE ScopedTypeVariables #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module : Data.Binary.Class
......@@ -78,6 +86,9 @@ import GHC.Generics
#ifdef HAS_NATURAL
import Numeric.Natural
#endif
import qualified Data.Fixed as Fixed
--
-- This isn't available in older Hugs or older GHC
--
......@@ -251,6 +262,18 @@ instance Binary Integer where
let v = roll bytes
return $! if sign == (1 :: Word8) then v else - v
-- | /Since: 0.8.0.0/
#ifdef HAS_FIXED_CONSTRUCTOR
instance Binary (Fixed.Fixed a) where
put (Fixed.MkFixed a) = put a
get = Fixed.MkFixed `liftM` get
#else
instance forall a. Fixed.HasResolution a => Binary (Fixed.Fixed a) where
-- Using undefined :: Maybe a as a proxy, as Data.Proxy is introduced only in base-4.7
put x = put (truncate (x * fromInteger (Fixed.resolution (undefined :: Maybe a))) :: Integer)
get = (\x -> fromInteger x / fromInteger (Fixed.resolution (undefined :: Maybe a))) `liftM` get
#endif
--
-- Fold and unfold an Integer to and from a list of its bytes
--
......
......@@ -28,6 +28,8 @@ import Numeric.Natural
import GHC.Fingerprint
#endif
import qualified Data.Fixed as Fixed
import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.QuickCheck
......@@ -475,6 +477,7 @@ tests =
, ("Word", p (test :: T Word ))
, ("Int", p (test :: T Int ))
, ("Integer", p (test :: T Integer ))
, ("Fixed", p (test :: T (Fixed.Fixed Fixed.E3) ))
#ifdef HAS_NATURAL
, ("Natural", prop_test_Natural )
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment