{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MagicHash #-}
-- |
-- Module      : Data.Prim.Memory.Text
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Prim.Memory.Text
  ( Text(..)
  , MText(..)
  , Array(..)
  , MArray(..)
  , fromArrayBytes
  , toArrayBytes
  , fromMArrayMBytes
  , toMArrayMBytes
  ) where

import Data.Text.Array
import Data.Text.Internal
import Data.Prim.Memory.Bytes.Internal
  ( Bytes(..)
  , MBytes(..)
  , Pinned(..)
  )

-- | Mutable version of a `Text`
data MText s =
  MText
    {-# UNPACK #-}!(MArray s) -- payload (Word16 elements)
    {-# UNPACK #-}!Int        -- offset (units of Word16, not Char)
    {-# UNPACK #-}!Int        -- length (units of Word16, not Char)

-- | /O(1)/ - Cast an immutable `Data.Text.Array.Array` from @text@ package to immutable `Bytes`
--
-- @since 0.3.0
fromArrayBytes :: Array -> Bytes 'Inc
fromArrayBytes :: Array -> Bytes 'Inc
fromArrayBytes (Array ByteArray#
ba#) = ByteArray# -> Bytes 'Inc
forall (p :: Pinned). ByteArray# -> Bytes p
Bytes ByteArray#
ba#
{-# INLINE fromArrayBytes #-}

-- | /O(1)/ - Cast immutable `Bytes` to an immutable `Data.Text.Array.Array` from @text@ package
--
-- @since 0.3.0
toArrayBytes :: Bytes p -> Array
toArrayBytes :: Bytes p -> Array
toArrayBytes (Bytes ByteArray#
ba#) = ByteArray# -> Array
Array ByteArray#
ba#
{-# INLINE toArrayBytes #-}

-- | /O(1)/ - Cast a mutable `Data.Text.Array.MArray` from @text@ package to mutable `MBytes`
--
-- @since 0.3.0
fromMArrayMBytes :: MArray s -> MBytes 'Inc s
fromMArrayMBytes :: MArray s -> MBytes 'Inc s
fromMArrayMBytes (MArray MutableByteArray# s
mba#) = MutableByteArray# s -> MBytes 'Inc s
forall (p :: Pinned) s. MutableByteArray# s -> MBytes p s
MBytes MutableByteArray# s
mba#
{-# INLINE fromMArrayMBytes #-}

-- | /O(1)/ - Cast mutable `MBytes` to a mutable `Data.Text.Array.MArray` from @text@ package
--
-- @since 0.3.0
toMArrayMBytes :: MBytes p s -> MArray s
toMArrayMBytes :: MBytes p s -> MArray s
toMArrayMBytes (MBytes MutableByteArray# s
ba#) = MutableByteArray# s -> MArray s
forall s. MutableByteArray# s -> MArray s
MArray MutableByteArray# s
ba#
{-# INLINE toMArrayMBytes #-}