@@ -13,10 +13,13 @@
13module Data.UUID.Internal
13module Data.UUID.Internal
18 ,nodeToList
19 ,nodeToList
19 ,listToNode
20 ,listToNode
20 ,fromString
23 ,fromString
22 ,versionMask
25 ,versionMask
@@ -47,6 +50,7 @@ import Foreign.Storable
47import Data.Binary
50import Data.Binary
48import Data.Binary.Put
51import Data.Binary.Put
49import Data.Binary.Get
52import Data.Binary.Get
53+import qualified Data.ByteString.Lazy as Lazy
51import System.Random
55import System.Random
@@ -71,11 +75,16 @@ data UUID = UUID
71 ,uuid_node :: SLOT(Node)
75 ,uuid_node :: SLOT(Node)
72 } deriving (Eq, Ord, Typeable)
76 } deriving (Eq, Ord, Typeable)
74−-- |Returns true if the passed-in UUID is the null UUID.
78+-- |Returns true if the passed-in UUID is the 'nil' UUID.
75null :: UUID -> Bool
79null :: UUID -> Bool
76null (UUID 0 0 0 0 0 node) | nullNode node = True
80null (UUID 0 0 0 0 0 node) | nullNode node = True
77null _ = False
81null _ = False
83+-- |The nil UUID, as defined in RFC 4122.
84+-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
86+nil = UUID 0 0 0 0 0 (Node 0 0 0 0 0 0)
79instance Random UUID where
88instance Random UUID where
80 random g = let (timeLow, g1) = randomBoundedIntegral g
89 random g = let (timeLow, g1) = randomBoundedIntegral g
81 (timeMid, g2) = randomBoundedIntegral g1
90 (timeMid, g2) = randomBoundedIntegral g1
@@ -220,6 +229,16 @@ instance Binary Node where
220 return $ Node w1 w2 w3 w4 w5 w6
229 return $ Node w1 w2 w3 w4 w5 w6
232+-- |Extract a UUID from a 'ByteString'.
233+-- The argument must be 16 bytes long, otherwise 'nil' is returned.
234+fromByteString :: Lazy.ByteString -> UUID
235+fromByteString bs | Lazy.length bs == 16 = decode bs
238+-- |Encode a UUID into a 'ByteString' in network order.
239+toByteString :: UUID -> Lazy.ByteString
240+toByteString uu = encode uu
223-- My goal with this instance was to make it work just enough to do what
242-- My goal with this instance was to make it work just enough to do what
224-- I want when used with the HStringTemplate library.
243-- I want when used with the HStringTemplate library.
225instance Data UUID where
244instance Data UUID where