on master help sign in
Fix the PeekRep/PokeRep instances for U1 constructor
commitb218f9904f911d988eb5348482fe8a806a0c1d64 authorHarendra Kumar <harendra@composewell.com> dateThu 2 Mar 2023, 23:47 (4 years ago) parenta1aea14
1 file changed+16−15
core/src/Streamly/Internal/Data/Unboxed.hs+16−15
core/src/Streamly/Internal/Data/Unboxed.hs+16−15
@@ -43,7 +43,7 @@ module Streamly.Internal.Data.Unboxed
4343
4444import Control.Monad (void, when)
4545import Data.Complex (Complex((:+)))
46−import Data.Functor ((<&>), ($>))
46+import Data.Functor ((<&>))
4747import Data.Functor.Const (Const(..))
4848import Data.Functor.Identity (Identity(..))
4949import Data.Kind (Type)
@@ -549,7 +549,10 @@ skipByte = Peeker (Builder step)
549549 step :: BoundedPtr -> IO (BoundedPtr, ())
550550 step (BoundedPtr arr pos end) = do
551551 let next = pos + 1
552− when (next > end) $ error "skipByte: reading beyond limit"
552+ when (next > end)
553+ $ error $ "skipByte: reading beyond limit. next = "
554+ ++ show next
555+ ++ " end = " ++ show end
553556 return (BoundedPtr arr next end, ())
554557
555558{-# INLINE runPeeker #-}
@@ -634,15 +637,6 @@ instance SizeOfRep V1 where
634637 {-# INLINE sizeOfRep #-}
635638 sizeOfRep = error "sizeOfRep: a value of a Void type must not exist"
636639
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−--
646640-- Note that when a sum type has many unit constructors only a single byte is
647641-- required to encode the type as only the constructor tag is stored.
648642instance SizeOfRep U1 where
@@ -685,6 +679,15 @@ instance (MaxArity256 (SumArity (f :+: g)), SizeOfRepSum f, SizeOfRepSum g) =>
685679 max (sizeOfRepSum (undefined :: f x))
686680 (sizeOfRepSum (undefined :: g x))
687681
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+--
688691{-# INLINE genericSizeOf #-}
689692genericSizeOf :: forall a. (SizeOfRep (Rep a)) => Proxy a -> Int
690693genericSizeOf _ =
@@ -712,9 +715,7 @@ instance PokeRep V1 where
712715
713716instance PokeRep U1 where
714717 {-# INLINE pokeRep #-}
715− -- XXX Can we use sizeof U1 to determine the size?
716− pokeRep _ (BoundedPtr arr pos end) =
717− return (BoundedPtr arr (pos + 1) end)
718+ pokeRep _ x = pure x
718719
719720instance (PokeRep f, PokeRep g) => PokeRep (f :*: g) where
720721 {-# INLINE pokeRep #-}
@@ -785,7 +786,7 @@ instance PeekRep V1 where
785786
786787instance PeekRep U1 where
787788 {-# INLINE peekRep #-}
788− peekRep = skipByte $> U1
789+ peekRep = pure U1
789790
790791instance (PeekRep f, PeekRep g) => PeekRep (f :*: g) where
791792 {-# INLINE peekRep #-}