on master help sign in
drop maccatcher dependency

V1 generation requires that the caller supply a MAC address to use

commit49c12383198503593ea10587b1cf7f1a68f82ef4 authorAntoine Latter <aslatter@gmail.com> dateSun 10 Jan 2010, 19:48 (17 years ago) parent1d8922b
4 files changed+22−16
Data/UUID/V1.hs+13−11 tests/BenchUUID.hs+5−1 tests/TestUUID.hs+1−1 uuid.cabal+3−3
Data/UUID/V1.hs+13−11
@@ -12,8 +12,12 @@
1212-- Portability : portable
1313--
1414-- RFC 4122 Version 1 UUID state machine.
15+--
16+-- Currently this is more useful as a reference. For
17+-- true machine-unique UUID generation see the
18+-- system-uuid package.
1519
16−module Data.UUID.V1(nextUUID)
20+module Data.UUID.V1(nextUUID, MAC(..))
1721where
1822
1923import Data.Time
@@ -25,9 +29,6 @@ import Data.IORef
2529import System.IO
2630import System.IO.Unsafe
2731
28−import qualified System.Info.MAC as SysMAC
29−import Data.MAC
30−
3132import Data.UUID.Builder
3233import Data.UUID.Internal
3334
@@ -36,14 +37,12 @@ import Data.UUID.Internal
3637-- Is generated according to the Version 1 UUID sepcified in
3738-- RFC 4122.
3839--
39−-- Returns nothing if the hardware MAC address could not
40−-- be discovered.
41−nextUUID :: IO (Maybe UUID)
42−nextUUID = do
40+-- Returns nothing if UUIDs are generated to quickly
41+nextUUID :: MAC -> IO (Maybe UUID)
42+nextUUID m = do
4343 res <- stepTime
44− mac <- SysMAC.mac
45− case (res, mac) of
46− (Just (c, t), Just m) -> return $ Just $ makeUUID t c m
44+ case res of
45+ Just (c, t) -> return $ Just $ makeUUID t c m
4746 _ -> return Nothing
4847
4948
@@ -59,6 +58,9 @@ instance ByteSource MACSource where
5958 z /-/ (MACSource (MAC a b c d e f)) = z a b c d e f
6059type instance ByteSink MACSource g = Takes3Bytes (Takes3Bytes g)
6160
61+-- | Machine address of computer generating UUIDs.
62+data MAC = MAC !Word8 !Word8 !Word8
63+ !Word8 !Word8 !Word8
6264
6365-- |Approximates the clock algorithm in RFC 4122, section 4.2
6466-- Isn't system wide or thread safe, nor does it properly randomize
tests/BenchUUID.hs+5−1
@@ -61,7 +61,7 @@ main = do
6161 bench "fromByteString" $ nf U.fromByteString b1
6262 ],
6363 bgroup "generation" [
64− bench "V1" $ nfIO U.nextUUID,
64+ bench "V1" $ nfIO (U.nextUUID testMAC),
6565 bench "V4" $ nfIO (randomUUID :: IO U.UUID),
6666 bench "V3" $ nf (U3.generateNamed U3.namespaceURL) n1,
6767 bench "V5" $ nf (U5.generateNamed U5.namespaceURL) n1
@@ -69,6 +69,10 @@ main = do
6969 bench "set making" $ nf Set.fromList uuids
7070 ]
7171
72+-- | MAC address to use in testing
73+testMAC :: U.MAC
74+testMAC = U.MAC 0 0 0 0 0 0
75+
7276-- 50 uuids, so tests can be repeatable
7377uuids :: [U.UUID]
7478uuids
tests/TestUUID.hs+1−1
@@ -138,7 +138,7 @@ prop_v5Valid = label "V5 valid" v5Valid
138138
139139main :: IO ()
140140main = do
141− v1s <- replicateM 100 U.nextUUID
141+ v1s <- replicateM 100 $ U.nextUUID $ U.MAC 0 0 0 0 0 0
142142 runTestText (putTextToHandle stderr False) (TestList [
143143 test_null,
144144 test_nil,
uuid.cabal+3−3
@@ -29,7 +29,7 @@ Flag test
2929 Default: False
3030
3131Library
32− Build-Depends: random, binary, bytestring, Crypto, maccatcher,
32+ Build-Depends: random, binary, bytestring, Crypto,
3333 time, base >=3, base < 5
3434
3535 Exposed-Modules:
@@ -54,7 +54,7 @@ Executable benchuuid
5454 Extensions: DeriveDataTypeable, CPP
5555 If flag(test)
5656 Buildable: True
57− Build-Depends: random, binary, bytestring, Crypto, maccatcher,
57+ Build-Depends: random, binary, bytestring, Crypto,
5858 time, base >=3, base < 5, containers,
5959 criterion >= 0.3 && < 0.5, parallel >= 2.1 && < 2.3,
6060 mersenne-random-pure64 >= 0.2 && < 0.3
@@ -71,7 +71,7 @@ Executable testuuid
7171
7272 If flag(test)
7373 Buildable: True
74− Build-Depends: random, binary, bytestring, Crypto, maccatcher,
74+ Build-Depends: random, binary, bytestring, Crypto,
7575 time, base >=3, base < 5,
7676 HUnit >=1.2 && < 1.3, QuickCheck >=1.2 && < 1.3
7777 Else