Skip to content

Commit e58b6f6

Browse files
committed
Added instances for ForeignPtr
1 parent ea56fb4 commit e58b6f6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.2
2+
---
3+
* Added instances for `ForeignPtr`.
4+
15
1.1.1.1
26
-------
37
* Relaxed upper version bound for `stm`.

StateVar.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: StateVar
2-
version: 1.1.1.1
2+
version: 1.2
33
synopsis: State variables
44
description:
55
This package contains state variables, which are references in the IO monad,

src/Data/StateVar.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
--------------------------------------------------------------------------------
1111
-- |
1212
-- 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
1414
-- License : BSD3
1515
--
1616
-- Maintainer : Sven Panne <[email protected]>
@@ -81,6 +81,7 @@ import Control.Concurrent.STM
8181
import Control.Monad.IO.Class
8282
import Data.IORef
8383
import Data.Typeable
84+
import Foreign.ForeignPtr
8485
import Foreign.Ptr
8586
import Foreign.Storable
8687
#if MIN_VERSION_base(4,12,0)
@@ -195,6 +196,10 @@ instance HasSetter (TVar a) a where
195196
p $= a = liftIO $ atomically $ writeTVar p a
196197
{-# INLINE ($=) #-}
197198

199+
instance Storable a => HasSetter (ForeignPtr a) a where
200+
p $= a = liftIO $ withForeignPtr p ($= a)
201+
{-# INLINE ($=) #-}
202+
198203
--------------------------------------------------------------------
199204
-- * HasUpdate
200205
--------------------------------------------------------------------
@@ -252,6 +257,10 @@ instance HasUpdate (TVar a) a a where
252257
a <- readTVar r
253258
writeTVar r $! f a
254259

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+
255264
--------------------------------------------------------------------
256265
-- * HasGetter
257266
--------------------------------------------------------------------
@@ -283,3 +292,8 @@ instance Storable a => HasGetter (Ptr a) a where
283292
instance HasGetter (IORef a) a where
284293
get = liftIO . readIORef
285294
{-# 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

Comments
 (0)