|
10 | 10 | --------------------------------------------------------------------------------
|
11 | 11 | -- |
|
12 | 12 | -- Module : Data.StateVar
|
13 |
| --- Copyright : (c) Edward Kmett 2014-2015, Sven Panne 2009-2018 |
| 13 | +-- Copyright : (c) Edward Kmett 2014-2019, Sven Panne 2009-2018 |
14 | 14 | -- License : BSD3
|
15 | 15 | --
|
16 | 16 | -- Maintainer : Sven Panne <[email protected]>
|
@@ -81,6 +81,7 @@ import Control.Concurrent.STM
|
81 | 81 | import Control.Monad.IO.Class
|
82 | 82 | import Data.IORef
|
83 | 83 | import Data.Typeable
|
| 84 | +import Foreign.ForeignPtr |
84 | 85 | import Foreign.Ptr
|
85 | 86 | import Foreign.Storable
|
86 | 87 | #if MIN_VERSION_base(4,12,0)
|
@@ -195,6 +196,10 @@ instance HasSetter (TVar a) a where
|
195 | 196 | p $= a = liftIO $ atomically $ writeTVar p a
|
196 | 197 | {-# INLINE ($=) #-}
|
197 | 198 |
|
| 199 | +instance Storable a => HasSetter (ForeignPtr a) a where |
| 200 | + p $= a = liftIO $ withForeignPtr p ($= a) |
| 201 | + {-# INLINE ($=) #-} |
| 202 | + |
198 | 203 | --------------------------------------------------------------------
|
199 | 204 | -- * HasUpdate
|
200 | 205 | --------------------------------------------------------------------
|
@@ -252,6 +257,10 @@ instance HasUpdate (TVar a) a a where
|
252 | 257 | a <- readTVar r
|
253 | 258 | writeTVar r $! f a
|
254 | 259 |
|
| 260 | +instance Storable a => HasUpdate (ForeignPtr a) a a where |
| 261 | + p $~ f = liftIO $ withForeignPtr p ($~ f) |
| 262 | + p $~! f = liftIO $ withForeignPtr p ($~! f) |
| 263 | + |
255 | 264 | --------------------------------------------------------------------
|
256 | 265 | -- * HasGetter
|
257 | 266 | --------------------------------------------------------------------
|
@@ -283,3 +292,8 @@ instance Storable a => HasGetter (Ptr a) a where
|
283 | 292 | instance HasGetter (IORef a) a where
|
284 | 293 | get = liftIO . readIORef
|
285 | 294 | {-# INLINE get #-}
|
| 295 | + |
| 296 | +instance Storable a => HasGetter (ForeignPtr a) a where |
| 297 | + get p = liftIO $ withForeignPtr p get |
| 298 | + {-# INLINE get #-} |
| 299 | + |
0 commit comments