~singpolyma/tokyocabinet-haskell

ref: 092c4e9f28ad06b2bf8bea2b55229493f6aef3a8 tokyocabinet-haskell/Database/TokyoCabinet/FDB/Key.hs -rw-r--r-- 1.2 KiB
092c4e9fStephen Paul Weber Merge branch 'master' of https://github.com/athanclark/tokyocabinet-haskell 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module Database.TokyoCabinet.FDB.Key (Key(..), ID(..)) where

import Database.TokyoCabinet.FDB.C (ID(..), unID)

import Data.Int
import Data.Word

class Key a where
    toID   :: a -> ID
    fromID :: ID -> a

instance Key Int where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Int8 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Int16 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Int32 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Int64 where
    toID = ID
    fromID = fromIntegral . unID

instance Key Word8 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Word16 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Word32 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key Word64 where
    toID = ID . fromIntegral
    fromID = fromIntegral . unID

instance Key ID where
    toID = id
    fromID = id

instance Key String where
    toID "min"  = IDMIN
    toID "max"  = IDMAX
    toID "prev" = IDPREV
    toID "next" = IDNEXT
    toID idstr  = ID (read idstr)
    fromID = show . unID