{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Prim.StableName
( StableName(..)
, makeStableName
, makeAnyStableName
, hashStableName
, eqStableName
) where
import Control.Prim.Monad
import GHC.Exts
#if MIN_VERSION_base(4,12,0)
import GHC.StableName (StableName(..), eqStableName, hashStableName)
instance Show (StableName a) where
showsPrec = showPrecStableName
#else
data StableName a = StableName (StableName# a)
instance Eq (StableName a) where
(==) = eqStableName
instance Show (StableName a) where
showsPrec = showPrecStableName
hashStableName :: StableName a -> Int
hashStableName (StableName sn) = I# (stableNameToInt# sn)
eqStableName :: StableName a -> StableName b -> Bool
eqStableName (StableName sn1) (StableName sn2) =
case eqStableName# sn1 sn2 of
0# -> False
_ -> True
#endif
showPrecStableName :: Int -> StableName a -> ShowS
showPrecStableName n sname =
case n of
0 -> inner
_ -> ('(' :) . inner . (')' :)
where
inner = ("StableName " ++) . shows (hashStableName sname)
makeStableName :: MonadPrim RW m => a -> m (StableName a)
makeStableName a =
prim $ \s ->
case makeStableName# a s of
(# s', sn #) -> (# s', StableName sn #)
makeAnyStableName :: MonadPrim RW m => a -> m (StableName Any)
makeAnyStableName a =
prim $ \s ->
case makeStableName# a s of
(# s', sn# #) -> (# s', StableName (unsafeCoerce# sn#) #)