From b7e7e7682f5e680e401eee8145f4e6180e4c79d6 Mon Sep 17 00:00:00 2001 From: Alexander Vershilov Date: Wed, 18 Mar 2015 21:02:22 +0300 Subject: [PATCH] Add NFData instances to Closure --- distributed-static.cabal | 3 ++- src/Control/Distributed/Static.hs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/distributed-static.cabal b/distributed-static.cabal index 938b456..620604c 100644 --- a/distributed-static.cabal +++ b/distributed-static.cabal @@ -36,7 +36,8 @@ Library rank1dynamic >= 0.1 && < 0.3, containers >= 0.4 && < 0.6, bytestring >= 0.9 && < 0.11, - binary >= 0.5 && < 0.8 + binary >= 0.5 && < 0.8, + deepseq >= 1.3.0.1 && < 1.6 HS-Source-Dirs: src Default-Language: Haskell2010 Default-Extensions: DeriveDataTypeable diff --git a/src/Control/Distributed/Static.hs b/src/Control/Distributed/Static.hs index 05baf8f..a344f07 100644 --- a/src/Control/Distributed/Static.hs +++ b/src/Control/Distributed/Static.hs @@ -196,6 +196,7 @@ -- > where -- > sdictSendPort :: forall a. SerializableDict a -> SerializableDict (SendPort a) -- > sdictSendPort SerializableDict = SerializableDict +{-# LANGUAGE DeriveGeneric #-} module Control.Distributed.Static ( -- * Static values Static @@ -237,6 +238,7 @@ import Data.Map (Map) import qualified Data.Map as Map (lookup, empty, insert) import Control.Applicative ((<$>), (<*>)) import Control.Arrow as Arrow ((***), app) +import Control.DeepSeq (NFData(rnf)) import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply) import Data.Rank1Typeable ( Typeable @@ -247,6 +249,7 @@ import Data.Rank1Typeable , ANY4 , isInstanceOf ) +import GHC.Generics -------------------------------------------------------------------------------- -- Introducing static values -- @@ -255,11 +258,18 @@ import Data.Rank1Typeable data StaticLabel = StaticLabel String | StaticApply StaticLabel StaticLabel - deriving (Eq, Ord, Typeable, Show) + deriving (Eq, Ord, Typeable, Show, Generic) + +instance NFData StaticLabel where + rnf (StaticLabel s) = rnf s `seq` () + rnf (StaticApply a b) = rnf a `seq` rnf b `seq` () -- | A static value. Static is opaque; see 'staticLabel' and 'staticApply'. newtype Static a = Static StaticLabel - deriving (Eq, Ord, Typeable, Show) + deriving (Eq, Ord, Typeable, Show, Generic) + +instance NFData (Static a) where + rnf (Static s) = rnf s `seq` () instance Typeable a => Binary (Static a) where put (Static label) = putStaticLabel label >> put (typeOf (undefined :: a)) @@ -342,12 +352,14 @@ unstatic rtable (Static static) = do -- | A closure is a static value and an encoded environment data Closure a = Closure (Static (ByteString -> a)) ByteString - deriving (Eq, Ord, Typeable, Show) + deriving (Eq, Ord, Typeable, Show, Generic) instance Typeable a => Binary (Closure a) where put (Closure static env) = put static >> put env get = Closure <$> get <*> get +instance NFData (Closure a) where rnf (Closure f b) = rnf f `seq` rnf b `seq` () + closure :: Static (ByteString -> a) -- ^ Decoder -> ByteString -- ^ Encoded closure environment -> Closure a