@@ -1,86 +1,85 @@
2−{-# LANGUAGE DeriveDataTypeable #-}
1+{-# LANGUAGE DeriveDataTypeable, TypeFamilies, CPP #-}
3−{-# LANGUAGE TypeFamilies #-}
4−{-# LANGUAGE ViewPatterns #-}
5−{-# LANGUAGE Unsafe #-}
6−{-# LANGUAGE DeriveLift #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8−{-# OPTIONS_HADDOCK hide #-}
11−-- Module : Data.UUID.Types.Internal
4+-- Module : Data.UUID
12−-- Copyright : (c) 2017-2018 Herbert Valerio Riedel
13−-- (c) 2008-2009, 2012 Antoine Latter
5+-- Copyright : (c) 2008-2009, 2012 Antoine Latter
14-- (c) 2009 Mark Lentczner
6-- (c) 2009 Mark Lentczner
16-- License : BSD-style
8-- License : BSD-style
18−-- Maintainer : hvr@gnu.org
10+-- Maintainer : aslatter@gmail.com
11+-- Stability : experimental
19-- Portability : portable
12-- Portability : portable
21−module Data.UUID.Types.Internal
14+module Data.UUID.Internal
22− ( UUID(..)
15+ (UUID(..)
25− , fromByteString
18+ ,fromByteString
26− , toByteString
19+ ,toByteString
27− , fromString
20+ ,fromString
28− , toString
21+ ,toString
31− , fromWords
22+ ,fromWords
32− , toWords
23+ ,toWords
36− , buildFromBytes
25+ ,buildFromBytes
37− , buildFromWords
26+ ,buildFromWords
38− , fromASCIIBytes
27+ ,fromASCIIBytes
39− , toASCIIBytes
28+ ,toASCIIBytes
40− , fromLazyASCIIBytes
29+ ,fromLazyASCIIBytes
41− , toLazyASCIIBytes
30+ ,toLazyASCIIBytes
42− , UnpackedUUID(..)
31+ ,UnpackedUUID(..)
47−import Prelude hiding (null)
36+import Prelude hiding (null)
38+import Control.Applicative ((<*>))
49−import Control.DeepSeq (NFData (..))
39+import Control.DeepSeq (NFData(..))
50−import Control.Monad (guard, liftM2)
40+import Control.Monad (liftM4, guard)
51−import Data.Bits
41+import Data.Functor ((<$>))
52−import Data.Char
42+import Data.Char
53−import Data.Data
43+import Data.Bits
54−import Data.Hashable
44+import Data.Hashable
55−import Data.List (elemIndices)
46+import Data.List (elemIndices)
56−import Foreign.Ptr (Ptr)
47+import Foreign.Ptr (Ptr)
49+#if MIN_VERSION_base(4,0,0)
52+import Data.Generics.Basics
58−import Foreign.Storable
55+import Foreign.Storable
60−import Data.Binary
58+import Data.Binary.Put
61−import Data.Binary.Get
59+import Data.Binary.Get
62−import Data.Binary.Put
63−import qualified Data.ByteString as B
60+import qualified Data.ByteString as B
64−import qualified Data.ByteString.Internal as BI
61+import qualified Data.ByteString.Internal as BI
65−import qualified Data.ByteString.Lazy as BL
62+import qualified Data.ByteString.Lazy as BL
66−import qualified Data.ByteString.Unsafe as BU
63+import qualified Data.ByteString.Unsafe as BU
67−import Data.Text (Text)
64+
68−import qualified Data.Text.Encoding as T
70−import Data.UUID.Types.Internal.Builder
65+import Data.UUID.Builder
71−
66+import Data.Word.Util
72−import System.Random (Random (..), uniform)
67+
73−import System.Random.Stateful (Uniform (..), uniformWord64)
68+import System.Random
75−import Language.Haskell.TH.Syntax (Lift)
70+
76−
71+-- |The UUID type. A 'Random' instance is provided which produces
77−-- | Type representing <https://en.wikipedia.org/wiki/UUID Universally Unique Identifiers (UUID)> as specified in
72+-- version 4 UUIDs as specified in RFC 4122. The 'Storable' and
78−-- <http://tools.ietf.org/html/rfc4122 RFC 4122>.
73+-- 'Binary' instances are compatible with RFC 4122, storing the fields
79−data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
74+-- in network order as 16 bytes.
77+ {-# UNPACK #-} !Word32
78+ {-# UNPACK #-} !Word32
79+ {-# UNPACK #-} !Word32
80+ {-# UNPACK #-} !Word32
80− deriving (Eq, Ord)
81+ deriving (Eq, Ord, Typeable)
82− Prior to uuid-types-1.0.4:
83− !Word32 !Word32 !Word32 !Word32
84 Other representations that we tried are:
83 Other representations that we tried are:
85 Mimic V1 structure: !Word32 !Word16 !Word16 !Word16
84 Mimic V1 structure: !Word32 !Word16 !Word16 !Word16
86 !Word8 !Word8 !Word8 !Word8 !Word8 !Word8
85 !Word8 !Word8 !Word8 !Word8 !Word8 !Word8
@@ -92,75 +91,40 @@ data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
92 None was as fast, overall, as the representation used here.
91 None was as fast, overall, as the representation used here.
95−-- | Convert a 'UUID' into a sequence of 'Word32' values.
94+-- | Covert a 'UUID' into a sequence of 'Word32' values.
96−-- Useful for when you need to serialize a UUID and
95+-- Usefull for when you need to serialize a UUID and
97-- neither 'Storable' nor 'Binary' are appropriate.
96-- neither 'Storable' nor 'Binary' are appropriate.
98−--
97+-- Introduced in version 1.2.2.
99−-- >>> toWords <$> fromString "550e8400-e29b-41d4-a716-446655440000"
100−-- Just (1427014656,3801825748,2803254374,1430519808)
102−-- See also 'toWords64'.
104−-- /Since: @uuid-1.2.2@/
107toWords :: UUID -> (Word32, Word32, Word32, Word32)
98toWords :: UUID -> (Word32, Word32, Word32, Word32)
108−toWords (UUID w12 w34) = (w1, w2, w3, w4)
99+toWords (UUID w1 w2 w3 w4) = (w1, w2, w3, w4)
110− w1 = fromIntegral (w12 `unsafeShiftR` 32)
111− w2 = fromIntegral w12
112− w3 = fromIntegral (w34 `unsafeShiftR` 32)
113− w4 = fromIntegral w34
115-- | Create a 'UUID' from a sequence of 'Word32'. The
101-- | Create a 'UUID' from a sequence of 'Word32'. The
116−-- inverse of 'toWords'. Useful when you need a total
102+-- opposite of 'toWords'. Useful when you need a total
117-- function for constructing 'UUID' values.
103-- function for constructing 'UUID' values.
118−--
104+-- Introduced in version 1.2.2.
119−-- See also 'fromWords64'.
121−-- /Since: @uuid-1.2.2@/
124fromWords :: Word32 -> Word32 -> Word32 -> Word32 -> UUID
105fromWords :: Word32 -> Word32 -> Word32 -> Word32 -> UUID
125−fromWords w1 w2 w3 w4 = UUID (w32to64 w1 w2) (w32to64 w3 w4)
106+fromWords = UUID
127−-- | Convert a 'UUID' into a pair of 'Word64's.
129−-- >>> toWords64 <$> fromString "550e8400-e29b-41d4-a716-446655440000"
130−-- Just (6128981282234515924,12039885860129472512)
132−-- See also 'toWords'.
135−toWords64 :: UUID -> (Word64, Word64)
136−toWords64 (UUID w12 w34) = (w12,w34)
138−-- | Create a 'UUID' from a pair of 'Word64's.
140−-- Inverse of 'toWords64'. See also 'fromWords'.
143−fromWords64 :: Word64 -> Word64 -> UUID
146data UnpackedUUID =
108data UnpackedUUID =
147 UnpackedUUID {
109 UnpackedUUID {
148− time_low :: Word32 -- 0-3
110+ time_low :: Word32 -- 0-3
149− , time_mid :: Word16 -- 4-5
111+ , time_mid :: Word16 -- 4-5
150 , time_hi_and_version :: Word16 -- 6-7
112 , time_hi_and_version :: Word16 -- 6-7
151− , clock_seq_hi_res :: Word8 -- 8
113+ , clock_seq_hi_res :: Word8 -- 8
152− , clock_seq_low :: Word8 -- 9
114+ , clock_seq_low :: Word8 -- 9
153− , node_0 :: Word8
115+ , node_0 :: Word8
154− , node_1 :: Word8
116+ , node_1 :: Word8
155− , node_2 :: Word8
117+ , node_2 :: Word8
156− , node_3 :: Word8
118+ , node_3 :: Word8
157− , node_4 :: Word8
119+ , node_4 :: Word8
158− , node_5 :: Word8
120+ , node_5 :: Word8
160 deriving (Read, Show, Eq, Ord)
122 deriving (Read, Show, Eq, Ord)
162unpack :: UUID -> UnpackedUUID
124unpack :: UUID -> UnpackedUUID
163−unpack (UUID w0 w1) = build /-/ w0 /-/ w1
125+unpack (UUID w0 w1 w2 w3) =
126+ build /-/ w0 /-/ w1 /-/ w2 /-/ w3
165 build x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF =
129 build x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF =
166 UnpackedUUID {
130 UnpackedUUID {
@@ -179,13 +143,13 @@ unpack (UUID w0 w1) = build /-/ w0 /-/ w1
180pack :: UnpackedUUID -> UUID
144pack :: UnpackedUUID -> UUID
181pack unpacked =
145pack unpacked =
182− makeFromBytes /-/ (time_low unpacked)
146+ makeFromBytes /-/ (time_low unpacked)
183− /-/ (time_mid unpacked)
147+ /-/ (time_mid unpacked)
184− /-/ (time_hi_and_version unpacked)
148+ /-/ (time_hi_and_version unpacked)
185− /-/ (clock_seq_hi_res unpacked)
149+ /-/ (clock_seq_hi_res unpacked)
186 /-/ (clock_seq_low unpacked)
150 /-/ (clock_seq_low unpacked)
187− /-/ (node_0 unpacked) /-/ (node_1 unpacked)
151+ /-/ (node_0 unpacked) /-/ (node_1 unpacked)
188− /-/ (node_2 unpacked) /-/ (node_3 unpacked)
152+ /-/ (node_2 unpacked) /-/ (node_3 unpacked)
189 /-/ (node_4 unpacked) /-/ (node_5 unpacked)
153 /-/ (node_4 unpacked) /-/ (node_5 unpacked)
@@ -195,26 +159,15 @@ pack unpacked =
196-- |Build a Word32 from four Word8 values, presented in big-endian order
160-- |Build a Word32 from four Word8 values, presented in big-endian order
197word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
161word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
198−word a b c d = (fromIntegral a `unsafeShiftL` 24)
162+word a b c d = (fromIntegral a `shiftL` 24)
199− .|. (fromIntegral b `unsafeShiftL` 16)
163+ .|. (fromIntegral b `shiftL` 16)
200− .|. (fromIntegral c `unsafeShiftL` 8)
164+ .|. (fromIntegral c `shiftL` 8)
201− .|. (fromIntegral d )
165+ .|. (fromIntegral d )
203−-- |Extract a Word8 from a Word64. Bytes, high to low, are numbered from 7 to 0,
167+-- |Extract a Word8 from a Word32. Bytes, high to low, are numbered from 3 to 0,
204−byte :: Int -> Word64 -> Word8
168+byte :: Int -> Word32 -> Word8
205byte i w = fromIntegral (w `shiftR` (i * 8))
169byte i w = fromIntegral (w `shiftR` (i * 8))
207−-- |Build a Word16 from two Word8 values, presented in big-endian order.
208−w8to16 :: Word8 -> Word8 -> Word16
210− (w0 `unsafeShiftL` 8) .|. w1
212− w0 = fromIntegral w0s
213− w1 = fromIntegral w1s
215−-- | Construct 'Word64' from low/high 'Word32's
216−w32to64 :: Word32 -> Word32 -> Word64
217−w32to64 w0 w1 = (fromIntegral w0 `unsafeShiftL` 32) .|. (fromIntegral w1)
219-- |Make a UUID from sixteen Word8 values
172-- |Make a UUID from sixteen Word8 values
220makeFromBytes :: Word8 -> Word8 -> Word8 -> Word8
173makeFromBytes :: Word8 -> Word8 -> Word8 -> Word8
@@ -223,12 +176,16 @@ makeFromBytes :: Word8 -> Word8 -> Word8 -> Word8
223 -> Word8 -> Word8 -> Word8 -> Word8
176 -> Word8 -> Word8 -> Word8 -> Word8
225makeFromBytes b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
178makeFromBytes b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
226− = fromWords w0 w1 w2 w3
179+ = UUID w0 w1 w2 w3
227 where w0 = word b0 b1 b2 b3
180 where w0 = word b0 b1 b2 b3
228 w1 = word b4 b5 b6 b7
181 w1 = word b4 b5 b6 b7
229 w2 = word b8 b9 ba bb
182 w2 = word b8 b9 ba bb
230 w3 = word bc bd be bf
183 w3 = word bc bd be bf
185+-- |Make a UUID from four Word32 values
186+makeFromWords :: Word32 -> Word32 -> Word32 -> Word32 -> UUID
232-- |A Builder for constructing a UUID of a given version.
189-- |A Builder for constructing a UUID of a given version.
233buildFromBytes :: Word8
190buildFromBytes :: Word8
234 -> Word8 -> Word8 -> Word8 -> Word8
191 -> Word8 -> Word8 -> Word8 -> Word8
@@ -241,20 +198,20 @@ buildFromBytes v b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf =
241 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
198 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
242 b8' = b8 .&. 0x3f .|. 0x80
199 b8' = b8 .&. 0x3f .|. 0x80
244−-- |Build a UUID of a given version from 'Word32' values.
201+-- |Build a UUID of a given version from Word32 values.
245buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
202buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
246−buildFromWords v w0 w1 w2 w3 = fromWords w0 w1' w2' w3
203+buildFromWords v w0 w1 w2 w3 = makeFromWords w0 w1' w2' w3
247 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
204 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
248 w2' = w2 .&. 0x3fffffff .|. 0x80000000
205 w2' = w2 .&. 0x3fffffff .|. 0x80000000
251-- |Return the bytes that make up the UUID
208-- |Return the bytes that make up the UUID
252toList :: UUID -> [Word8]
209toList :: UUID -> [Word8]
253−toList (UUID w0 w1) =
210+toList (UUID w0 w1 w2 w3) =
254− [byte 7 w0, byte 6 w0, byte 5 w0, byte 4 w0,
211+ [byte 3 w0, byte 2 w0, byte 1 w0, byte 0 w0,
255− byte 3 w0, byte 2 w0, byte 1 w0, byte 0 w0,
212+ byte 3 w1, byte 2 w1, byte 1 w1, byte 0 w1,
256− byte 7 w1, byte 6 w1, byte 5 w1, byte 4 w1,
213+ byte 3 w2, byte 2 w2, byte 1 w2, byte 0 w2,
257− byte 3 w1, byte 2 w1, byte 1 w1, byte 0 w1]
214+ byte 3 w3, byte 2 w3, byte 1 w3, byte 0 w3]
259-- |Construct a UUID from a list of Word8. Returns Nothing if the list isn't
216-- |Construct a UUID from a list of Word8. Returns Nothing if the list isn't
260-- exactly sixteen bytes long
217-- exactly sixteen bytes long
@@ -275,10 +232,10 @@ null = (== nil)
275 -- null (UUID 0 0 0 0) = True
232 -- null (UUID 0 0 0 0) = True
276 -- null _ = False
233 -- null _ = False
278−-- |The 'nil' UUID, as defined in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
235+-- |The nil UUID, as defined in RFC 4122.
279−-- It is a UUID of all zeros. @'null' u@ /iff/ @'u' == 'nil'@.
236+-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
280nil :: UUID
237nil :: UUID
281−nil = UUID 0 0
238+nil = UUID 0 0 0 0
283-- |Extract a UUID from a 'ByteString' in network byte order.
240-- |Extract a UUID from a 'ByteString' in network byte order.
284-- The argument must be 16 bytes long, otherwise 'Nothing' is returned.
241-- The argument must be 16 bytes long, otherwise 'Nothing' is returned.
@@ -286,8 +243,6 @@ fromByteString :: BL.ByteString -> Maybe UUID
286fromByteString = fromList . BL.unpack
243fromByteString = fromList . BL.unpack
288-- |Encode a UUID into a 'ByteString' in network order.
245-- |Encode a UUID into a 'ByteString' in network order.
290−-- This uses the same encoding as the 'Binary' instance.
291toByteString :: UUID -> BL.ByteString
246toByteString :: UUID -> BL.ByteString
292toByteString = BL.pack . toList
247toByteString = BL.pack . toList
@@ -295,8 +250,9 @@ toByteString = BL.pack . toList
295-- The hyphens may not be omitted.
250-- The hyphens may not be omitted.
296-- Example:
251-- Example:
298−-- >>> fromString "c2cc10e1-57d6-4b6f-9899-38d972112d8c"
254+-- fromString \"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"
299−-- Just c2cc10e1-57d6-4b6f-9899-38d972112d8c
255+-- @
301-- Hex digits may be upper or lower-case.
257-- Hex digits may be upper or lower-case.
302fromString :: String -> Maybe UUID
258fromString :: String -> Maybe UUID
@@ -311,7 +267,7 @@ fromString' s0 = do
311 (w2, s3) <- hexWord s2
267 (w2, s3) <- hexWord s2
312 (w3, s4) <- hexWord s3
268 (w3, s4) <- hexWord s3
313 if s4 /= "" then Nothing
269 if s4 /= "" then Nothing
314− else Just $ fromWords w0 w1 w2 w3
270+ else Just $ UUID w0 w1 w2 w3
315 where hexWord :: String -> Maybe (Word32, String)
271 where hexWord :: String -> Maybe (Word32, String)
316 hexWord s = Just (0, s) >>= hexByte >>= hexByte
272 hexWord s = Just (0, s) >>= hexByte >>= hexByte
317 >>= hexByte >>= hexByte
273 >>= hexByte >>= hexByte
@@ -328,39 +284,21 @@ fromString' s0 = do
328-- | Convert a UUID into a hypenated string using lower-case letters.
284-- | Convert a UUID into a hypenated string using lower-case letters.
329-- Example:
285-- Example:
331−-- >>> toString <$> fromString "550e8400-e29b-41d4-a716-446655440000"
288+-- toString \<$\> fromString \"550e8400-e29b-41d4-a716-446655440000\"
332−-- Just "550e8400-e29b-41d4-a716-446655440000"
289+-- @
335toString :: UUID -> String
290toString :: UUID -> String
336−toString uuid = hexw0 w0 $ hexw1 w1 ""
291+toString (UUID w0 w1 w2 w3) = hexw w0 $ hexw' w1 $ hexw' w2 $ hexw w3 ""
337− where hexw0 :: Word64 -> String -> String
292+ where hexw :: Word32 -> String -> String
338− hexw0 w s = hexn w 60 : hexn w 56 : hexn w 52 : hexn w 48
339− : hexn w 44 : hexn w 40 : hexn w 36 : hexn w 32
340− : '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
293+ hexw w s = hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
341− : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0
294+ : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
344− hexw1 :: Word64 -> String -> String
345− hexw1 w s = '-' : hexn w 60 : hexn w 56 : hexn w 52 : hexn w 48
346− : '-' : hexn w 44 : hexn w 40 : hexn w 36 : hexn w 32
347− : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
348− : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0
351− hexn :: Word64 -> Int -> Char
352− hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
354− (w0,w1) = toWords64 uuid
296+ hexw' :: Word32 -> String -> String
297+ hexw' w s = '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
298+ : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
356−-- | If the passed in `Text` can be parsed as an ASCII representation of
300+ hexn :: Word32 -> Int -> Char
357−-- a `UUID`, it will be. The hyphens may not be omitted.
301+ hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
358−fromText :: Text -> Maybe UUID
359−fromText = fromASCIIBytes . T.encodeUtf8
361−-- | Convert a UUID into a hyphentated string using lower-case letters.
362−toText :: UUID -> Text
363−toText = T.decodeLatin1 . toASCIIBytes
365-- | Convert a UUID into a hyphentated string using lower-case letters, packed
303-- | Convert a UUID into a hyphentated string using lower-case letters, packed
366-- as ASCII bytes into `B.ByteString`.
304-- as ASCII bytes into `B.ByteString`.
@@ -454,9 +392,7 @@ fromASCIIBytes bs = do
455-- | Similar to `toASCIIBytes` except we produce a lazy `BL.ByteString`.
393-- | Similar to `toASCIIBytes` except we produce a lazy `BL.ByteString`.
456toLazyASCIIBytes :: UUID -> BL.ByteString
394toLazyASCIIBytes :: UUID -> BL.ByteString
457−toLazyASCIIBytes =
395+toLazyASCIIBytes = BL.fromStrict . toASCIIBytes
461-- | Similar to `fromASCIIBytes` except parses from a lazy `BL.ByteString`.
397-- | Similar to `fromASCIIBytes` except parses from a lazy `BL.ByteString`.
462fromLazyASCIIBytes :: BL.ByteString -> Maybe UUID
398fromLazyASCIIBytes :: BL.ByteString -> Maybe UUID
@@ -467,18 +403,27 @@ fromLazyASCIIBytes bs =
467-- Class Instances
403-- Class Instances
470−-- | This 'Random' instance produces __insecure__ version 4 UUIDs as
471−-- specified in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
472instance Random UUID where
406instance Random UUID where
473− random = uniform
407+ random g = (fromGenNext w0 w1 w2 w3 w4, g4)
408+ where (w0, g0) = next g
474 randomR _ = random -- range is ignored
413 randomR _ = random -- range is ignored
476−-- @since 1.0.4
415+-- |Build a UUID from the results of five calls to next on a StdGen.
477−instance Uniform UUID where
416+-- While next on StdGet returns an Int, it doesn't provide 32 bits of
478− uniformM gen = do
417+-- randomness. This code relies on at last 28 bits of randomness in the
479− w0 <- uniformWord64 gen
418+-- and optimizes its use so as to make only five random values, not six.
480− w1 <- uniformWord64 gen
419+fromGenNext :: Int -> Int -> Int -> Int -> Int -> UUID
420+fromGenNext w0 w1 w2 w3 w4 =
481− pure $ buildFromBytes 4 /-/ w0 /-/ w1
421+ buildFromBytes 4 /-/ (ThreeByte w0)
423+ /-/ w2 -- use all 4 bytes because we know the version
424+ -- field will "cover" the upper, non-random bits
483-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
428-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
484-- only returns 31 bits of randomness.
429-- only returns 31 bits of randomness.
@@ -486,29 +431,24 @@ type instance ByteSink ThreeByte g = Takes3Bytes g
486newtype ThreeByte = ThreeByte Int
431newtype ThreeByte = ThreeByte Int
487instance ByteSource ThreeByte where
432instance ByteSource ThreeByte where
488 f /-/ (ThreeByte w) = f b1 b2 b3
433 f /-/ (ThreeByte w) = f b1 b2 b3
489− where b1 = fromIntegral (w `unsafeShiftR` 16)
434+ where b1 = fromIntegral (w `shiftR` 16)
490− b2 = fromIntegral (w `unsafeShiftR` 8)
435+ b2 = fromIntegral (w `shiftR` 8)
491 b3 = fromIntegral w
436 b3 = fromIntegral w
493instance NFData UUID where
438instance NFData UUID where
494− rnf = flip seq ()
439+ rnf = rnf . toWords
496instance Hashable UUID where
441instance Hashable UUID where
497− hash (toWords -> (w0,w1,w2,w3)) =
442+ hash (UUID w0 w1 w2 w3) =
498 hash w0 `hashWithSalt` w1
443 hash w0 `hashWithSalt` w1
499 `hashWithSalt` w2
444 `hashWithSalt` w2
500 `hashWithSalt` w3
445 `hashWithSalt` w3
501− hashWithSalt s (toWords -> (w0,w1,w2,w3)) =
446+ hashWithSalt s (UUID w0 w1 w2 w3) =
502 s `hashWithSalt` w0
447 s `hashWithSalt` w0
503 `hashWithSalt` w1
448 `hashWithSalt` w1
504 `hashWithSalt` w2
449 `hashWithSalt` w2
505 `hashWithSalt` w3
450 `hashWithSalt` w3
507−-- | Pretty prints a 'UUID' (without quotation marks). See also 'toString'.
510−-- "00000000-0000-0000-0000-000000000000"
512instance Show UUID where
452instance Show UUID where
513 show = toString
453 show = toString
@@ -519,7 +459,7 @@ instance Read UUID where
519 Nothing -> []
459 Nothing -> []
520 Just u -> [(u,drop 36 noSpaces)]
460 Just u -> [(u,drop 36 noSpaces)]
522−-- | This 'Storable' instance uses the memory layout as described in <http://tools.ietf.org/html/rfc4122 RFC 4122>, but in contrast to the 'Binary' instance, __the fields are stored in host byte order__.
462+
523instance Storable UUID where
463instance Storable UUID where
524 sizeOf _ = 16
464 sizeOf _ = 16
525 alignment _ = 4
465 alignment _ = 4
@@ -556,10 +496,10 @@ instance Storable UUID where
556 pokeByteOff p (off+14) x9
496 pokeByteOff p (off+14) x9
557 pokeByteOff p (off+15) x10
497 pokeByteOff p (off+15) x10
559−-- | This 'Binary' instance is compatible with <http://tools.ietf.org/html/rfc4122 RFC 4122>, storing the fields in network order as 16 bytes.
560instance Binary UUID where
499instance Binary UUID where
561− put (UUID w0 w1) = putWord64be w0 >> putWord64be w1
500+ put (UUID w0 w1 w2 w3) =
562− get = liftM2 UUID getWord64be getWord64be
501+ putWord32be w0 >> putWord32be w1 >> putWord32be w2 >> putWord32be w3
502+ get = liftM4 UUID getWord32be getWord32be getWord32be getWord32be
565-- My goal with this instance was to make it work just enough to do what
505-- My goal with this instance was to make it work just enough to do what
@@ -570,6 +510,10 @@ instance Data UUID where
570 dataTypeOf _ = uuidType
510 dataTypeOf _ = uuidType
572uuidType :: DataType
512uuidType :: DataType
573−uuidType = mkNoRepType "Data.UUID.Types.UUID"
513+uuidType = mkNoRepType "Data.UUID.UUID"
515+#if !(MIN_VERSION_base(4,2,0))
516+mkNoRepType :: String -> DataType
517+mkNoRepType = mkNorepType
575−deriving instance Lift UUID