module Data.Unique.Really (
Unique, newUnique, hashUnique,
) where
import Control.Applicative ((<$>))
import Data.Hashable
#if UseGHC
import Control.Exception (evaluate)
import qualified Data.Unique
import System.Mem.StableName
newtype Unique = Unique (StableName Data.Unique.Unique) deriving (Unique -> Unique -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Unique -> Unique -> Bool
$c/= :: Unique -> Unique -> Bool
== :: Unique -> Unique -> Bool
$c== :: Unique -> Unique -> Bool
Eq)
newUnique :: IO Unique
newUnique = do
Unique
x <- IO Unique
Data.Unique.newUnique
Unique
_ <- forall a. a -> IO a
evaluate Unique
x
StableName Unique -> Unique
Unique forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> IO (StableName a)
makeStableName Unique
x
hashUnique :: Unique -> Int
hashUnique (Unique StableName Unique
s) = forall a. StableName a -> Int
hashStableName StableName Unique
s
#else
import Data.IORef
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE refNumber #-}
refNumber :: IORef Integer
refNumber = unsafePerformIO $ newIORef 0
newNumber = atomicModifyIORef' refNumber $ \x -> let x' = x+1 in (x', x')
newtype Unique = Unique Integer deriving (Eq,Ord)
newUnique = Unique <$> newNumber
hashUnique (Unique s) = fromIntegral s
#endif
newUnique :: IO Unique
hashUnique :: Unique -> Int
instance Hashable Unique where hashWithSalt :: Int -> Unique -> Int
hashWithSalt Int
s = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique -> Int
hashUnique