on 5cc710d help sign in
hakyll/src/Hakyll/Core/CompiledItem.hs
47 lines 1.4 kB history raw
1-- | A module containing a box datatype representing a compiled item. This
2-- item can be of any type, given that a few restrictions hold:
3--
4-- * we need a 'Typeable' instance to perform type-safe casts;
5--
6-- * we need a 'Binary' instance so we can serialize these items to the cache;
7--
8-- * we need a 'Writable' instance so the results can be saved.
9--
10{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}
11module Hakyll.Core.CompiledItem
12 ( CompiledItem (..)
13 , compiledItem
14 , unCompiledItem
15 ) where
17import Data.Binary (Binary)
18import Data.Typeable (Typeable, cast, typeOf)
19import Data.Maybe (fromMaybe)
21import Hakyll.Core.Writable
23-- | Box type for a compiled item
24--
25data CompiledItem = forall a. (Binary a, Typeable a, Writable a)
26 => CompiledItem a
27 deriving (Typeable)
29instance Writable CompiledItem where
30 write p (CompiledItem x) = write p x
32-- | Box a value into a 'CompiledItem'
33--
34compiledItem :: (Binary a, Typeable a, Writable a)
35 => a
36 -> CompiledItem
37compiledItem = CompiledItem
39-- | Unbox a value from a 'CompiledItem'
40--
41unCompiledItem :: (Binary a, Typeable a, Writable a)
42 => CompiledItem
43 -> a
44unCompiledItem (CompiledItem x) = fromMaybe error' $ cast x
45 where
46 error' = error $ "Hakyll.Core.CompiledItem.unCompiledItem: "
47 ++ "unsupported type (got " ++ show (typeOf x) ++ ")"