Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Add NFData instances to Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
qnikst committed Mar 18, 2015
1 parent 5c7f843 commit b7e7e76
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion distributed-static.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/Control/Distributed/Static.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -247,6 +249,7 @@ import Data.Rank1Typeable
, ANY4
, isInstanceOf
)
import GHC.Generics

--------------------------------------------------------------------------------
-- Introducing static values --
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b7e7e76

Please sign in to comment.