@@ -178,7 +178,6 @@ unpin arr@(MutableByteArray marr#) =
178-- The Unbox type class
178-- The Unbox type class
179--------------------------------------------------------------------------------
179--------------------------------------------------------------------------------
181−-- XXX Use Proxy type in sizeOf method.
182-- XXX generate error if the size is < 1
181-- XXX generate error if the size is < 1
184-- In theory we could convert a type to and from a byte stream and use that
183-- In theory we could convert a type to and from a byte stream and use that
@@ -257,10 +256,10 @@ unpin arr@(MutableByteArray marr#) =
258class Unbox a where
257class Unbox a where
259 -- | Get the size.
258 -- | Get the size.
260− sizeOf :: a -> Int
259+ sizeOf :: Proxy a -> Int
262− default sizeOf :: (SizeOfRep (Rep a)) => a -> Int
261+ default sizeOf :: (SizeOfRep (Rep a)) => Proxy a -> Int
263− sizeOf _ = genericSizeOf (Proxy :: Proxy a)
262+ sizeOf = genericSizeOf
265 -- | Read an element of type "a" from a MutableByteArray given the byte
264 -- | Read an element of type "a" from a MutableByteArray given the byte
266 -- index.
265 -- index.
@@ -449,7 +448,7 @@ instance Unbox IoSubSystem where
449 pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
448 pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
451 {-# INLINE sizeOf #-}
450 {-# INLINE sizeOf #-}
452− sizeOf = sizeOf . fromEnum
451+ sizeOf _ = sizeOf (Proxy :: Proxy Int)
455instance Unbox Bool where
454instance Unbox Bool where
@@ -511,7 +510,7 @@ readUnsafe = Peeker (Builder step)
511 {-# INLINE step #-}
510 {-# INLINE step #-}
512 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
511 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
513 step (BoundedPtr arr pos end) = do
512 step (BoundedPtr arr pos end) = do
514− let next = pos + sizeOf (undefined :: a)
513+ let next = pos + sizeOf (Proxy :: Proxy a)
515 r <- peekByteIndex pos arr
514 r <- peekByteIndex pos arr
516 return (BoundedPtr arr next end, r)
515 return (BoundedPtr arr next end, r)
@@ -524,7 +523,7 @@ read = Peeker (Builder step)
524 {-# INLINE step #-}
523 {-# INLINE step #-}
525 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
524 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
526 step (BoundedPtr arr pos end) = do
525 step (BoundedPtr arr pos end) = do
527− let next = pos + sizeOf (undefined :: a)
526+ let next = pos + sizeOf (Proxy :: Proxy a)
528 when (next > end) $ error "peekObject reading beyond limit"
527 when (next > end) $ error "peekObject reading beyond limit"
529 r <- peekByteIndex pos arr
528 r <- peekByteIndex pos arr
530 return (BoundedPtr arr next end, r)
529 return (BoundedPtr arr next end, r)
@@ -557,16 +556,16 @@ runPeeker (Peeker (Builder f)) ptr = fmap snd (f ptr)
558-- Does not check writing beyond bound.
557-- Does not check writing beyond bound.
559{-# INLINE pokeBoundedPtrUnsafe #-}
558{-# INLINE pokeBoundedPtrUnsafe #-}
560−pokeBoundedPtrUnsafe :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
559+pokeBoundedPtrUnsafe :: forall a. Unbox a => a -> BoundedPtr -> IO BoundedPtr
561pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
560pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
562− let next = pos + sizeOf a
561+ let next = pos + sizeOf (Proxy :: Proxy a)
563 pokeByteIndex pos arr a
562 pokeByteIndex pos arr a
564 return (BoundedPtr arr next end)
563 return (BoundedPtr arr next end)
566{-# INLINE pokeBoundedPtr #-}
565{-# INLINE pokeBoundedPtr #-}
567−pokeBoundedPtr :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
566+pokeBoundedPtr :: forall a. Unbox a => a -> BoundedPtr -> IO BoundedPtr
568pokeBoundedPtr a (BoundedPtr arr pos end) = do
567pokeBoundedPtr a (BoundedPtr arr pos end) = do
569− let next = pos + sizeOf a
568+ let next = pos + sizeOf (Proxy :: Proxy a)
570 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
569 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
571 pokeByteIndex pos arr a
570 pokeByteIndex pos arr a
572 return (BoundedPtr arr next end)
571 return (BoundedPtr arr next end)
@@ -615,7 +614,7 @@ instance SizeOfRep f => SizeOfRep (M1 i c f) where
615-- Primitive type "a".
614-- Primitive type "a".
616instance Unbox a => SizeOfRep (K1 i a) where
615instance Unbox a => SizeOfRep (K1 i a) where
617 {-# INLINE sizeOfRep #-}
616 {-# INLINE sizeOfRep #-}
618− sizeOfRep _ = sizeOf (undefined :: a)
617+ sizeOfRep _ = sizeOf (Proxy :: Proxy a)
620-- Void: data type without constructors. Values of this type cannot exist,
619-- Void: data type without constructors. Values of this type cannot exist,
621-- therefore the size is undefined. We should never be serializing structures
620-- therefore the size is undefined. We should never be serializing structures
@@ -671,7 +670,7 @@ instance (MaxArity256 (SumArity (f :+: g)), SizeOfRepSum f, SizeOfRepSum g) =>
671 {-# INLINE sizeOfRep #-}
670 {-# INLINE sizeOfRep #-}
672 sizeOfRep _ =
671 sizeOfRep _ =
673 -- One byte for the constructor id and then the constructor value.
672 -- One byte for the constructor id and then the constructor value.
674− sizeOf (undefined :: Word8) +
673+ sizeOf (Proxy :: Proxy Word8) +
675 max (sizeOfRepSum (undefined :: f x))
674 max (sizeOfRepSum (undefined :: f x))
676 (sizeOfRepSum (undefined :: g x))
675 (sizeOfRepSum (undefined :: g x))