@@ -88,7 +88,7 @@ where
89import Control.Exception (assert)
89import Control.Exception (assert)
90import Control.DeepSeq (NFData(..))
90import Control.DeepSeq (NFData(..))
91−import Control.Monad (when)
91+import Control.Monad (when, void)
92import Control.Monad.IO.Class (MonadIO(..))
92import Control.Monad.IO.Class (MonadIO(..))
93import Data.Functor.Identity (runIdentity)
93import Data.Functor.Identity (runIdentity)
94#if __GLASGOW_HASKELL__ < 808
94#if __GLASGOW_HASKELL__ < 808
@@ -208,7 +208,7 @@ foreign import ccall unsafe "string.h memchr" c_memchr
209-- XXX we are converting Int to CSize
209-- XXX we are converting Int to CSize
210memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
210memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
211−memcpy dst src len = c_memcpy dst src (fromIntegral len) >> return ()
211+memcpy dst src len = void (c_memcpy dst src (fromIntegral len))
213foreign import ccall unsafe "string.h memcmp" c_memcmp
213foreign import ccall unsafe "string.h memcmp" c_memcmp
214 :: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
214 :: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
@@ -293,16 +293,16 @@ unsafeSnoc arr@Array{..} x = do
293 error "BUG: unsafeSnoc: writing beyond array bounds"
293 error "BUG: unsafeSnoc: writing beyond array bounds"
294 poke aEnd x
294 poke aEnd x
295 touchForeignPtr aStart
295 touchForeignPtr aStart
296− return $ arr {aEnd = aEnd `plusPtr` (sizeOf (undefined :: a))}
296+ return $ arr {aEnd = aEnd `plusPtr` sizeOf (undefined :: a)}
298{-# INLINE snoc #-}
298{-# INLINE snoc #-}
299snoc :: forall a. Storable a => Array a -> a -> IO (Array a)
299snoc :: forall a. Storable a => Array a -> a -> IO (Array a)
300−snoc arr@Array {..} x = do
300+snoc arr@Array {..} x =
301− if (aEnd == aBound)
301+ if aEnd == aBound
303 let oldStart = unsafeForeignPtrToPtr aStart
303 let oldStart = unsafeForeignPtrToPtr aStart
304 size = aEnd `minusPtr` oldStart
304 size = aEnd `minusPtr` oldStart
305− newSize = (size + (sizeOf (undefined :: a)))
305+ newSize = size + sizeOf (undefined :: a)
306 newPtr <- Malloc.mallocForeignPtrAlignedBytes
306 newPtr <- Malloc.mallocForeignPtrAlignedBytes
307 newSize (alignment (undefined :: a))
307 newSize (alignment (undefined :: a))
308 withForeignPtr newPtr $ \pNew -> do
308 withForeignPtr newPtr $ \pNew -> do
@@ -317,7 +317,7 @@ snoc arr@Array {..} x = do
318 poke aEnd x
318 poke aEnd x
319 touchForeignPtr aStart
319 touchForeignPtr aStart
320− return $ arr {aEnd = aEnd `plusPtr` (sizeOf (undefined :: a))}
320+ return $ arr {aEnd = aEnd `plusPtr` sizeOf (undefined :: a)}
322-- | Reallocate the array to the specified size in bytes. If the size is less
322-- | Reallocate the array to the specified size in bytes. If the size is less
323-- than the original array the array gets truncated.
323-- than the original array the array gets truncated.
@@ -449,7 +449,7 @@ toStreamD Array{..} =
449 r <- peek p
449 r <- peek p
450 touchForeignPtr aStart
450 touchForeignPtr aStart
452− return $ D.Yield x (p `plusPtr` (sizeOf (undefined :: a)))
452+ return $ D.Yield x (p `plusPtr` sizeOf (undefined :: a))
454{-# INLINE toStreamK #-}
454{-# INLINE toStreamK #-}
455toStreamK :: forall t m a. (K.IsStream t, Storable a) => Array a -> t m a
455toStreamK :: forall t m a. (K.IsStream t, Storable a) => Array a -> t m a
@@ -466,7 +466,7 @@ toStreamK Array{..} =
466 r <- peek p
466 r <- peek p
467 touchForeignPtr aStart
467 touchForeignPtr aStart
469− in x `K.cons` go (p `plusPtr` (sizeOf (undefined :: a)))
469+ in x `K.cons` go (p `plusPtr` sizeOf (undefined :: a))
471{-# INLINE_NORMAL toStreamDRev #-}
471{-# INLINE_NORMAL toStreamDRev #-}
472toStreamDRev :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
472toStreamDRev :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
@@ -584,7 +584,7 @@ writeNUnsafe n = Fold step initial extract
584 return $ ArrayUnsafe start end
584 return $ ArrayUnsafe start end
585 step (ArrayUnsafe start end) x = do
585 step (ArrayUnsafe start end) x = do
586 liftIO $ poke end x
586 liftIO $ poke end x
587− return $ (ArrayUnsafe start (end `plusPtr` sizeOf (undefined :: a)))
587+ return $ ArrayUnsafe start (end `plusPtr` sizeOf (undefined :: a))
588 extract (ArrayUnsafe start end) = return $ Array start end end -- liftIO . shrinkToFit
588 extract (ArrayUnsafe start end) = return $ Array start end end -- liftIO . shrinkToFit
590-- XXX The realloc based implementation needs to make one extra copy if we use
590-- XXX The realloc based implementation needs to make one extra copy if we use
@@ -739,7 +739,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
739 touchForeignPtr startf
739 touchForeignPtr startf
741 return $ D.Yield x (InnerLoop st startf
741 return $ D.Yield x (InnerLoop st startf
742− (p `plusPtr` (sizeOf (undefined :: a))) end)
742+ (p `plusPtr` sizeOf (undefined :: a)) end)
744{-# INLINE_NORMAL flattenArraysRev #-}
744{-# INLINE_NORMAL flattenArraysRev #-}
745flattenArraysRev :: forall m a. (MonadIO m, Storable a)
745flattenArraysRev :: forall m a. (MonadIO m, Storable a)
@@ -811,7 +811,7 @@ toListFB c n Array{..} = go (unsafeForeignPtrToPtr aStart)
811 r <- peek p
811 r <- peek p
812 touchForeignPtr aStart
812 touchForeignPtr aStart
814− in c x (go (p `plusPtr` (sizeOf (undefined :: a))))
814+ in c x (go (p `plusPtr` sizeOf (undefined :: a)))
816-- | Convert an 'Array' into a list.
816-- | Convert an 'Array' into a list.
@@ -842,9 +842,7 @@ fromList xs = unsafePerformIO $ fromStreamD $ D.fromList xs
843instance (Storable a, Read a, Show a) => Read (Array a) where
843instance (Storable a, Read a, Show a) => Read (Array a) where
844 {-# INLINE readPrec #-}
844 {-# INLINE readPrec #-}
845− readPrec = do
845+ readPrec = fromList <$> readPrec
847− return (fromList xs)
848 readListPrec = readListPrecDefault
846 readListPrec = readListPrecDefault
850instance (a ~ Char) => IsString (Array a) where
848instance (a ~ Char) => IsString (Array a) where
@@ -1037,7 +1035,7 @@ unlines sep (D.Stream step state) = D.Stream step' (OuterLoop state)
1037 touchForeignPtr startf
1035 touchForeignPtr startf
1038 return r
1036 return r
1039 return $ D.Yield x (InnerLoop st startf
1037 return $ D.Yield x (InnerLoop st startf
1040− (p `plusPtr` (sizeOf (undefined :: a))) end)
1038+ (p `plusPtr` sizeOf (undefined :: a)) end)
1042-- Splice an array into a pre-reserved mutable array. The user must ensure
1040-- Splice an array into a pre-reserved mutable array. The user must ensure
1043-- that there is enough space in the mutable array.
1041-- that there is enough space in the mutable array.
@@ -1048,7 +1046,7 @@ spliceWith dst@(Array _ end bound) src = liftIO $ do
1048 if end `plusPtr` srcLen > bound
1046 if end `plusPtr` srcLen > bound
1049 then error "Bug: spliceIntoUnsafe: Not enough space in the target array"
1047 then error "Bug: spliceIntoUnsafe: Not enough space in the target array"
1051− withForeignPtr (aStart dst) $ \_ -> do
1049+ withForeignPtr (aStart dst) $ \_ ->
1052 withForeignPtr (aStart src) $ \psrc -> do
1050 withForeignPtr (aStart src) $ \psrc -> do
1053 let pdst = aEnd dst
1051 let pdst = aEnd dst
1054 memcpy (castPtr pdst) (castPtr psrc) srcLen
1052 memcpy (castPtr pdst) (castPtr psrc) srcLen
@@ -1109,7 +1107,7 @@ packArraysChunksOf n (D.Stream step state) =
1109 then D.Skip (SpliceYielding arr (SpliceInitial s))
1107 then D.Skip (SpliceYielding arr (SpliceInitial s))
1110 else D.Skip (SpliceBuffering s arr)
1108 else D.Skip (SpliceBuffering s arr)
1111 D.Skip s -> return $ D.Skip (SpliceInitial s)
1109 D.Skip s -> return $ D.Skip (SpliceInitial s)
1112− D.Stop -> return $ D.Stop
1110+ D.Stop -> return D.Stop
1114 step' gst (SpliceBuffering st buf) = do
1112 step' gst (SpliceBuffering st buf) = do
1115 r <- step gst st
1113 r <- step gst st
@@ -1147,22 +1145,20 @@ lpackArraysChunksOf n (Fold step1 initial1 extract1) =
1147 -- XXX we can pass the module string from the higher level API
1145 -- XXX we can pass the module string from the higher level API
1148 error $ "Streamly.Internal.Memory.Array.Types.packArraysChunksOf: the size of "
1146 error $ "Streamly.Internal.Memory.Array.Types.packArraysChunksOf: the size of "
1149 ++ "arrays [" ++ show n ++ "] must be a natural number"
1147 ++ "arrays [" ++ show n ++ "] must be a natural number"
1151− return (Tuple' Nothing r1)
1148+ Tuple' Nothing <$> initial1
1153 extract (Tuple' Nothing r1) = extract1 r1
1150 extract (Tuple' Nothing r1) = extract1 r1
1154 extract (Tuple' (Just buf) r1) = do
1151 extract (Tuple' (Just buf) r1) = do
1155 r <- step1 r1 buf
1152 r <- step1 r1 buf
1156 extract1 r
1153 extract1 r
1158− step (Tuple' Nothing r1) arr = do
1155+ step (Tuple' Nothing r1) arr =
1159 let len = byteLength arr
1156 let len = byteLength arr
1160 in if len >= n
1157 in if len >= n
1162 r <- step1 r1 arr
1159 r <- step1 r1 arr
1163 extract1 r
1160 extract1 r
1165− return (Tuple' Nothing r1')
1161+ Tuple' Nothing <$> initial1
1166 else return (Tuple' (Just arr) r1)
1162 else return (Tuple' (Just arr) r1)
1168 step (Tuple' (Just buf) r1) arr = do
1164 step (Tuple' (Just buf) r1) arr = do
@@ -1176,8 +1172,7 @@ lpackArraysChunksOf n (Fold step1 initial1 extract1) =
1177 r <- step1 r1 buf''
1173 r <- step1 r1 buf''
1178 extract1 r
1174 extract1 r
1180− return (Tuple' Nothing r1')
1175+ Tuple' Nothing <$> initial1
1181 else return (Tuple' (Just buf'') r1)
1176 else return (Tuple' (Just buf'') r1)
1183#if !defined(mingw32_HOST_OS)
1178#if !defined(mingw32_HOST_OS)
@@ -1222,7 +1217,7 @@ groupIOVecsOf n maxIOVLen (D.Stream step state) =
1222 then return $ D.Skip (GatherYielding iov' (GatherInitial s))
1217 then return $ D.Skip (GatherYielding iov' (GatherInitial s))
1223 else return $ D.Skip (GatherBuffering s iov' len)
1218 else return $ D.Skip (GatherBuffering s iov' len)
1224 D.Skip s -> return $ D.Skip (GatherInitial s)
1219 D.Skip s -> return $ D.Skip (GatherInitial s)
1225− D.Stop -> return $ D.Stop
1220+ D.Stop -> return D.Stop
1227 step' gst (GatherBuffering st iov len) = do
1222 step' gst (GatherBuffering st iov len) = do
1228 r <- step (adaptState gst) st
1223 r <- step (adaptState gst) st
@@ -1330,7 +1325,7 @@ splitOn byte (D.Stream step state) = D.Stream step' (Initial state)
1330 Nothing -> D.Skip (Buffering s arr1)
1325 Nothing -> D.Skip (Buffering s arr1)
1331 Just arr2 -> D.Skip (Yielding arr1 (Splitting s arr2))
1326 Just arr2 -> D.Skip (Yielding arr1 (Splitting s arr2))
1332 D.Skip s -> return $ D.Skip (Initial s)
1327 D.Skip s -> return $ D.Skip (Initial s)
1333− D.Stop -> return $ D.Stop
1328+ D.Stop -> return D.Stop
1335 step' gst (Buffering st buf) = do
1330 step' gst (Buffering st buf) = do
1336 r <- step gst st
1331 r <- step gst st
@@ -1354,4 +1349,4 @@ splitOn byte (D.Stream step state) = D.Stream step' (Initial state)
1354 Just arr2 -> D.Skip $ Yielding arr1 (Splitting st arr2)
1349 Just arr2 -> D.Skip $ Yielding arr1 (Splitting st arr2)
1356 step' _ (Yielding arr next) = return $ D.Yield arr next
1351 step' _ (Yielding arr next) = return $ D.Yield arr next
1357− step' _ Finishing = return $ D.Stop
1352+ step' _ Finishing = return D.Stop