on master help sign in
Add default implementations in the Unbox type class
commit82a4167fde6a0ff4a2947c152694245129b6a6b4 authorHarendra Kumar <harendra@composewell.com> dateSat 7 Jan 2023, 10:08 (4 years ago) parenta4174aa
1 file changed+14
core/src/Streamly/Internal/Data/Unboxed.hs+14
core/src/Streamly/Internal/Data/Unboxed.hs+14
@@ -1,4 +1,5 @@
11{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DefaultSignatures #-}
23{-# LANGUAGE UnboxedTuples #-}
34{-# LANGUAGE UndecidableInstances #-}
45
@@ -261,12 +262,21 @@ unpin arr@(MutableByteArray marr#) =
261262class Unbox a where
262263 -- | Get the size.
263264 sizeOf :: a -> Int
265+
266+ default sizeOf :: (SizeOfRep (Rep a)) => a -> Int
267+ sizeOf _ = genericSizeOf (Proxy :: Proxy a)
268+
264269 -- | Read an element of type "a" from a MutableByteArray given the byte
265270 -- index.
266271 --
267272 -- IMPORTANT: The implementation of this interface may not check the bounds
268273 -- of the array, the caller must not assume that.
269274 peekByteIndex :: MutableByteArray a -> Int -> IO a
275+
276+ default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
277+ MutableByteArray a -> Int -> IO a
278+ peekByteIndex = genericPeekByteIndex
279+
270280 -- | Write an element of type "a" to a MutableByteArray given the byte
271281 -- index.
272282 --
@@ -274,6 +284,10 @@ class Unbox a where
274284 -- of the array, the caller must not assume that.
275285 pokeByteIndex :: MutableByteArray a -> Int -> a -> IO ()
276286
287+ default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
288+ MutableByteArray a -> Int -> a -> IO ()
289+ pokeByteIndex = genericPokeByteIndex
290+
277291#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
278292instance Unbox _type where { \
279293; {-# INLINE peekByteIndex #-} \