@@ -10,7 +10,6 @@ module Streamly.Internal.Data.Unboxed
1010 , peekWith
1111 , pokeWith
1212 , MutableByteArray(..)
13− , castContents
1413 , touch
1514 , getMutableByteArray#
1615 , pin
@@ -72,24 +71,20 @@ import Prelude hiding (read)
7271--------------------------------------------------------------------------------
7372
7473-- XXX can use UnliftedNewtypes
75−data MutableByteArray a = MutableByteArray (MutableByteArray# RealWorld)
74+data MutableByteArray = MutableByteArray (MutableByteArray# RealWorld)
7675
7776{-# INLINE getMutableByteArray# #-}
78−getMutableByteArray# :: MutableByteArray a -> MutableByteArray# RealWorld
77+getMutableByteArray# :: MutableByteArray -> MutableByteArray# RealWorld
7978getMutableByteArray# (MutableByteArray mbarr) = mbarr
8079
81−{-# INLINE castContents #-}
82−castContents :: MutableByteArray a -> MutableByteArray b
83−castContents (MutableByteArray mbarr) = MutableByteArray mbarr
84−
8580{-# INLINE touch #-}
86−touch :: MutableByteArray a -> IO ()
81+touch :: MutableByteArray -> IO ()
8782touch (MutableByteArray contents) =
8883 IO $ \s -> case touch# contents s of s' -> (# s', () #)
8984
9085-- | Return the size of the array in bytes.
9186{-# INLINE sizeOfMutableByteArray #-}
92−sizeOfMutableByteArray :: MutableByteArray a -> IO Int
87+sizeOfMutableByteArray :: MutableByteArray -> IO Int
9388sizeOfMutableByteArray (MutableByteArray arr) =
9489 IO $ \s ->
9590 case getSizeofMutableByteArray# arr s of
@@ -100,11 +95,11 @@ sizeOfMutableByteArray (MutableByteArray arr) =
10095--------------------------------------------------------------------------------
10196
10297{-# NOINLINE nil #-}
103−nil :: MutableByteArray a
98+nil :: MutableByteArray
10499nil = unsafePerformIO $ newUnpinnedBytes 0
105100
106101{-# INLINE newUnpinnedBytes #-}
107−newUnpinnedBytes :: Int -> IO (MutableByteArray a)
102+newUnpinnedBytes :: Int -> IO MutableByteArray
108103newUnpinnedBytes nbytes | nbytes < 0 =
109104 errorWithoutStackTrace "newUnpinnedBytes: size must be >= 0"
110105newUnpinnedBytes (I# nbytes) = IO $ \s ->
@@ -114,7 +109,7 @@ newUnpinnedBytes (I# nbytes) = IO $ \s ->
114109 in (# s', c #)
115110
116111{-# INLINE newPinnedBytes #-}
117−newPinnedBytes :: Int -> IO (MutableByteArray a)
112+newPinnedBytes :: Int -> IO MutableByteArray
118113newPinnedBytes nbytes | nbytes < 0 =
119114 errorWithoutStackTrace "newPinnedBytes: size must be >= 0"
120115newPinnedBytes (I# nbytes) = IO $ \s ->
@@ -124,7 +119,7 @@ newPinnedBytes (I# nbytes) = IO $ \s ->
124119 in (# s', c #)
125120
126121{-# INLINE newAlignedPinnedBytes #-}
127−newAlignedPinnedBytes :: Int -> Int -> IO (MutableByteArray a)
122+newAlignedPinnedBytes :: Int -> Int -> IO MutableByteArray
128123newAlignedPinnedBytes nbytes _align | nbytes < 0 =
129124 errorWithoutStackTrace "newAlignedPinnedBytes: size must be >= 0"
130125newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
@@ -138,7 +133,7 @@ newAlignedPinnedBytes (I# nbytes) (I# align) = IO $ \s ->
138133-------------------------------------------------------------------------------
139134
140135{-# INLINE isPinned #-}
141−isPinned :: MutableByteArray a -> Bool
136+isPinned :: MutableByteArray -> Bool
142137isPinned (MutableByteArray arr#) =
143138 let pinnedInt = I# (isMutableByteArrayPinned# arr#)
144139 in pinnedInt == 1
@@ -160,7 +155,7 @@ cloneMutableArrayWith# alloc# arr# s# =
160155 s3# -> (# s3#, arr1# #)
161156
162157{-# INLINE pin #-}
163−pin :: MutableByteArray a -> IO (MutableByteArray a)
158+pin :: MutableByteArray -> IO MutableByteArray
164159pin arr@(MutableByteArray marr#) =
165160 if isPinned arr
166161 then return arr
@@ -170,7 +165,7 @@ pin arr@(MutableByteArray marr#) =
170165 (# s1#, marr1# #) -> (# s1#, MutableByteArray marr1# #)
171166
172167{-# INLINE unpin #-}
173−unpin :: MutableByteArray a -> IO (MutableByteArray a)
168+unpin :: MutableByteArray -> IO MutableByteArray
174169unpin arr@(MutableByteArray marr#) =
175170 if not (isPinned arr)
176171 then return arr
@@ -185,8 +180,6 @@ unpin arr@(MutableByteArray marr#) =
185180
186181-- XXX Use Proxy type in sizeOf method.
187182-- XXX generate error if the size is < 1
188−-- XXX Remove the phantom parameter from MutableByteArray?
189−-- XXX Rename/remove castContents
190183
191184-- In theory we could convert a type to and from a byte stream and use that
192185-- to implement boxing, unboxing. But that would be inefficient. This type
@@ -250,19 +243,16 @@ unpin arr@(MutableByteArray marr#) =
250243--
251244-- If you want to write the instance manually:
252245--
253−-- >>> import Streamly.Internal.Data.Unboxed (castContents)
254246-- >>> :{
255247-- instance Unbox Object where
256248-- sizeOf _ = 16
257249-- peekByteIndex i arr = do
258−-- let p = castContents arr
259−-- x0 <- peekByteIndex i p
260−-- x1 <- peekByteIndex (i + 8) p
250+-- x0 <- peekByteIndex i arr
251+-- x1 <- peekByteIndex (i + 8) arr
261252-- return $ Object x0 x1
262253-- pokeByteIndex i arr (Object x0 x1) = do
263−-- let p = castContents arr
264−-- pokeByteIndex i p x0
265−-- pokeByteIndex (i + 8) p x1
254+-- pokeByteIndex i arr x0
255+-- pokeByteIndex (i + 8) arr x1
266256-- :}
267257--
268258class Unbox a where
@@ -277,10 +267,10 @@ class Unbox a where
277267 --
278268 -- IMPORTANT: The implementation of this interface may not check the bounds
279269 -- of the array, the caller must not assume that.
280− peekByteIndex :: Int -> MutableByteArray a -> IO a
270+ peekByteIndex :: Int -> MutableByteArray -> IO a
281271
282272 default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
283− Int -> MutableByteArray a -> IO a
273+ Int -> MutableByteArray -> IO a
284274 peekByteIndex i arr = genericPeekByteIndex arr i
285275
286276 -- | Write an element of type "a" to a MutableByteArray given the byte
@@ -288,10 +278,10 @@ class Unbox a where
288278 --
289279 -- IMPORTANT: The implementation of this interface may not check the bounds
290280 -- of the array, the caller must not assume that.
291− pokeByteIndex :: Int -> MutableByteArray a -> a -> IO ()
281+ pokeByteIndex :: Int -> MutableByteArray -> a -> IO ()
292282
293283 default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
294− Int -> MutableByteArray a -> a -> IO ()
284+ Int -> MutableByteArray -> a -> IO ()
295285 pokeByteIndex i arr = genericPokeByteIndex arr i
296286
297287#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
@@ -310,9 +300,9 @@ instance Unbox _type where { \
310300#define DERIVE_WRAPPED_UNBOX(_constraint, _type, _constructor, _innerType) \
311301instance _constraint Unbox _type where \
312302; {-# INLINE peekByteIndex #-} \
313−; peekByteIndex i arr = _constructor <$> peekByteIndex i (castContents arr) \
303+; peekByteIndex i arr = _constructor <$> peekByteIndex i arr \
314304; {-# INLINE pokeByteIndex #-} \
315−; pokeByteIndex i arr (_constructor a) = pokeByteIndex i (castContents arr) a \
305+; pokeByteIndex i arr (_constructor a) = pokeByteIndex i arr a \
316306; {-# INLINE sizeOf #-} \
317307; sizeOf _ = SIZE_OF(_innerType)
318308
@@ -320,15 +310,13 @@ instance _constraint Unbox _type where \
320310instance _constraint Unbox _type where { \
321311; {-# INLINE peekByteIndex #-} \
322312; peekByteIndex i arr = \
323− let contents = castContents arr :: MutableByteArray _innerType \
324− in peekByteIndex i contents >>= \
325− (\p1 -> peekByteIndex (i + SIZE_OF(_innerType)) contents \
326− <&> _constructor p1) \
313+ peekByteIndex i arr >>= \
314+ (\p1 -> peekByteIndex (i + SIZE_OF(_innerType)) arr \
315+ <&> _constructor p1) \
327316; {-# INLINE pokeByteIndex #-} \
328317; pokeByteIndex i arr (_constructor p1 p2) = \
329− let contents = castContents arr :: MutableByteArray _innerType \
330− in pokeByteIndex i contents p1 >> \
331− pokeByteIndex (i + SIZE_OF(_innerType)) contents p2 \
318+ pokeByteIndex i arr p1 >> \
319+ pokeByteIndex (i + SIZE_OF(_innerType)) arr p2 \
332320; {-# INLINE sizeOf #-} \
333321; sizeOf _ = 2 * SIZE_OF(_innerType) \
334322}
@@ -455,10 +443,10 @@ instance Unbox () where
455443instance Unbox IoSubSystem where
456444
457445 {-# INLINE peekByteIndex #-}
458− peekByteIndex i arr = toEnum <$> peekByteIndex i (castContents arr)
446+ peekByteIndex i arr = toEnum <$> peekByteIndex i arr
459447
460448 {-# INLINE pokeByteIndex #-}
461− pokeByteIndex i arr a = pokeByteIndex i (castContents arr) (fromEnum a)
449+ pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
462450
463451 {-# INLINE sizeOf #-}
464452 sizeOf = sizeOf . fromEnum
@@ -468,14 +456,14 @@ instance Unbox Bool where
468456
469457 {-# INLINE peekByteIndex #-}
470458 peekByteIndex i arr = do
471− res <- peekByteIndex i (castContents arr)
459+ res <- peekByteIndex i arr
472460 return $ res /= (0 :: Int8)
473461
474462 {-# INLINE pokeByteIndex #-}
475463 pokeByteIndex i arr a =
476464 if a
477− then pokeByteIndex i (castContents arr) (1 :: Int8)
478− else pokeByteIndex i (castContents arr) (0 :: Int8)
465+ then pokeByteIndex i arr (1 :: Int8)
466+ else pokeByteIndex i arr (0 :: Int8)
479467
480468 {-# INLINE sizeOf #-}
481469 sizeOf _ = 1
@@ -485,11 +473,11 @@ instance Unbox Bool where
485473--------------------------------------------------------------------------------
486474
487475{-# INLINE peekWith #-}
488−peekWith :: Unbox a => MutableByteArray a -> Int -> IO a
476+peekWith :: Unbox a => MutableByteArray -> Int -> IO a
489477peekWith arr i = peekByteIndex i arr
490478
491479{-# INLINE pokeWith #-}
492−pokeWith :: Unbox a => MutableByteArray a -> Int -> a -> IO ()
480+pokeWith :: Unbox a => MutableByteArray -> Int -> a -> IO ()
493481pokeWith arr i = pokeByteIndex i arr
494482
495483--------------------------------------------------------------------------------
@@ -502,7 +490,7 @@ pokeWith arr i = pokeByteIndex i arr
502490-- cheaper to just get the bound using the size of the array whenever needed?
503491data BoundedPtr =
504492 BoundedPtr
505− (MutableByteArray Word8) -- byte array
493+ MutableByteArray -- byte array
506494 Int -- current pos
507495 Int -- position after end
508496
@@ -524,7 +512,7 @@ readUnsafe = Peeker (Builder step)
524512 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
525513 step (BoundedPtr arr pos end) = do
526514 let next = pos + sizeOf (undefined :: a)
527− r <- peekByteIndex pos (castContents arr)
515+ r <- peekByteIndex pos arr
528516 return (BoundedPtr arr next end, r)
529517
530518{-# INLINE read #-}
@@ -538,7 +526,7 @@ read = Peeker (Builder step)
538526 step (BoundedPtr arr pos end) = do
539527 let next = pos + sizeOf (undefined :: a)
540528 when (next > end) $ error "peekObject reading beyond limit"
541− r <- peekByteIndex pos (castContents arr)
529+ r <- peekByteIndex pos arr
542530 return (BoundedPtr arr next end, r)
543531
544532{-# INLINE skipByte #-}
@@ -572,7 +560,7 @@ runPeeker (Peeker (Builder f)) ptr = fmap snd (f ptr)
572560pokeBoundedPtrUnsafe :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
573561pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
574562 let next = pos + sizeOf a
575− pokeByteIndex pos (castContents arr) a
563+ pokeByteIndex pos arr a
576564 return (BoundedPtr arr next end)
577565
578566{-# INLINE pokeBoundedPtr #-}
@@ -580,7 +568,7 @@ pokeBoundedPtr :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
580568pokeBoundedPtr a (BoundedPtr arr pos end) = do
581569 let next = pos + sizeOf a
582570 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
583− pokeByteIndex pos (castContents arr) a
571+ pokeByteIndex pos arr a
584572 return (BoundedPtr arr next end)
585573
586574--------------------------------------------------------------------------------
@@ -758,11 +746,11 @@ genericPokeObj a ptr = void $ genericPokeObject a ptr
758746
759747{-# INLINE genericPokeByteIndex #-}
760748genericPokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
761− MutableByteArray a -> Int -> a -> IO ()
749+ MutableByteArray -> Int -> a -> IO ()
762750genericPokeByteIndex arr index x = do
763751 -- XXX Should we use unsafe poke?
764752 end <- sizeOfMutableByteArray arr
765− genericPokeObj x (BoundedPtr (castContents arr) index end)
753+ genericPokeObj x (BoundedPtr arr index end)
766754
767755--------------------------------------------------------------------------------
768756-- Generic peek
@@ -843,8 +831,8 @@ genericPeekBoundedPtr = runPeeker genericPeeker
843831
844832{-# INLINE genericPeekByteIndex #-}
845833genericPeekByteIndex :: (Generic a, PeekRep (Rep a)) =>
846− MutableByteArray a -> Int -> IO a
834+ MutableByteArray -> Int -> IO a
847835genericPeekByteIndex arr index = do
848836 -- XXX Should we use unsafe peek?
849837 end <- sizeOfMutableByteArray arr
850− genericPeekBoundedPtr (BoundedPtr (castContents arr) index end)
838+ genericPeekBoundedPtr (BoundedPtr arr index end)