|
1 | 1 | {-# LANGUAGE DefaultSignatures #-}
|
| 2 | +{-# LANGUAGE FlexibleContexts #-} |
2 | 3 | {-# LANGUAGE QuantifiedConstraints #-}
|
3 | 4 | {-# LANGUAGE TypeFamilyDependencies #-}
|
4 |
| -{-# LANGUAGE TypeOperators #-} |
5 | 5 |
|
6 |
| -module Control.Concurrent.Class.MonadMVar (MonadMVar (..)) where |
| 6 | +module Control.Concurrent.Class.MonadMVar |
| 7 | + ( MonadMVar (..) |
| 8 | + , MonadInspectMVar (..) |
| 9 | + ) where |
7 | 10 |
|
8 | 11 | import qualified Control.Concurrent.MVar as IO
|
9 | 12 | import Control.Monad.Class.MonadThrow
|
@@ -127,6 +130,9 @@ class Monad m => MonadMVar m where
|
127 | 130 | return b
|
128 | 131 | {-# INLINE modifyMVarMasked #-}
|
129 | 132 |
|
| 133 | +-- |
| 134 | +-- IO instance |
| 135 | +-- |
130 | 136 |
|
131 | 137 | instance MonadMVar IO where
|
132 | 138 | type MVar IO = IO.MVar
|
@@ -181,8 +187,22 @@ instance ( MonadMask m
|
181 | 187 | modifyMVarMasked (WrappedMVar v) f = ReaderT $ \r ->
|
182 | 188 | modifyMVarMasked v (\a -> runReaderT (f a) r)
|
183 | 189 |
|
| 190 | +-- |
| 191 | +-- MonadInspectMVar |
| 192 | +-- |
184 | 193 |
|
185 |
| - |
| 194 | +-- | This type class is intended for |
| 195 | +-- ['io-sim'](https://hackage.haskell.org/package/io-sim), where one might want |
| 196 | +-- to access an 'MVar' in the underlying 'ST' monad. |
| 197 | +class (MonadMVar m, Monad (InspectMVarMonad m)) => MonadInspectMVar m where |
| 198 | + type InspectMVarMonad m :: Type -> Type |
| 199 | + -- | Return the value of an 'MVar' as an 'InspectMVarMonad' computation. Can |
| 200 | + -- be 'Nothing' if the 'MVar' is empty. |
| 201 | + inspectMVar :: proxy m -> MVar m a -> InspectMVarMonad m (Maybe a) |
| 202 | + |
| 203 | +instance MonadInspectMVar IO where |
| 204 | + type InspectMVarMonad IO = IO |
| 205 | + inspectMVar _ = tryReadMVar |
186 | 206 |
|
187 | 207 | --
|
188 | 208 | -- Utilities
|
|
0 commit comments