on master help sign in
Add default implementations in the Unbox type class
82a4167Harendra Kumar
core/src/Streamly/Internal/Data/Unboxed.hs+14
@@ -1,4 +1,5 @@
1{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DefaultSignatures #-}
2{-# LANGUAGE UnboxedTuples #-}
3{-# LANGUAGE UnboxedTuples #-}
3{-# LANGUAGE UndecidableInstances #-}
4{-# LANGUAGE UndecidableInstances #-}
4
5
@@ -261,12 +262,21 @@ unpin arr@(MutableByteArray marr#) =
261class Unbox a where
262class Unbox a where
262 -- | Get the size.
263 -- | Get the size.
263 sizeOf :: a -> Int
264 sizeOf :: a -> Int
265+
266+ default sizeOf :: (SizeOfRep (Rep a)) => a -> Int
267+ sizeOf _ = genericSizeOf (Proxy :: Proxy a)
268+
264 -- | Read an element of type "a" from a MutableByteArray given the byte
269 -- | Read an element of type "a" from a MutableByteArray given the byte
265 -- index.
270 -- index.
266 --
271 --
267 -- IMPORTANT: The implementation of this interface may not check the bounds
272 -- IMPORTANT: The implementation of this interface may not check the bounds
268 -- of the array, the caller must not assume that.
273 -- of the array, the caller must not assume that.
269 peekByteIndex :: MutableByteArray a -> Int -> IO a
274 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+
270 -- | Write an element of type "a" to a MutableByteArray given the byte
280 -- | Write an element of type "a" to a MutableByteArray given the byte
271 -- index.
281 -- index.
272 --
282 --
@@ -274,6 +284,10 @@ class Unbox a where
274 -- of the array, the caller must not assume that.
284 -- of the array, the caller must not assume that.
275 pokeByteIndex :: MutableByteArray a -> Int -> a -> IO ()
285 pokeByteIndex :: MutableByteArray a -> Int -> a -> IO ()
276
286
287+ default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
288+ MutableByteArray a -> Int -> a -> IO ()
289+ pokeByteIndex = genericPokeByteIndex
290+
277#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
291#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
278instance Unbox _type where { \
292instance Unbox _type where { \
279; {-# INLINE peekByteIndex #-} \
293; {-# INLINE peekByteIndex #-} \