summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Klebinger <klebinger.andreas@gmx.at>2022-03-15 14:19:40 +0100
committerMatthew Pickering <matthewtpickering@gmail.com>2022-03-25 10:14:13 +0000
commit27a8d36c9e9d1b900193becf3090735788db94a2 (patch)
tree0f68bbdf49379310810c22da4a767f896fad9ccb
parent5ff690b8474c74e9c968ef31e568c1ad0fe719a1 (diff)
downloadhaskell-wip/andreask/export_mbytearray.tar.gz
Export MutableByteArray from Data.Array.Bytewip/andreask/export_mbytearray
This implements CLC proposal #49
-rw-r--r--libraries/base/Data/Array/Byte.hs24
1 files changed, 22 insertions, 2 deletions
diff --git a/libraries/base/Data/Array/Byte.hs b/libraries/base/Data/Array/Byte.hs
index 7d43c03a31..2a49468be1 100644
--- a/libraries/base/Data/Array/Byte.hs
+++ b/libraries/base/Data/Array/Byte.hs
@@ -15,11 +15,12 @@
{-# LANGUAGE UnboxedTuples #-}
module Data.Array.Byte (
- ByteArray(..)
+ ByteArray(..),
+ MutableByteArray(..),
) where
import Data.Bits ((.&.), unsafeShiftR)
-import Data.Data (mkNoRepType, Data(..))
+import Data.Data (mkNoRepType, Data(..), Typeable)
import qualified Data.Foldable as F
import Data.Semigroup
import GHC.Show (intToDigit)
@@ -39,6 +40,16 @@ import GHC.Word (Word8(..))
-- @since 4.17.0.0
data ByteArray = ByteArray ByteArray#
+-- | Boxed wrapper for 'MutableByteArray#'.
+--
+-- Since 'MutableByteArray#' is an unlifted type and not a member of kind 'Data.Kind.Type',
+-- things like @[MutableByteArray#]@ or @IO MutableByteArray#@ are ill-typed. To work around this
+-- inconvenience this module provides a standard boxed wrapper, inhabiting 'Data.Kind.Type'.
+-- Clients are expected to use 'MutableByteArray' in higher-level APIs,
+-- but wrap and unwrap 'MutableByteArray' internally as they please
+-- and use functions from "GHC.Exts".
+--
+-- @since 4.17.0.0
data MutableByteArray s = MutableByteArray (MutableByteArray# s)
-- | Create a new mutable byte array of the specified size in bytes.
@@ -121,6 +132,11 @@ instance Data ByteArray where
gunfold _ _ = error "gunfold"
dataTypeOf _ = mkNoRepType "Data.Array.Byte.ByteArray"
+instance Typeable s => Data (MutableByteArray s) where
+ toConstr _ = error "toConstr"
+ gunfold _ _ = error "gunfold"
+ dataTypeOf _ = mkNoRepType "Data.Array.Byte.MutableByteArray"
+
instance Show ByteArray where
showsPrec _ ba =
showString "[" . go 0
@@ -160,6 +176,10 @@ instance Eq ByteArray where
n1 = sizeofByteArray ba1
n2 = sizeofByteArray ba2
+instance Eq (MutableByteArray s) where
+ (==) (MutableByteArray arr#) (MutableByteArray brr#)
+ = isTrue# (sameMutableByteArray# arr# brr#)
+
-- | Non-lexicographic ordering. This compares the lengths of
-- the byte arrays first and uses a lexicographic ordering if
-- the lengths are equal. Subject to change between major versions.