on master help sign in
Fix the PeekRep/PokeRep instances for U1 constructor
b218f99Harendra Kumar
core/src/Streamly/Internal/Data/Unboxed.hs+16−15
@@ -43,7 +43,7 @@ module Streamly.Internal.Data.Unboxed
43
43
44import Control.Monad (void, when)
44import Control.Monad (void, when)
45import Data.Complex (Complex((:+)))
45import Data.Complex (Complex((:+)))
46−import Data.Functor ((<&>), ($>))
46+import Data.Functor ((<&>))
47import Data.Functor.Const (Const(..))
47import Data.Functor.Const (Const(..))
48import Data.Functor.Identity (Identity(..))
48import Data.Functor.Identity (Identity(..))
49import Data.Kind (Type)
49import Data.Kind (Type)
@@ -549,7 +549,10 @@ skipByte = Peeker (Builder step)
549 step :: BoundedPtr -> IO (BoundedPtr, ())
549 step :: BoundedPtr -> IO (BoundedPtr, ())
550 step (BoundedPtr arr pos end) = do
550 step (BoundedPtr arr pos end) = do
551 let next = pos + 1
551 let next = pos + 1
552+ when (next > end)
552− when (next > end) $ error "skipByte: reading beyond limit"
553+ $ error $ "skipByte: reading beyond limit. next = "
554+ ++ show next
555+ ++ " end = " ++ show end
553 return (BoundedPtr arr next end, ())
556 return (BoundedPtr arr next end, ())
554
557
555{-# INLINE runPeeker #-}
558{-# INLINE runPeeker #-}
@@ -634,15 +637,6 @@ instance SizeOfRep V1 where
634 {-# INLINE sizeOfRep #-}
637 {-# INLINE sizeOfRep #-}
635 sizeOfRep = error "sizeOfRep: a value of a Void type must not exist"
638 sizeOfRep = error "sizeOfRep: a value of a Void type must not exist"
636
639
637−-- Unit: constructors without arguments.
638−-- Theoretically the size can be 0, but we use 1 to simplify the implementation
639−-- of an array of unit type elements. With a non-zero size we can count the number
640−-- of elements in the array based on the size of the array. Otherwise we will
641−-- have to store a virtual length in the array, but keep the physical size of
642−-- the array as 0. Or we will have to make a special handling for zero sized
643−-- elements to make the size as 1. Or we can disallow arrays with elements
644−-- having size 0.
645−--
646-- Note that when a sum type has many unit constructors only a single byte is
640-- Note that when a sum type has many unit constructors only a single byte is
647-- required to encode the type as only the constructor tag is stored.
641-- required to encode the type as only the constructor tag is stored.
648instance SizeOfRep U1 where
642instance SizeOfRep U1 where
@@ -685,6 +679,15 @@ instance (MaxArity256 (SumArity (f :+: g)), SizeOfRepSum f, SizeOfRepSum g) =>
685 max (sizeOfRepSum (undefined :: f x))
679 max (sizeOfRepSum (undefined :: f x))
686 (sizeOfRepSum (undefined :: g x))
680 (sizeOfRepSum (undefined :: g x))
687
681
682+-- Unit: constructors without arguments.
683+-- Theoretically the size can be 0, but we use 1 to simplify the implementation
684+-- of an array of unit type elements. With a non-zero size we can count the number
685+-- of elements in the array based on the size of the array. Otherwise we will
686+-- have to store a virtual length in the array, but keep the physical size of
687+-- the array as 0. Or we will have to make a special handling for zero sized
688+-- elements to make the size as 1. Or we can disallow arrays with elements
689+-- having size 0.
690+--
688{-# INLINE genericSizeOf #-}
691{-# INLINE genericSizeOf #-}
689genericSizeOf :: forall a. (SizeOfRep (Rep a)) => Proxy a -> Int
692genericSizeOf :: forall a. (SizeOfRep (Rep a)) => Proxy a -> Int
690genericSizeOf _ =
693genericSizeOf _ =
@@ -712,9 +715,7 @@ instance PokeRep V1 where
712
715
713instance PokeRep U1 where
716instance PokeRep U1 where
714 {-# INLINE pokeRep #-}
717 {-# INLINE pokeRep #-}
715− -- XXX Can we use sizeof U1 to determine the size?
718+ pokeRep _ x = pure x
716− pokeRep _ (BoundedPtr arr pos end) =
717− return (BoundedPtr arr (pos + 1) end)
718
719
719instance (PokeRep f, PokeRep g) => PokeRep (f :*: g) where
720instance (PokeRep f, PokeRep g) => PokeRep (f :*: g) where
720 {-# INLINE pokeRep #-}
721 {-# INLINE pokeRep #-}
@@ -785,7 +786,7 @@ instance PeekRep V1 where
785
786
786instance PeekRep U1 where
787instance PeekRep U1 where
787 {-# INLINE peekRep #-}
788 {-# INLINE peekRep #-}
788− peekRep = skipByte $> U1
789+ peekRep = pure U1
789
790
790instance (PeekRep f, PeekRep g) => PeekRep (f :*: g) where
791instance (PeekRep f, PeekRep g) => PeekRep (f :*: g) where
791 {-# INLINE peekRep #-}
792 {-# INLINE peekRep #-}