on master help sign in
Add 'Hashable' instance and switch to network-info
commit4637838fcfdd325c4b9bbd45f768cdd29328f1fb authorAntoine Latter <aslatter@gmail.com> dateSat 19 Oct 2013, 13:39 (13 years ago) parent5d304d2
3 files changed+29−16
Data/UUID/Internal.hs+12−1 Data/UUID/V1.hs+15−14 uuid.cabal+2−1
Data/UUID/Internal.hs+12−1
@@ -30,8 +30,9 @@ import Prelude hiding (null)
3030
3131import Control.Monad (liftM4)
3232import Data.Char
33−import Data.Maybe
3433import Data.Bits
34+import Data.Hashable
35+import Data.Maybe
3536import Data.List (elemIndices)
3637
3738#if MIN_VERSION_base(4,0,0)
@@ -273,6 +274,16 @@ instance ByteSource ThreeByte where
273274 b2 = fromIntegral (w `shiftR` 8)
274275 b3 = fromIntegral w
275276
277+instance Hashable UUID where
278+ hash (UUID w0 w1 w2 w3) =
279+ hash w0 `hashWithSalt` w1
280+ `hashWithSalt` w2
281+ `hashWithSalt` w3
282+ hashWithSalt s (UUID w0 w1 w2 w3) =
283+ s `hashWithSalt` w0
284+ `hashWithSalt` w1
285+ `hashWithSalt` w2
286+ `hashWithSalt` w3
276287
277288instance Show UUID where
278289 show = toString
Data/UUID/V1.hs+15−14
@@ -25,9 +25,10 @@ generator with a psuedo-random number.
2525module Data.UUID.V1(nextUUID)
2626where
2727
28−import Data.Time
2928
3029import Data.Bits
30+import Data.Maybe
31+import Data.Time
3132import Data.Word
3233
3334import Control.Applicative ((<$>),(<*>))
@@ -36,8 +37,7 @@ import System.IO.Unsafe
3637
3738import qualified System.Random as R
3839
39−import qualified System.Info.MAC as SysMAC
40−import Data.MAC
40+import Network.Info
4141
4242import Data.UUID.Builder
4343import Data.UUID.Internal
@@ -52,13 +52,13 @@ nextUUID :: IO (Maybe UUID)
5252nextUUID = do
5353 res <- stepTime
5454 case res of
55− Just (mac, c, t) -> return $ Just $ makeUUID t c mac
55+ Just (mac', c, t) -> return $ Just $ makeUUID t c mac'
5656 _ -> return Nothing
5757
5858
5959makeUUID :: Word64 -> Word16 -> MAC -> UUID
60−makeUUID time clock mac =
61− buildFromBytes 1 /-/ tLow /-/ tMid /-/ tHigh /-/ clock /-/ (MACSource mac)
60+makeUUID time clock mac' =
61+ buildFromBytes 1 /-/ tLow /-/ tMid /-/ tHigh /-/ clock /-/ (MACSource mac')
6262 where tLow = (fromIntegral time) :: Word32
6363 tMid = (fromIntegral (time `shiftR` 32)) :: Word16
6464 tHigh = (fromIntegral (time `shiftR` 48)) :: Word16
@@ -75,17 +75,17 @@ type instance ByteSink MACSource g = Takes3Bytes (Takes3Bytes g)
7575stepTime :: IO (Maybe (MAC, Word16, Word64))
7676stepTime = do
7777 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
78− modifyMVar state $ \s@(State mac c0 h0) ->
78+ modifyMVar state $ \s@(State mac' c0 h0) ->
7979 if h1 > h0
8080 then
81− return (State mac c0 h1, Just (mac, c0, h1))
81+ return (State mac' c0 h1, Just (mac', c0, h1))
8282 else
8383 let
8484 c1 = succ c0
8585 in if c1 <= 0x3fff -- when clock is initially randomized,
8686 -- then this test will need to change
8787 then
88− return (State mac c1 h1, Just (mac, c1, h1))
88+ return (State mac' c1 h1, Just (mac', c1, h1))
8989 else
9090 return (s, Nothing)
9191
@@ -94,16 +94,17 @@ stepTime = do
9494state :: MVar State
9595state = unsafePerformIO $ do
9696 h0 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
97− mac <- getMac
98− newMVar $ State mac 0 h0 -- the 0 should be a random number
97+ mac' <- getMac
98+ newMVar $ State mac' 0 h0 -- the 0 should be a random number
9999
100100-- SysMAC.mac can fail on some machines.
101101-- In those cases we fake it with a random
102102-- 6 bytes seed.
103103getMac :: IO MAC
104104getMac =
105− SysMAC.mac >>= \macM ->
106− case macM of
105+ getNetworkInterfaces >>=
106+ return . listToMaybe . filter (minBound /=) . map mac >>=
107+ \macM -> case macM of
107108 Just m -> return m
108109 Nothing -> randomMac
109110
@@ -112,7 +113,7 @@ randomMac =
112113 -- I'm too lazy to thread through
113114 -- the random state ...
114115 MAC
115− <$> R.randomIO
116+ <$> (R.randomIO >>= return . (1 .|.)) -- We must set the multicast bit to True. See section 4.5 of the RFC.
116117 <*> R.randomIO
117118 <*> R.randomIO
118119 <*> R.randomIO
uuid.cabal+2−1
@@ -30,7 +30,8 @@ Library
3030 binary >= 0.4 && < 0.8,
3131 bytestring >= 0.9 && < 1.1,
3232 cryptohash >= 0.7 && < 0.11,
33− maccatcher >= 1.0 && < 2.2,
33+ hashable == 1.2.*,
34+ network-info == 0.2.*,
3435 random >= 1.0.1 && < 1.1,
3536 time >= 1.1 && < 1.5
3637