git.iko.soy/typeable/postgresql-simple
on master help sign in
Add FromField instances for IORefs and MVars
dc6932aLeon P Smith
src/Database/PostgreSQL/Simple/FromField.hs+15
@@ -95,12 +95,14 @@ module Database.PostgreSQL.Simple.FromField
95
95
96import Control.Applicative
96import Control.Applicative
97 ( Applicative, (<|>), (<$>), pure )
97 ( Applicative, (<|>), (<$>), pure )
98+import Control.Concurrent.MVar (MVar, newMVar)
98import Control.Exception (Exception)
99import Control.Exception (Exception)
99import qualified Data.Aeson as JSON
100import qualified Data.Aeson as JSON
100import Data.Attoparsec.Char8 hiding (Result)
101import Data.Attoparsec.Char8 hiding (Result)
101import Data.ByteString (ByteString)
102import Data.ByteString (ByteString)
102import qualified Data.ByteString.Char8 as B
103import qualified Data.ByteString.Char8 as B
103import Data.Int (Int16, Int32, Int64)
104import Data.Int (Int16, Int32, Int64)
105+import Data.IORef (IORef, newIORef)
104import Data.Ratio (Ratio)
106import Data.Ratio (Ratio)
105import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
107import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
106import Data.Typeable (Typeable, typeOf)
108import Data.Typeable (Typeable, typeOf)
@@ -476,6 +478,19 @@ fromJSONField f mbBs = do
476 "JSON decoding error: " ++ err
478 "JSON decoding error: " ++ err
477 JSON.Success x -> pure x
479 JSON.Success x -> pure x
478
480
481+
482+-- | Compatible with the same set of types as @a@. Note that
483+-- modifying the 'IORef' does not have any effects outside
484+-- the local process on the local machine.
485+instance FromField a => FromField (IORef a) where
486+ fromField f v = liftConversion . newIORef =<< fromField f v
487+
488+-- | Compatible with the same set of types as @a@. Note that
489+-- modifying the 'MVar' does not have any effects outside
490+-- the local process on the local machine.
491+instance FromField a => FromField (MVar a) where
492+ fromField f v = liftConversion . newMVar =<< fromField f v
493+
479type Compat = PQ.Oid -> Bool
494type Compat = PQ.Oid -> Bool
480
495
481okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
496okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat