{-# language MagicHash #-}
{-# language UnboxedTuples #-}
{-# language TypeFamilies #-}
{-# language ScopedTypeVariables #-}
{-# language DataKinds #-}

module Data.Primitive.Unlifted.Class
  ( PrimUnlifted(..)
  ) where

import Data.ByteString.Short.Internal (ShortByteString(SBS))
import Data.Text.Short (ShortText,toShortByteString)
import Data.Text.Short.Unsafe (fromShortByteStringUnsafe)
import Data.Primitive.PrimArray (PrimArray(..),MutablePrimArray(..))
import Data.Primitive.ByteArray (ByteArray(..),MutableByteArray(..))
import Data.Primitive.Array (Array (..), MutableArray (..))
import Data.Primitive.SmallArray (SmallArray (..), SmallMutableArray (..))
import Data.Primitive.MutVar (MutVar (..))
import GHC.MVar (MVar(..))
import GHC.IORef (IORef(..))
import GHC.STRef (STRef(..))
import GHC.Weak (Weak(..))
import GHC.Conc (TVar(..),ThreadId(..))
import GHC.StableName (StableName(..))
import GHC.Exts (MutableByteArray#,ByteArray#
                ,Array#,MutableArray#,SmallArray#,SmallMutableArray#
                ,Weak#,TVar#,ThreadId#,StableName#)
import GHC.Exts (MVar#,MutVar#,RealWorld)

import qualified Data.Primitive.MVar as PM
import qualified GHC.Exts as Exts

import Data.Primitive.Unlifted.Type

class PrimUnlifted a where
  type Unlifted a :: UnliftedType
  toUnlifted# :: a -> Unlifted a
  fromUnlifted# :: Unlifted a -> a

instance PrimUnlifted (Array a) where
  type Unlifted (Array a) = Array# a
  toUnlifted# :: Array a -> Unlifted (Array a)
toUnlifted# (Array Array# a
a) = Array# a
Unlifted (Array a)
a
  fromUnlifted# :: Unlifted (Array a) -> Array a
fromUnlifted# Unlifted (Array a)
x = Array# a -> Array a
forall a. Array# a -> Array a
Array Array# a
Unlifted (Array a)
x

instance PrimUnlifted (MutableArray s a) where
  type Unlifted (MutableArray s a) = MutableArray# s a
  toUnlifted# :: MutableArray s a -> Unlifted (MutableArray s a)
toUnlifted# (MutableArray MutableArray# s a
a) = MutableArray# s a
Unlifted (MutableArray s a)
a
  fromUnlifted# :: Unlifted (MutableArray s a) -> MutableArray s a
fromUnlifted# Unlifted (MutableArray s a)
x = MutableArray# s a -> MutableArray s a
forall s a. MutableArray# s a -> MutableArray s a
MutableArray MutableArray# s a
Unlifted (MutableArray s a)
x

instance PrimUnlifted (SmallArray a) where
  type Unlifted (SmallArray a) = SmallArray# a
  toUnlifted# :: SmallArray a -> Unlifted (SmallArray a)
toUnlifted# (SmallArray SmallArray# a
a) = SmallArray# a
Unlifted (SmallArray a)
a
  fromUnlifted# :: Unlifted (SmallArray a) -> SmallArray a
fromUnlifted# Unlifted (SmallArray a)
x = SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray SmallArray# a
Unlifted (SmallArray a)
x

instance PrimUnlifted (SmallMutableArray s a) where
  type Unlifted (SmallMutableArray s a) = SmallMutableArray# s a
  toUnlifted# :: SmallMutableArray s a -> Unlifted (SmallMutableArray s a)
toUnlifted# (SmallMutableArray SmallMutableArray# s a
a) = SmallMutableArray# s a
Unlifted (SmallMutableArray s a)
a
  fromUnlifted# :: Unlifted (SmallMutableArray s a) -> SmallMutableArray s a
fromUnlifted# Unlifted (SmallMutableArray s a)
x = SmallMutableArray# s a -> SmallMutableArray s a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# s a
Unlifted (SmallMutableArray s a)
x

instance PrimUnlifted (PrimArray a) where
  type Unlifted (PrimArray a) = ByteArray#
  toUnlifted# :: PrimArray a -> Unlifted (PrimArray a)
toUnlifted# (PrimArray ByteArray#
x) = ByteArray#
Unlifted (PrimArray a)
x
  fromUnlifted# :: Unlifted (PrimArray a) -> PrimArray a
fromUnlifted# Unlifted (PrimArray a)
x = ByteArray# -> PrimArray a
forall a. ByteArray# -> PrimArray a
PrimArray ByteArray#
Unlifted (PrimArray a)
x

instance PrimUnlifted ByteArray where
  type Unlifted ByteArray = ByteArray#
  toUnlifted# :: ByteArray -> Unlifted ByteArray
toUnlifted# (ByteArray ByteArray#
x) = ByteArray#
Unlifted ByteArray
x
  fromUnlifted# :: Unlifted ByteArray -> ByteArray
fromUnlifted# Unlifted ByteArray
x = ByteArray# -> ByteArray
ByteArray ByteArray#
Unlifted ByteArray
x

instance PrimUnlifted ShortByteString where
  type Unlifted ShortByteString = ByteArray#
  toUnlifted# :: ShortByteString -> Unlifted ShortByteString
toUnlifted# (SBS ByteArray#
x) = ByteArray#
Unlifted ShortByteString
x
  fromUnlifted# :: Unlifted ShortByteString -> ShortByteString
fromUnlifted# Unlifted ShortByteString
x = ByteArray# -> ShortByteString
SBS ByteArray#
Unlifted ShortByteString
x

instance PrimUnlifted ShortText where
  type Unlifted ShortText = ByteArray#
  toUnlifted# :: ShortText -> Unlifted ShortText
toUnlifted# ShortText
t = case ShortText -> ShortByteString
toShortByteString ShortText
t of { SBS ByteArray#
x -> ByteArray#
Unlifted ShortText
x }
  fromUnlifted# :: Unlifted ShortText -> ShortText
fromUnlifted# Unlifted ShortText
x = ShortByteString -> ShortText
fromShortByteStringUnsafe (ByteArray# -> ShortByteString
SBS ByteArray#
Unlifted ShortText
x)

instance PrimUnlifted (MutableByteArray s) where
  type Unlifted (MutableByteArray s) = MutableByteArray# s
  toUnlifted# :: MutableByteArray s -> Unlifted (MutableByteArray s)
toUnlifted# (MutableByteArray MutableByteArray# s
x) = MutableByteArray# s
Unlifted (MutableByteArray s)
x
  fromUnlifted# :: Unlifted (MutableByteArray s) -> MutableByteArray s
fromUnlifted# Unlifted (MutableByteArray s)
x = MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
Unlifted (MutableByteArray s)
x

instance PrimUnlifted (MutablePrimArray s a) where
  type Unlifted (MutablePrimArray s a) = MutableByteArray# s
  toUnlifted# :: MutablePrimArray s a -> Unlifted (MutablePrimArray s a)
toUnlifted# (MutablePrimArray MutableByteArray# s
x) = MutableByteArray# s
Unlifted (MutablePrimArray s a)
x
  fromUnlifted# :: Unlifted (MutablePrimArray s a) -> MutablePrimArray s a
fromUnlifted# Unlifted (MutablePrimArray s a)
x = MutableByteArray# s -> MutablePrimArray s a
forall s a. MutableByteArray# s -> MutablePrimArray s a
MutablePrimArray MutableByteArray# s
Unlifted (MutablePrimArray s a)
x

instance PrimUnlifted (PM.MVar s a) where
  type Unlifted (PM.MVar s a) = MVar# s a
  toUnlifted# :: MVar s a -> Unlifted (MVar s a)
toUnlifted# (PM.MVar MVar# s a
x) = MVar# s a
Unlifted (MVar s a)
x
  fromUnlifted# :: Unlifted (MVar s a) -> MVar s a
fromUnlifted# Unlifted (MVar s a)
x = MVar# s a -> MVar s a
forall s a. MVar# s a -> MVar s a
PM.MVar MVar# s a
Unlifted (MVar s a)
x

instance PrimUnlifted (MVar a) where
  type Unlifted (MVar a) = MVar# RealWorld a
  toUnlifted# :: MVar a -> Unlifted (MVar a)
toUnlifted# (MVar MVar# RealWorld a
x) = MVar# RealWorld a
Unlifted (MVar a)
x
  fromUnlifted# :: Unlifted (MVar a) -> MVar a
fromUnlifted# Unlifted (MVar a)
x = MVar# RealWorld a -> MVar a
forall a. MVar# RealWorld a -> MVar a
MVar MVar# RealWorld a
Unlifted (MVar a)
x

instance PrimUnlifted (MutVar s a) where
  type Unlifted (MutVar s a) = MutVar# s a
  toUnlifted# :: MutVar s a -> Unlifted (MutVar s a)
toUnlifted# (MutVar MutVar# s a
x) = MutVar# s a
Unlifted (MutVar s a)
x
  fromUnlifted# :: Unlifted (MutVar s a) -> MutVar s a
fromUnlifted# Unlifted (MutVar s a)
x = MutVar# s a -> MutVar s a
forall s a. MutVar# s a -> MutVar s a
MutVar MutVar# s a
Unlifted (MutVar s a)
x

instance PrimUnlifted (STRef s a) where
  type Unlifted (STRef s a) = MutVar# s a
  toUnlifted# :: STRef s a -> Unlifted (STRef s a)
toUnlifted# (STRef MutVar# s a
x) = MutVar# s a
Unlifted (STRef s a)
x
  fromUnlifted# :: Unlifted (STRef s a) -> STRef s a
fromUnlifted# Unlifted (STRef s a)
x = MutVar# s a -> STRef s a
forall s a. MutVar# s a -> STRef s a
STRef MutVar# s a
Unlifted (STRef s a)
x

instance PrimUnlifted (IORef a) where
  type Unlifted (IORef a) = MutVar# RealWorld a
  toUnlifted# :: IORef a -> Unlifted (IORef a)
toUnlifted# (IORef (STRef MutVar# RealWorld a
x)) = MutVar# RealWorld a
Unlifted (IORef a)
x
  fromUnlifted# :: Unlifted (IORef a) -> IORef a
fromUnlifted# Unlifted (IORef a)
x = STRef RealWorld a -> IORef a
forall a. STRef RealWorld a -> IORef a
IORef (MutVar# RealWorld a -> STRef RealWorld a
forall s a. MutVar# s a -> STRef s a
STRef MutVar# RealWorld a
Unlifted (IORef a)
x)

instance PrimUnlifted (Weak a) where
  type Unlifted (Weak a) = Weak# a
  toUnlifted# :: Weak a -> Unlifted (Weak a)
toUnlifted# (Weak Weak# a
w) = Weak# a
Unlifted (Weak a)
w
  fromUnlifted# :: Unlifted (Weak a) -> Weak a
fromUnlifted# Unlifted (Weak a)
w = Weak# a -> Weak a
forall v. Weak# v -> Weak v
Weak Weak# a
Unlifted (Weak a)
w

instance PrimUnlifted (TVar a) where
  type Unlifted (TVar a) = TVar# Exts.RealWorld a
  toUnlifted# :: TVar a -> Unlifted (TVar a)
toUnlifted# (TVar TVar# RealWorld a
t) = TVar# RealWorld a
Unlifted (TVar a)
t
  fromUnlifted# :: Unlifted (TVar a) -> TVar a
fromUnlifted# Unlifted (TVar a)
t = TVar# RealWorld a -> TVar a
forall a. TVar# RealWorld a -> TVar a
TVar TVar# RealWorld a
Unlifted (TVar a)
t

instance PrimUnlifted ThreadId where
  type Unlifted ThreadId = ThreadId#
  toUnlifted# :: ThreadId -> Unlifted ThreadId
toUnlifted# (ThreadId ThreadId#
tid) = ThreadId#
Unlifted ThreadId
tid
  fromUnlifted# :: Unlifted ThreadId -> ThreadId
fromUnlifted# Unlifted ThreadId
tid = ThreadId# -> ThreadId
ThreadId ThreadId#
Unlifted ThreadId
tid

instance PrimUnlifted (StableName a) where
  type Unlifted (StableName a) = StableName# a
  toUnlifted# :: StableName a -> Unlifted (StableName a)
toUnlifted# (StableName StableName# a
sn) = StableName# a
Unlifted (StableName a)
sn
  fromUnlifted# :: Unlifted (StableName a) -> StableName a
fromUnlifted# Unlifted (StableName a)
sn = StableName# a -> StableName a
forall a. StableName# a -> StableName a
StableName StableName# a
Unlifted (StableName a)
sn