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
(Unique -> Unique -> Bool)
-> (Unique -> Unique -> Bool) -> Eq Unique
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Unique -> Unique -> Bool
== :: Unique -> Unique -> Bool
$c/= :: Unique -> Unique -> Bool
/= :: Unique -> Unique -> Bool
Eq)
newUnique :: IO Unique
newUnique = do
x <- IO Unique
Data.Unique.newUnique
_ <- evaluate x
Unique <$> makeStableName x
hashUnique :: Unique -> Int
hashUnique (Unique StableName Unique
s) = StableName Unique -> Int
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 = Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
s (Int -> Int) -> (Unique -> Int) -> Unique -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique -> Int
hashUnique