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