on master help sign in
Remove phantom parameter from MutableByteArray
5884b1eAdithya Kumar
core/src/Streamly/Internal/Data/Array.hs+1−2
@@ -128,7 +128,6 @@ import Foreign.Ptr (castPtr)
128import Foreign.Storable (Storable)
128import Foreign.Storable (Storable)
129import Streamly.Internal.Data.Unboxed
129import Streamly.Internal.Data.Unboxed
130 ( Unbox
130 ( Unbox
131− , castContents
132 , peekWith
131 , peekWith
133 , sizeOf
132 , sizeOf
134 )
133 )
@@ -507,7 +506,7 @@ castUnsafe ::
507#endif
506#endif
508 Array a -> Array b
507 Array a -> Array b
509castUnsafe (Array contents start end) =
508castUnsafe (Array contents start end) =
510− Array (castContents contents) start end
509+ Array contents start end
511
510
512-- | Cast an @Array a@ into an @Array Word8@.
511-- | Cast an @Array a@ into an @Array Word8@.
513--
512--
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+20−14
@@ -217,13 +217,13 @@ where
217import Control.Monad (when, void)
217import Control.Monad (when, void)
218import Control.Monad.IO.Class (MonadIO(..))
218import Control.Monad.IO.Class (MonadIO(..))
219import Data.Bits (shiftR, (.|.), (.&.))
219import Data.Bits (shiftR, (.|.), (.&.))
220+import Data.Proxy (Proxy(..))
220import Data.Word (Word8)
221import Data.Word (Word8)
221import Foreign.C.Types (CSize(..), CInt(..))
222import Foreign.C.Types (CSize(..), CInt(..))
222import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
223import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
223import Streamly.Internal.Data.Unboxed
224import Streamly.Internal.Data.Unboxed
224 ( MutableByteArray(..)
225 ( MutableByteArray(..)
225 , Unbox
226 , Unbox
226− , castContents
227 , getMutableByteArray#
227 , getMutableByteArray#
228 , peekWith
228 , peekWith
229 , pokeWith
229 , pokeWith
@@ -336,7 +336,7 @@ data Array a =
336 -- The array is a range into arrContents. arrContents may be a superset of
336 -- The array is a range into arrContents. arrContents may be a superset of
337 -- the slice represented by the array. All offsets are in bytes.
337 -- the slice represented by the array. All offsets are in bytes.
338 Array
338 Array
339− { arrContents :: {-# UNPACK #-} !(MutableByteArray a)
339+ { arrContents :: {-# UNPACK #-} !MutableByteArray
340 , arrStart :: {-# UNPACK #-} !Int -- ^ index into arrContents
340 , arrStart :: {-# UNPACK #-} !Int -- ^ index into arrContents
341 , arrEnd :: {-# UNPACK #-} !Int -- ^ index into arrContents
341 , arrEnd :: {-# UNPACK #-} !Int -- ^ index into arrContents
342 -- Represents the first invalid index of
342 -- Represents the first invalid index of
@@ -388,7 +388,7 @@ unpin arr@Array{..} = do
388-- /Pre-release/
388-- /Pre-release/
389{-# INLINE newArrayWith #-}
389{-# INLINE newArrayWith #-}
390newArrayWith :: forall m a. (MonadIO m, Unbox a)
390newArrayWith :: forall m a. (MonadIO m, Unbox a)
391− => (Int -> Int -> m (MutableByteArray a)) -> Int -> Int -> m (Array a)
391+ => (Int -> Int -> m MutableByteArray) -> Int -> Int -> m (Array a)
392newArrayWith alloc alignSize count = do
392newArrayWith alloc alignSize count = do
393 let size = max (count * SIZE_OF(a)) 0
393 let size = max (count * SIZE_OF(a)) 0
394 contents <- alloc size alignSize
394 contents <- alloc size alignSize
@@ -581,12 +581,18 @@ modify Array{..} f = liftIO $
581 go (INDEX_NEXT(i,a))
581 go (INDEX_NEXT(i,a))
582
582
583{-# INLINE swapArrayByteIndices #-}
583{-# INLINE swapArrayByteIndices #-}
584−swapArrayByteIndices :: Unbox a => MutableByteArray a -> Int -> Int -> IO ()
584+swapArrayByteIndices ::
585+ forall a. Unbox a
586+ => Proxy a
587+ -> MutableByteArray
588+ -> Int
589+ -> Int
590+ -> IO ()
585−swapArrayByteIndices arrContents i1 i2 = do
591+swapArrayByteIndices _ arrContents i1 i2 = do
586 r1 <- peekWith arrContents i1
592 r1 <- peekWith arrContents i1
587 r2 <- peekWith arrContents i2
593 r2 <- peekWith arrContents i2
588− pokeWith arrContents i1 r2
594+ pokeWith arrContents i1 (r2 :: a)
589− pokeWith arrContents i2 r1
595+ pokeWith arrContents i2 (r1 :: a)
590
596
591-- | Swap the elements at two indices without validating the indices.
597-- | Swap the elements at two indices without validating the indices.
592--
598--
@@ -599,7 +605,7 @@ unsafeSwapIndices :: forall m a. (MonadIO m, Unbox a)
599unsafeSwapIndices i1 i2 Array{..} = liftIO $ do
605unsafeSwapIndices i1 i2 Array{..} = liftIO $ do
600 let t1 = INDEX_OF(arrStart,i1,a)
606 let t1 = INDEX_OF(arrStart,i1,a)
601 t2 = INDEX_OF(arrStart,i2,a)
607 t2 = INDEX_OF(arrStart,i2,a)
602− swapArrayByteIndices arrContents t1 t2
608+ swapArrayByteIndices (Proxy :: Proxy a) arrContents t1 t2
603
609
604-- | Swap the elements at two indices.
610-- | Swap the elements at two indices.
605--
611--
@@ -613,7 +619,7 @@ swapIndices i1 i2 Array{..} = liftIO $ do
613 $ invalidIndex "swapIndices" i1
619 $ invalidIndex "swapIndices" i1
614 when (i2 < 0 || INDEX_INVALID(t2,arrEnd,a))
620 when (i2 < 0 || INDEX_INVALID(t2,arrEnd,a))
615 $ invalidIndex "swapIndices" i2
621 $ invalidIndex "swapIndices" i2
616− swapArrayByteIndices arrContents t1 t2
622+ swapArrayByteIndices (Proxy :: Proxy a) arrContents t1 t2
617
623
618-------------------------------------------------------------------------------
624-------------------------------------------------------------------------------
619-- Rounding
625-- Rounding
@@ -1101,7 +1107,7 @@ reverse Array{..} = liftIO $ do
1101
1107
1102 swap l h = do
1108 swap l h = do
1103 when (l < h) $ do
1109 when (l < h) $ do
1104− swapArrayByteIndices arrContents l h
1110+ swapArrayByteIndices (Proxy :: Proxy a) arrContents l h
1105 swap (INDEX_NEXT(l,a)) (INDEX_PREV(h,a))
1111 swap (INDEX_NEXT(l,a)) (INDEX_PREV(h,a))
1106
1112
1107-- | Generate the next permutation of the sequence, returns False if this is
1113-- | Generate the next permutation of the sequence, returns False if this is
@@ -1302,7 +1308,7 @@ arraysOf n (D.Stream step state) =
1302 error $ "Streamly.Internal.Data.Array.Mut.Type.arraysOf: "
1308 error $ "Streamly.Internal.Data.Array.Mut.Type.arraysOf: "
1303 ++ "the size of arrays [" ++ show n
1309 ++ "the size of arrays [" ++ show n
1304 ++ "] must be a natural number"
1310 ++ "] must be a natural number"
1305− Array contents start end bound <- liftIO $ newPinned n
1311+ (Array contents start end bound :: Array a) <- liftIO $ newPinned n
1306 return $ D.Skip (GroupBuffer st contents start end bound)
1312 return $ D.Skip (GroupBuffer st contents start end bound)
1307
1313
1308 step' gst (GroupBuffer st contents start end bound) = do
1314 step' gst (GroupBuffer st contents start end bound) = do
@@ -1410,7 +1416,7 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1410-------------------------------------------------------------------------------
1416-------------------------------------------------------------------------------
1411
1417
1412data ArrayUnsafe a = ArrayUnsafe
1418data ArrayUnsafe a = ArrayUnsafe
1413− {-# UNPACK #-} !(MutableByteArray a) -- contents
1419+ {-# UNPACK #-} !MutableByteArray -- contents
1414 {-# UNPACK #-} !Int -- index 1
1420 {-# UNPACK #-} !Int -- index 1
1415 {-# UNPACK #-} !Int -- index 2
1421 {-# UNPACK #-} !Int -- index 2
1416
1422
@@ -1909,7 +1915,7 @@ fromStreamDN :: forall m a. (MonadIO m, Unbox a)
1909 => Int -> D.Stream m a -> m (Array a)
1915 => Int -> D.Stream m a -> m (Array a)
1910-- fromStreamDN n = D.fold (writeN n)
1916-- fromStreamDN n = D.fold (writeN n)
1911fromStreamDN limit str = do
1917fromStreamDN limit str = do
1912− arr <- liftIO $ newPinned limit
1918+ (arr :: Array a) <- liftIO $ newPinned limit
1913 end <- D.foldlM' (fwrite (arrContents arr)) (return $ arrEnd arr) $ D.take limit str
1919 end <- D.foldlM' (fwrite (arrContents arr)) (return $ arrEnd arr) $ D.take limit str
1914 return $ arr {arrEnd = end}
1920 return $ arr {arrEnd = end}
1915
1921
@@ -2173,7 +2179,7 @@ castUnsafe ::
2173#endif
2179#endif
2174 Array a -> Array b
2180 Array a -> Array b
2175castUnsafe (Array contents start end bound) =
2181castUnsafe (Array contents start end bound) =
2176− Array (castContents contents) start end bound
2182+ Array contents start end bound
2177
2183
2178-- | Cast an @Array a@ into an @Array Word8@.
2184-- | Cast an @Array a@ into an @Array Word8@.
2179--
2185--
core/src/Streamly/Internal/Data/Array/Type.hs+1−1
@@ -131,7 +131,7 @@ data Array a =
131#endif
131#endif
132 -- All offsets are in terms of bytes from the start of arraycontents
132 -- All offsets are in terms of bytes from the start of arraycontents
133 Array
133 Array
134− { arrContents :: {-# UNPACK #-} !(MutableByteArray a)
134+ { arrContents :: {-# UNPACK #-} !MutableByteArray
135 , arrStart :: {-# UNPACK #-} !Int -- offset
135 , arrStart :: {-# UNPACK #-} !Int -- offset
136 , arrEnd :: {-# UNPACK #-} !Int -- offset + len
136 , arrEnd :: {-# UNPACK #-} !Int -- offset + len
137 }
137 }
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+1−1
@@ -51,7 +51,7 @@ import Streamly.Internal.Data.Unboxed
51import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
51import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
52
52
53-- | An 'IORef' holds a single 'Unbox'-able value.
53-- | An 'IORef' holds a single 'Unbox'-able value.
54−newtype IORef a = IORef (MutableByteArray a)
54+newtype IORef a = IORef MutableByteArray
55
55
56-- | Create a new 'IORef'.
56-- | Create a new 'IORef'.
57--
57--
core/src/Streamly/Internal/Data/Ring/Unboxed.hs+5−10
@@ -79,12 +79,7 @@ import Foreign.Storable
79import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, touchForeignPtr)
79import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, touchForeignPtr)
80import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
80import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
81import Foreign.Ptr (plusPtr, minusPtr, castPtr)
81import Foreign.Ptr (plusPtr, minusPtr, castPtr)
82−import Streamly.Internal.Data.Unboxed as Unboxed
82+import Streamly.Internal.Data.Unboxed as Unboxed (Unbox, peekWith)
83− ( MutableByteArray
84− , Unbox
85− , castContents
86− , peekWith
87− )
88import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
83import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
89import GHC.Ptr (Ptr(..))
84import GHC.Ptr (Ptr(..))
90import Streamly.Internal.Data.Array.Mut.Type (Array)
85import Streamly.Internal.Data.Array.Mut.Type (Array)
@@ -412,10 +407,10 @@ unsafeEqArrayN Ring{..} rh A.Array{..} nBytes
412
407
413 where
408 where
414
409
415− w8Contents = castContents arrContents :: MutableByteArray Word8
410+ w8Contents = arrContents
416
411
417 check p i = do
412 check p i = do
418− relem <- peek p
413+ (relem :: Word8) <- peek p
419 aelem <- peekWith w8Contents i
414 aelem <- peekWith w8Contents i
420 if relem == aelem
415 if relem == aelem
421 then go (p `plusPtr` 1) (i + 1)
416 then go (p `plusPtr` 1) (i + 1)
@@ -446,10 +441,10 @@ unsafeEqArray Ring{..} rh A.Array{..} =
446
441
447 where
442 where
448
443
449− w8Contents = castContents arrContents :: MutableByteArray Word8
444+ w8Contents = arrContents
450
445
451 check p i = do
446 check p i = do
452− relem <- peek p
447+ (relem :: Word8) <- peek p
453 aelem <- peekWith w8Contents i
448 aelem <- peekWith w8Contents i
454 if relem == aelem
449 if relem == aelem
455 then go (p `plusPtr` 1) (i + 1)
450 then go (p `plusPtr` 1) (i + 1)
core/src/Streamly/Internal/Data/Stream/Chunked.hs+1−1
@@ -171,7 +171,7 @@ interposeSuffix x = S.interposeSuffix x A.reader
171
171
172data FlattenState s a =
172data FlattenState s a =
173 OuterLoop s
173 OuterLoop s
174− | InnerLoop s !(MA.MutableByteArray a) !Int !Int
174+ | InnerLoop s !MA.MutableByteArray !Int !Int
175
175
176-- XXX Remove monadIO constraint
176-- XXX Remove monadIO constraint
177{-# INLINE_NORMAL unlines #-}
177{-# INLINE_NORMAL unlines #-}
core/src/Streamly/Internal/Data/Unboxed.hs+42−54
@@ -10,7 +10,6 @@ module Streamly.Internal.Data.Unboxed
10 , peekWith
10 , peekWith
11 , pokeWith
11 , pokeWith
12 , MutableByteArray(..)
12 , MutableByteArray(..)
13− , castContents
14 , touch
13 , touch
15 , getMutableByteArray#
14 , getMutableByteArray#
16 , pin
15 , pin
@@ -72,24 +71,20 @@ import Prelude hiding (read)
72--------------------------------------------------------------------------------
71--------------------------------------------------------------------------------
73
72
74-- XXX can use UnliftedNewtypes
73-- XXX can use UnliftedNewtypes
75−data MutableByteArray a = MutableByteArray (MutableByteArray# RealWorld)
74+data MutableByteArray = MutableByteArray (MutableByteArray# RealWorld)
76
75
77{-# INLINE getMutableByteArray# #-}
76{-# INLINE getMutableByteArray# #-}
78−getMutableByteArray# :: MutableByteArray a -> MutableByteArray# RealWorld
77+getMutableByteArray# :: MutableByteArray -> MutableByteArray# RealWorld
79getMutableByteArray# (MutableByteArray mbarr) = mbarr
78getMutableByteArray# (MutableByteArray mbarr) = mbarr
80
79
81−{-# INLINE castContents #-}
82−castContents :: MutableByteArray a -> MutableByteArray b
83−castContents (MutableByteArray mbarr) = MutableByteArray mbarr
84−
85{-# INLINE touch #-}
80{-# INLINE touch #-}
86−touch :: MutableByteArray a -> IO ()
81+touch :: MutableByteArray -> IO ()
87touch (MutableByteArray contents) =
82touch (MutableByteArray contents) =
88 IO $ \s -> case touch# contents s of s' -> (# s', () #)
83 IO $ \s -> case touch# contents s of s' -> (# s', () #)
89
84
90-- | Return the size of the array in bytes.
85-- | Return the size of the array in bytes.
91{-# INLINE sizeOfMutableByteArray #-}
86{-# INLINE sizeOfMutableByteArray #-}
92−sizeOfMutableByteArray :: MutableByteArray a -> IO Int
87+sizeOfMutableByteArray :: MutableByteArray -> IO Int
93sizeOfMutableByteArray (MutableByteArray arr) =
88sizeOfMutableByteArray (MutableByteArray arr) =
94 IO $ \s ->
89 IO $ \s ->
95 case getSizeofMutableByteArray# arr s of
90 case getSizeofMutableByteArray# arr s of
@@ -100,11 +95,11 @@ sizeOfMutableByteArray (MutableByteArray arr) =
100--------------------------------------------------------------------------------
95--------------------------------------------------------------------------------
101
96
102{-# NOINLINE nil #-}
97{-# NOINLINE nil #-}
103−nil :: MutableByteArray a
98+nil :: MutableByteArray
104nil = unsafePerformIO $ newUnpinnedBytes 0
99nil = unsafePerformIO $ newUnpinnedBytes 0
105
100
106{-# INLINE newUnpinnedBytes #-}
101{-# INLINE newUnpinnedBytes #-}
107−newUnpinnedBytes :: Int -> IO (MutableByteArray a)
102+newUnpinnedBytes :: Int -> IO MutableByteArray
108newUnpinnedBytes nbytes | nbytes < 0 =
103newUnpinnedBytes nbytes | nbytes < 0 =
109 errorWithoutStackTrace "newUnpinnedBytes: size must be >= 0"
104 errorWithoutStackTrace "newUnpinnedBytes: size must be >= 0"
110newUnpinnedBytes (I# nbytes) = IO $ \s ->
105newUnpinnedBytes (I# nbytes) = IO $ \s ->
@@ -114,7 +109,7 @@ newUnpinnedBytes (I# nbytes) = IO $ \s ->
114 in (# s', c #)
109 in (# s', c #)
115
110
116{-# INLINE newPinnedBytes #-}
111{-# INLINE newPinnedBytes #-}
117−newPinnedBytes :: Int -> IO (MutableByteArray a)
112+newPinnedBytes :: Int -> IO MutableByteArray
118newPinnedBytes nbytes | nbytes < 0 =
113newPinnedBytes nbytes | nbytes < 0 =
119 errorWithoutStackTrace "newPinnedBytes: size must be >= 0"
114 errorWithoutStackTrace "newPinnedBytes: size must be >= 0"
120newPinnedBytes (I# nbytes) = IO $ \s ->
115newPinnedBytes (I# nbytes) = IO $ \s ->
@@ -124,7 +119,7 @@ newPinnedBytes (I# nbytes) = IO $ \s ->
124 in (# s', c #)
119 in (# s', c #)
125
120
126{-# INLINE newAlignedPinnedBytes #-}
121{-# INLINE newAlignedPinnedBytes #-}
127−newAlignedPinnedBytes :: Int -> Int -> IO (MutableByteArray a)
122+newAlignedPinnedBytes :: Int -> Int -> IO MutableByteArray
128newAlignedPinnedBytes nbytes _align | nbytes < 0 =
123newAlignedPinnedBytes nbytes _align | nbytes < 0 =
129 errorWithoutStackTrace "newAlignedPinnedBytes: size must be >= 0"
124 errorWithoutStackTrace "newAlignedPinnedBytes: size must be >= 0"
130newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
125newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
@@ -138,7 +133,7 @@ newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
138-------------------------------------------------------------------------------
133-------------------------------------------------------------------------------
139
134
140{-# INLINE isPinned #-}
135{-# INLINE isPinned #-}
141−isPinned :: MutableByteArray a -> Bool
136+isPinned :: MutableByteArray -> Bool
142isPinned (MutableByteArray arr#) =
137isPinned (MutableByteArray arr#) =
143 let pinnedInt = I# (isMutableByteArrayPinned# arr#)
138 let pinnedInt = I# (isMutableByteArrayPinned# arr#)
144 in pinnedInt == 1
139 in pinnedInt == 1
@@ -160,7 +155,7 @@ cloneMutableArrayWith# alloc# arr# s# =
160 s3# -> (# s3#, arr1# #)
155 s3# -> (# s3#, arr1# #)
161
156
162{-# INLINE pin #-}
157{-# INLINE pin #-}
163−pin :: MutableByteArray a -> IO (MutableByteArray a)
158+pin :: MutableByteArray -> IO MutableByteArray
164pin arr@(MutableByteArray marr#) =
159pin arr@(MutableByteArray marr#) =
165 if isPinned arr
160 if isPinned arr
166 then return arr
161 then return arr
@@ -170,7 +165,7 @@ pin arr@(MutableByteArray marr#) =
170 (# s1#, marr1# #) -> (# s1#, MutableByteArray marr1# #)
165 (# s1#, marr1# #) -> (# s1#, MutableByteArray marr1# #)
171
166
172{-# INLINE unpin #-}
167{-# INLINE unpin #-}
173−unpin :: MutableByteArray a -> IO (MutableByteArray a)
168+unpin :: MutableByteArray -> IO MutableByteArray
174unpin arr@(MutableByteArray marr#) =
169unpin arr@(MutableByteArray marr#) =
175 if not (isPinned arr)
170 if not (isPinned arr)
176 then return arr
171 then return arr
@@ -185,8 +180,6 @@ unpin arr@(MutableByteArray marr#) =
185
180
186-- XXX Use Proxy type in sizeOf method.
181-- XXX Use Proxy type in sizeOf method.
187-- XXX generate error if the size is < 1
182-- XXX generate error if the size is < 1
188−-- XXX Remove the phantom parameter from MutableByteArray?
189−-- XXX Rename/remove castContents
190
183
191-- In theory we could convert a type to and from a byte stream and use that
184-- In theory we could convert a type to and from a byte stream and use that
192-- to implement boxing, unboxing. But that would be inefficient. This type
185-- to implement boxing, unboxing. But that would be inefficient. This type
@@ -250,19 +243,16 @@ unpin arr@(MutableByteArray marr#) =
250--
243--
251-- If you want to write the instance manually:
244-- If you want to write the instance manually:
252--
245--
253−-- >>> import Streamly.Internal.Data.Unboxed (castContents)
254-- >>> :{
246-- >>> :{
255-- instance Unbox Object where
247-- instance Unbox Object where
256-- sizeOf _ = 16
248-- sizeOf _ = 16
257-- peekByteIndex i arr = do
249-- peekByteIndex i arr = do
258−-- let p = castContents arr
259−-- x0 <- peekByteIndex i p
250+-- x0 <- peekByteIndex i arr
260−-- x1 <- peekByteIndex (i + 8) p
251+-- x1 <- peekByteIndex (i + 8) arr
261-- return $ Object x0 x1
252-- return $ Object x0 x1
262-- pokeByteIndex i arr (Object x0 x1) = do
253-- pokeByteIndex i arr (Object x0 x1) = do
263−-- let p = castContents arr
264−-- pokeByteIndex i p x0
254+-- pokeByteIndex i arr x0
265−-- pokeByteIndex (i + 8) p x1
255+-- pokeByteIndex (i + 8) arr x1
266-- :}
256-- :}
267--
257--
268class Unbox a where
258class Unbox a where
@@ -277,10 +267,10 @@ class Unbox a where
277 --
267 --
278 -- IMPORTANT: The implementation of this interface may not check the bounds
268 -- IMPORTANT: The implementation of this interface may not check the bounds
279 -- of the array, the caller must not assume that.
269 -- of the array, the caller must not assume that.
280− peekByteIndex :: Int -> MutableByteArray a -> IO a
270+ peekByteIndex :: Int -> MutableByteArray -> IO a
281
271
282 default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
272 default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
283− Int -> MutableByteArray a -> IO a
273+ Int -> MutableByteArray -> IO a
284 peekByteIndex i arr = genericPeekByteIndex arr i
274 peekByteIndex i arr = genericPeekByteIndex arr i
285
275
286 -- | Write an element of type "a" to a MutableByteArray given the byte
276 -- | Write an element of type "a" to a MutableByteArray given the byte
@@ -288,10 +278,10 @@ class Unbox a where
288 --
278 --
289 -- IMPORTANT: The implementation of this interface may not check the bounds
279 -- IMPORTANT: The implementation of this interface may not check the bounds
290 -- of the array, the caller must not assume that.
280 -- of the array, the caller must not assume that.
291− pokeByteIndex :: Int -> MutableByteArray a -> a -> IO ()
281+ pokeByteIndex :: Int -> MutableByteArray -> a -> IO ()
292
282
293 default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
283 default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
294− Int -> MutableByteArray a -> a -> IO ()
284+ Int -> MutableByteArray -> a -> IO ()
295 pokeByteIndex i arr = genericPokeByteIndex arr i
285 pokeByteIndex i arr = genericPokeByteIndex arr i
296
286
297#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
287#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
@@ -310,9 +300,9 @@ instance Unbox _type where { \
310#define DERIVE_WRAPPED_UNBOX(_constraint, _type, _constructor, _innerType) \
300#define DERIVE_WRAPPED_UNBOX(_constraint, _type, _constructor, _innerType) \
311instance _constraint Unbox _type where \
301instance _constraint Unbox _type where \
312; {-# INLINE peekByteIndex #-} \
302; {-# INLINE peekByteIndex #-} \
313−; peekByteIndex i arr = _constructor <$> peekByteIndex i (castContents arr) \
303+; peekByteIndex i arr = _constructor <$> peekByteIndex i arr \
314; {-# INLINE pokeByteIndex #-} \
304; {-# INLINE pokeByteIndex #-} \
315−; pokeByteIndex i arr (_constructor a) = pokeByteIndex i (castContents arr) a \
305+; pokeByteIndex i arr (_constructor a) = pokeByteIndex i arr a \
316; {-# INLINE sizeOf #-} \
306; {-# INLINE sizeOf #-} \
317; sizeOf _ = SIZE_OF(_innerType)
307; sizeOf _ = SIZE_OF(_innerType)
318
308
@@ -320,15 +310,13 @@ instance _constraint Unbox _type where \
320instance _constraint Unbox _type where { \
310instance _constraint Unbox _type where { \
321; {-# INLINE peekByteIndex #-} \
311; {-# INLINE peekByteIndex #-} \
322; peekByteIndex i arr = \
312; peekByteIndex i arr = \
323− let contents = castContents arr :: MutableByteArray _innerType \
324− in peekByteIndex i contents >>= \
313+ peekByteIndex i arr >>= \
325− (\p1 -> peekByteIndex (i + SIZE_OF(_innerType)) contents \
314+ (\p1 -> peekByteIndex (i + SIZE_OF(_innerType)) arr \
326− <&> _constructor p1) \
315+ <&> _constructor p1) \
327; {-# INLINE pokeByteIndex #-} \
316; {-# INLINE pokeByteIndex #-} \
328; pokeByteIndex i arr (_constructor p1 p2) = \
317; pokeByteIndex i arr (_constructor p1 p2) = \
329− let contents = castContents arr :: MutableByteArray _innerType \
330− in pokeByteIndex i contents p1 >> \
318+ pokeByteIndex i arr p1 >> \
331− pokeByteIndex (i + SIZE_OF(_innerType)) contents p2 \
319+ pokeByteIndex (i + SIZE_OF(_innerType)) arr p2 \
332; {-# INLINE sizeOf #-} \
320; {-# INLINE sizeOf #-} \
333; sizeOf _ = 2 * SIZE_OF(_innerType) \
321; sizeOf _ = 2 * SIZE_OF(_innerType) \
334}
322}
@@ -455,10 +443,10 @@ instance Unbox () where
455instance Unbox IoSubSystem where
443instance Unbox IoSubSystem where
456
444
457 {-# INLINE peekByteIndex #-}
445 {-# INLINE peekByteIndex #-}
458− peekByteIndex i arr = toEnum <$> peekByteIndex i (castContents arr)
446+ peekByteIndex i arr = toEnum <$> peekByteIndex i arr
459
447
460 {-# INLINE pokeByteIndex #-}
448 {-# INLINE pokeByteIndex #-}
461− pokeByteIndex i arr a = pokeByteIndex i (castContents arr) (fromEnum a)
449+ pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
462
450
463 {-# INLINE sizeOf #-}
451 {-# INLINE sizeOf #-}
464 sizeOf = sizeOf . fromEnum
452 sizeOf = sizeOf . fromEnum
@@ -468,14 +456,14 @@ instance Unbox Bool where
468
456
469 {-# INLINE peekByteIndex #-}
457 {-# INLINE peekByteIndex #-}
470 peekByteIndex i arr = do
458 peekByteIndex i arr = do
471− res <- peekByteIndex i (castContents arr)
459+ res <- peekByteIndex i arr
472 return $ res /= (0 :: Int8)
460 return $ res /= (0 :: Int8)
473
461
474 {-# INLINE pokeByteIndex #-}
462 {-# INLINE pokeByteIndex #-}
475 pokeByteIndex i arr a =
463 pokeByteIndex i arr a =
476 if a
464 if a
477− then pokeByteIndex i (castContents arr) (1 :: Int8)
465+ then pokeByteIndex i arr (1 :: Int8)
478− else pokeByteIndex i (castContents arr) (0 :: Int8)
466+ else pokeByteIndex i arr (0 :: Int8)
479
467
480 {-# INLINE sizeOf #-}
468 {-# INLINE sizeOf #-}
481 sizeOf _ = 1
469 sizeOf _ = 1
@@ -485,11 +473,11 @@ instance Unbox Bool where
485--------------------------------------------------------------------------------
473--------------------------------------------------------------------------------
486
474
487{-# INLINE peekWith #-}
475{-# INLINE peekWith #-}
488−peekWith :: Unbox a => MutableByteArray a -> Int -> IO a
476+peekWith :: Unbox a => MutableByteArray -> Int -> IO a
489peekWith arr i = peekByteIndex i arr
477peekWith arr i = peekByteIndex i arr
490
478
491{-# INLINE pokeWith #-}
479{-# INLINE pokeWith #-}
492−pokeWith :: Unbox a => MutableByteArray a -> Int -> a -> IO ()
480+pokeWith :: Unbox a => MutableByteArray -> Int -> a -> IO ()
493pokeWith arr i = pokeByteIndex i arr
481pokeWith arr i = pokeByteIndex i arr
494
482
495--------------------------------------------------------------------------------
483--------------------------------------------------------------------------------
@@ -502,7 +490,7 @@ pokeWith arr i = pokeByteIndex i arr
502-- cheaper to just get the bound using the size of the array whenever needed?
490-- cheaper to just get the bound using the size of the array whenever needed?
503data BoundedPtr =
491data BoundedPtr =
504 BoundedPtr
492 BoundedPtr
505− (MutableByteArray Word8) -- byte array
493+ MutableByteArray -- byte array
506 Int -- current pos
494 Int -- current pos
507 Int -- position after end
495 Int -- position after end
508
496
@@ -524,7 +512,7 @@ readUnsafe = Peeker (Builder step)
524 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
512 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
525 step (BoundedPtr arr pos end) = do
513 step (BoundedPtr arr pos end) = do
526 let next = pos + sizeOf (undefined :: a)
514 let next = pos + sizeOf (undefined :: a)
527− r <- peekByteIndex pos (castContents arr)
515+ r <- peekByteIndex pos arr
528 return (BoundedPtr arr next end, r)
516 return (BoundedPtr arr next end, r)
529
517
530{-# INLINE read #-}
518{-# INLINE read #-}
@@ -538,7 +526,7 @@ read = Peeker (Builder step)
538 step (BoundedPtr arr pos end) = do
526 step (BoundedPtr arr pos end) = do
539 let next = pos + sizeOf (undefined :: a)
527 let next = pos + sizeOf (undefined :: a)
540 when (next > end) $ error "peekObject reading beyond limit"
528 when (next > end) $ error "peekObject reading beyond limit"
541− r <- peekByteIndex pos (castContents arr)
529+ r <- peekByteIndex pos arr
542 return (BoundedPtr arr next end, r)
530 return (BoundedPtr arr next end, r)
543
531
544{-# INLINE skipByte #-}
532{-# INLINE skipByte #-}
@@ -572,7 +560,7 @@ runPeeker (Peeker (Builder f)) ptr = fmap snd (f ptr)
572pokeBoundedPtrUnsafe :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
560pokeBoundedPtrUnsafe :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
573pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
561pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
574 let next = pos + sizeOf a
562 let next = pos + sizeOf a
575− pokeByteIndex pos (castContents arr) a
563+ pokeByteIndex pos arr a
576 return (BoundedPtr arr next end)
564 return (BoundedPtr arr next end)
577
565
578{-# INLINE pokeBoundedPtr #-}
566{-# INLINE pokeBoundedPtr #-}
@@ -580,7 +568,7 @@ pokeBoundedPtr :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
580pokeBoundedPtr a (BoundedPtr arr pos end) = do
568pokeBoundedPtr a (BoundedPtr arr pos end) = do
581 let next = pos + sizeOf a
569 let next = pos + sizeOf a
582 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
570 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
583− pokeByteIndex pos (castContents arr) a
571+ pokeByteIndex pos arr a
584 return (BoundedPtr arr next end)
572 return (BoundedPtr arr next end)
585
573
586--------------------------------------------------------------------------------
574--------------------------------------------------------------------------------
@@ -758,11 +746,11 @@ genericPokeObj a ptr = void $ genericPokeObject a ptr
758
746
759{-# INLINE genericPokeByteIndex #-}
747{-# INLINE genericPokeByteIndex #-}
760genericPokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
748genericPokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
761− MutableByteArray a -> Int -> a -> IO ()
749+ MutableByteArray -> Int -> a -> IO ()
762genericPokeByteIndex arr index x = do
750genericPokeByteIndex arr index x = do
763 -- XXX Should we use unsafe poke?
751 -- XXX Should we use unsafe poke?
764 end <- sizeOfMutableByteArray arr
752 end <- sizeOfMutableByteArray arr
765− genericPokeObj x (BoundedPtr (castContents arr) index end)
753+ genericPokeObj x (BoundedPtr arr index end)
766
754
767--------------------------------------------------------------------------------
755--------------------------------------------------------------------------------
768-- Generic peek
756-- Generic peek
@@ -843,8 +831,8 @@ genericPeekBoundedPtr = runPeeker genericPeeker
843
831
844{-# INLINE genericPeekByteIndex #-}
832{-# INLINE genericPeekByteIndex #-}
845genericPeekByteIndex :: (Generic a, PeekRep (Rep a)) =>
833genericPeekByteIndex :: (Generic a, PeekRep (Rep a)) =>
846− MutableByteArray a -> Int -> IO a
834+ MutableByteArray -> Int -> IO a
847genericPeekByteIndex arr index = do
835genericPeekByteIndex arr index = do
848 -- XXX Should we use unsafe peek?
836 -- XXX Should we use unsafe peek?
849 end <- sizeOfMutableByteArray arr
837 end <- sizeOfMutableByteArray arr
850− genericPeekBoundedPtr (BoundedPtr (castContents arr) index end)
838+ genericPeekBoundedPtr (BoundedPtr arr index end)
core/src/Streamly/Internal/Unicode/Stream.hs+3−3
@@ -686,9 +686,9 @@ decodeUtf8Lax = decodeUtf8
686#endif
686#endif
687data FlattenState s a
687data FlattenState s a
688 = OuterLoop s !(Maybe (DecodeState, CodePoint))
688 = OuterLoop s !(Maybe (DecodeState, CodePoint))
689− | InnerLoopDecodeInit s (MutableByteArray a) !Int !Int
689+ | InnerLoopDecodeInit s MutableByteArray !Int !Int
690− | InnerLoopDecodeFirst s (MutableByteArray a) !Int !Int Word8
690+ | InnerLoopDecodeFirst s MutableByteArray !Int !Int Word8
691− | InnerLoopDecoding s (MutableByteArray a) !Int !Int
691+ | InnerLoopDecoding s MutableByteArray !Int !Int
692 !DecodeState !CodePoint
692 !DecodeState !CodePoint
693 | YAndC !Char (FlattenState s a) -- These constructors can be
693 | YAndC !Char (FlattenState s a) -- These constructors can be
694 -- encoded in the UTF8DecodeState
694 -- encoded in the UTF8DecodeState