Skip to content

Commit 9e2bb80

Browse files
committed
Use IO callbacks in Array modules
1 parent d4df8c9 commit 9e2bb80

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

core/docs/Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
See [0.2.2-0.3.0 API Changelog](/core/docs/ApiChangelogs/0.2.2-0.3.0.txt) for a
4040
full list of deprecations, additions, and changes to the function signatures.
4141

42+
### Breaking Changes
43+
44+
* Functions in Array and MutArray that take a callback now explicitly use `IO`
45+
instead of a generalized monad `m`.
46+
4247
## 0.2.2 (Jan 2024)
4348

4449
* Add fixities `infixr 5` for `cons` and `consM` functions.

core/src/Streamly/Internal/Data/Array/Type.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ data Array a =
296296
-- /Pre-release/
297297
--
298298
{-# INLINE unsafePinnedAsPtr #-}
299-
unsafePinnedAsPtr :: MonadIO m => Array a -> (Ptr a -> Int -> m b) -> m b
299+
unsafePinnedAsPtr :: MonadIO m => Array a -> (Ptr a -> Int -> IO b) -> m b
300300
unsafePinnedAsPtr arr f = do
301301
let marr = unsafeThaw arr
302302
pinned <- liftIO $ MA.pin marr
@@ -311,7 +311,7 @@ unsafePinnedAsPtr arr f = do
311311
--
312312
{-# INLINE unsafeAsForeignPtr #-}
313313
unsafeAsForeignPtr
314-
:: MonadIO m => Array a -> (ForeignPtr a -> Int -> m b) -> m b
314+
:: MonadIO m => Array a -> (ForeignPtr a -> Int -> IO b) -> m b
315315
unsafeAsForeignPtr arr0 f = do
316316
let marr = unsafeThaw arr0
317317
pinned <- liftIO $ MA.pin marr
@@ -345,7 +345,7 @@ unsafeFromForeignPtr (ForeignPtr addr# _) len =
345345

346346
{-# DEPRECATED asPtrUnsafe "Please use unsafePinnedAsPtr instead." #-}
347347
{-# INLINE asPtrUnsafe #-}
348-
asPtrUnsafe :: MonadIO m => Array a -> (Ptr a -> m b) -> m b
348+
asPtrUnsafe :: MonadIO m => Array a -> (Ptr a -> IO b) -> m b
349349
asPtrUnsafe arr f = unsafePinnedAsPtr arr (\p _ -> f p)
350350

351351
-------------------------------------------------------------------------------

core/src/Streamly/Internal/Data/MutArray/Type.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ isPinned MutArray{..} = Unboxed.isPinned arrContents
573573
-- /Pre-release/
574574
{-# INLINE emptyWithAligned #-}
575575
newArrayWith, emptyWithAligned :: forall m a. (MonadIO m, Unbox a)
576-
=> (Int -> Int -> m MutByteArray) -> Int -> Int -> m (MutArray a)
577-
emptyWithAligned alloc alignSize count = do
576+
=> (Int -> Int -> IO MutByteArray) -> Int -> Int -> m (MutArray a)
577+
emptyWithAligned alloc alignSize count = liftIO $ do
578578
let size = max (count * SIZE_OF(a)) 0
579579
contents <- alloc size alignSize
580580
return $ MutArray
@@ -2226,6 +2226,7 @@ writeAppend :: forall m a. (MonadIO m, Unbox a) =>
22262226
m (MutArray a) -> Fold m a (MutArray a)
22272227
writeAppend = append
22282228

2229+
-- XXX Use "IO" instead of "m"
22292230
-- XXX We can carry bound as well in the state to make sure we do not lose the
22302231
-- remaining capacity. Need to check perf impact.
22312232
--
@@ -2292,6 +2293,7 @@ pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
22922293
=> Int -> Fold m a (MutArray a)
22932294
pinnedWriteNUnsafe = unsafeCreateOf'
22942295

2296+
-- XXX Use "IO" instead of "m"
22952297
-- | @createWithOf alloc n@ folds a maximum of @n@ elements into an array
22962298
-- allocated using the @alloc@ function.
22972299
--
@@ -3179,14 +3181,14 @@ cast arr =
31793181
-- array after pinning.
31803182
{-# DEPRECATED unsafePinnedAsPtr "Pin the array and then use unsafeAsPtr." #-}
31813183
{-# INLINE unsafePinnedAsPtr #-}
3182-
unsafePinnedAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> m b) -> m b
3184+
unsafePinnedAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> IO b) -> m b
31833185
unsafePinnedAsPtr arr f = do
31843186
arr1 <- liftIO $ pin arr
31853187
unsafeAsPtr arr1 f
31863188

31873189
{-# DEPRECATED asPtrUnsafe "Pin the array and then use unsafeAsPtr." #-}
31883190
{-# INLINE asPtrUnsafe #-}
3189-
asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> m b) -> m b
3191+
asPtrUnsafe :: MonadIO m => MutArray a -> (Ptr a -> IO b) -> m b
31903192
asPtrUnsafe a f = unsafePinnedAsPtr a (\p _ -> f p)
31913193

31923194
-- | @unsafeAsPtr arr f@, f is a function used as @f ptr len@ where @ptr@ is a
@@ -3200,7 +3202,7 @@ asPtrUnsafe a f = unsafePinnedAsPtr a (\p _ -> f p)
32003202
-- /Pre-release/
32013203
--
32023204
{-# INLINE unsafeAsPtr #-}
3203-
unsafeAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> m b) -> m b
3205+
unsafeAsPtr :: MonadIO m => MutArray a -> (Ptr a -> Int -> IO b) -> m b
32043206
unsafeAsPtr arr f =
32053207
Unboxed.unsafeAsPtr
32063208
(arrContents arr)
@@ -3216,7 +3218,7 @@ unsafeAsPtr arr f =
32163218
-- to the total capacity.
32173219
{-# INLINE unsafePinnedCreateUsingPtr #-}
32183220
unsafePinnedCreateUsingPtr
3219-
:: MonadIO m => Int -> (Ptr Word8 -> m Int) -> m (MutArray Word8)
3221+
:: MonadIO m => Int -> (Ptr Word8 -> IO Int) -> m (MutArray Word8)
32203222
unsafePinnedCreateUsingPtr cap pop = do
32213223
(arr :: MutArray Word8) <- emptyOf' cap
32223224
len <- Unboxed.unsafeAsPtr (arrContents arr) pop

core/src/Streamly/Internal/Data/MutByteArray/Type.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ touch (MutByteArray contents) =
113113
-- array after pinning.
114114
{-# DEPRECATED unsafePinnedAsPtr "Pin the array and then use unsafeAsPtr." #-}
115115
{-# INLINE unsafePinnedAsPtr #-}
116-
unsafePinnedAsPtr :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
116+
unsafePinnedAsPtr :: MonadIO m => MutByteArray -> (Ptr a -> IO b) -> m b
117117
unsafePinnedAsPtr arr f = do
118118
arr1 <- liftIO $ pin arr
119119
unsafeAsPtr arr1 f
120120

121121
{-# DEPRECATED asPtrUnsafe "Pin the array and then use unsafeAsPtr." #-}
122122
{-# INLINE asPtrUnsafe #-}
123-
asPtrUnsafe :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
123+
asPtrUnsafe :: MonadIO m => MutByteArray -> (Ptr a -> IO b) -> m b
124124
asPtrUnsafe = unsafePinnedAsPtr
125125

126126
-- | Use a @MutByteArray@ as @Ptr a@. This is useful when we want to pass
@@ -137,8 +137,8 @@ asPtrUnsafe = unsafePinnedAsPtr
137137
-- /Pre-release/
138138
--
139139
{-# INLINE unsafeAsPtr #-}
140-
unsafeAsPtr :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
141-
unsafeAsPtr arr f = do
140+
unsafeAsPtr :: MonadIO m => MutByteArray -> (Ptr a -> IO b) -> m b
141+
unsafeAsPtr arr f = liftIO $ do
142142
when (not (isPinned arr))
143143
$ error "unsafeAsPtr requires the array to be pinned"
144144

@@ -147,7 +147,7 @@ unsafeAsPtr arr f = do
147147
r <- f ptr
148148
-- While f is using the bare pointer, the MutByteArray may be garbage
149149
-- collected by the GC, tell the GC that we are still using it.
150-
liftIO $ touch arr
150+
touch arr
151151
return r
152152

153153
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)