@@ -95,25 +95,22 @@ uuidType = mkNorepType "Data.UUID.UUID"
9595-- Otherwise a UUID will be generated based on the current time and the
9696-- hardware MAC address, if available.
9797generate :: IO UUID
98−generate = do
99− fp <- mallocForeignPtrArray 16
100− withForeignPtr fp $ \p -> c_generate p
101− return $ U fp
98+generate = generateWrap c_generate
10299
103100-- |Create a new 'UUID'. If \/dev\/urandom is available, it will be used.
104101-- Otherwise a psuedorandom generator will be used.
105102generateRandom :: IO UUID
106−generateRandom = do
107− fp <- mallocForeignPtrArray 16
108− withForeignPtr fp $ \p -> c_generate_random p
109− return $ U fp
103+generateRandom = generateWrap c_generate_random
110104
111105-- |Create a new 'UUID'. The UUID will be generated based on the
112106-- current time and the hardware MAC address, if available.
113107generateTime :: IO UUID
114−generateTime = do
108+generateTime = generateWrap c_generate_time
109+
110+generateWrap :: (C_UUID -> IO ()) -> IO UUID
111+generateWrap gen = do
115112 fp <- mallocForeignPtrArray 16
116− withForeignPtr fp $ \p -> c_generate_time p
113+ withForeignPtr fp $ \p -> gen p
117114 return $ U fp
118115
119116-- |Returns 'True' if the passed-in 'UUID' is the null UUID.
@@ -136,7 +133,7 @@ fromString s = unsafePerformIO $ do
136133 fp <- mallocForeignPtrArray 16
137134 res <- withCAString s $ \chars ->
138135 withForeignPtr fp $ \p ->
139− c_read (castPtr chars) p
136+ c_read chars p
140137 case res of
141138 0 -> return . Just $ U fp
142139 _ -> return Nothing
@@ -145,33 +142,27 @@ fromString s = unsafePerformIO $ do
145142-- Hex digits occuring in the output will be either upper or
146143-- lower-case depending on system defaults and locale.
147144toString :: UUID -> String
148−toString (U fp) = unsafePerformIO $ do
149− chars <- mallocBytes 37
150− withForeignPtr fp $ \p -> c_show p chars
151− st <- peekCAString chars
152− free chars
153− return st
145+toString = toStringWrap c_show
154146
155147-- |Returns a 'String' representation of the passed in 'UUID'.
156148-- Hex digits occuring in the output will be lower-case.
157149toStringLower :: UUID -> String
158−toStringLower (U fp) = unsafePerformIO $ do
159− chars <- mallocBytes 37
160− withForeignPtr fp $ \p -> c_show_lower p chars
161− st <- peekCAString chars
162− free chars
163− return st
150+toStringLower = toStringWrap c_show_lower
164151
165152-- |Returns a 'String' representation of the passed in 'UUID'.
166153-- Hex digits occuring in the output will be upper-case.
167154toStringUpper :: UUID -> String
168−toStringUpper (U fp) = unsafePerformIO $ do
169− chars <- mallocBytes 37
170− withForeignPtr fp $ \p -> c_show_upper p chars
171− st <- peekCAString chars
172− free chars
173− return st
174−
155+toStringUpper = toStringWrap c_show_upper
156+
157+-- |The passed in function /f/ is given a the raw UUID data
158+-- and a 37-byte buffer to fill with the textual representation
159+-- of the UUID. The buffer is expected to be null-terminated
160+-- after /f/ is finished.
161+toStringWrap :: (C_UUID -> CString -> IO ()) -> UUID -> String
162+toStringWrap f (U fp) = unsafePerformIO $
163+ allocaBytes 37 $ \chars -> do
164+ withForeignPtr fp $ \p -> f p chars
165+ peekCAString chars
175166
176167-- FFI calls to do the work
177168