git.iko.soy/typeable/postgresql-simple
on master help sign in
Add FromField instances for IORefs and MVars
commitdc6932a6969c729cbc247d669a4fc9d4f1a9387f authorLeon P Smith <leon@melding-monads.com> dateSat 21 Dec 2013, 06:17 (13 years ago) parent8635f88
1 file changed+15
src/Database/PostgreSQL/Simple/FromField.hs+15
src/Database/PostgreSQL/Simple/FromField.hs+15
@@ -95,12 +95,14 @@ module Database.PostgreSQL.Simple.FromField
9595
9696import Control.Applicative
9797 ( Applicative, (<|>), (<$>), pure )
98+import Control.Concurrent.MVar (MVar, newMVar)
9899import Control.Exception (Exception)
99100import qualified Data.Aeson as JSON
100101import Data.Attoparsec.Char8 hiding (Result)
101102import Data.ByteString (ByteString)
102103import qualified Data.ByteString.Char8 as B
103104import Data.Int (Int16, Int32, Int64)
105+import Data.IORef (IORef, newIORef)
104106import Data.Ratio (Ratio)
105107import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
106108import Data.Typeable (Typeable, typeOf)
@@ -476,6 +478,19 @@ fromJSONField f mbBs = do
476478 "JSON decoding error: " ++ err
477479 JSON.Success x -> pure x
478480
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+
479494type Compat = PQ.Oid -> Bool
480495
481496okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat