1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE UndecidableInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
6module Control.Distributed.Process.Serializable
14 , SerializableDict(SerializableDict)
17import Data.Binary (Binary)
18import Data.Typeable (Typeable(..))
20#if MIN_VERSION_base(4,6,0)
21import Data.Typeable.Internal (TypeRep(TypeRep))
23import Data.Typeable.Internal (TypeRep(TypeRep), typeOf)
26import Numeric (showHex)
27import Control.Exception (throw)
28import GHC.Fingerprint.Type (Fingerprint(..))
29import Data.ByteString (ByteString)
30import qualified Data.ByteString as BS
31import qualified Data.ByteString.Internal as BSI ( unsafeCreate
35import Foreign.Storable (pokeByteOff, peekByteOff, sizeOf)
36import Foreign.ForeignPtr (withForeignPtr)
38-- | Reification of 'Serializable' (see "Control.Distributed.Process.Closure")
39data SerializableDict a where
40 SerializableDict :: Serializable a => SerializableDict a
43-- | Objects that can be sent across the network
44class (Binary a, Typeable a) => Serializable a
45instance (Binary a, Typeable a) => Serializable a
47-- | Encode type representation as a bytestring
48encodeFingerprint :: Fingerprint -> ByteString
50 -- Since all CH nodes will run precisely the same binary, we don't have to
51 -- worry about cross-arch issues here (like endianness)
52 BSI.unsafeCreate sizeOfFingerprint $ \p -> pokeByteOff p 0 fp
54-- | Decode a bytestring into a fingerprint. Throws an IO exception on failure
55decodeFingerprint :: ByteString -> Fingerprint
57 | BS.length bs /= sizeOfFingerprint =
58 throw $ userError "decodeFingerprint: Invalid length"
59 | otherwise = BSI.inlinePerformIO $ do
60 let (fp, offset, _) = BSI.toForeignPtr bs
61 withForeignPtr fp $ \p -> peekByteOff p offset
63-- | Size of a fingerprint
64sizeOfFingerprint :: Int
65sizeOfFingerprint = sizeOf (undefined :: Fingerprint)
67-- | The fingerprint of the typeRep of the argument
68fingerprint :: Typeable a => a -> Fingerprint
69fingerprint a = let TypeRep fp _ _ = typeOf a in fp
71-- | Show fingerprint (for debugging purposes)
72showFingerprint :: Fingerprint -> ShowS
73showFingerprint (Fingerprint hi lo) =
74 showString "(" . showHex hi . showString "," . showHex lo . showString ")"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list