summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Data/Torrent/InfoHash.hs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index f120d46a..a5ba3786 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
1module Data.Torrent.InfoHash 2module Data.Torrent.InfoHash
2 ( InfoHash (getInfoHash) 3 ( InfoHash (getInfoHash)
3 , addHashToURI 4 , addHashToURI
@@ -10,9 +11,12 @@ module Data.Torrent.InfoHash
10 ) where 11 ) where
11 12
12import Control.Applicative 13import Control.Applicative
13import Data.Foldable 14import Data.BEncode
14import Data.List as L
15import Data.Char 15import Data.Char
16import Data.List as L
17import Data.Foldable
18import Data.Map (Map)
19import qualified Data.Map as M
16import Data.ByteString (ByteString) 20import Data.ByteString (ByteString)
17import qualified Data.ByteString as B 21import qualified Data.ByteString as B
18import qualified Data.ByteString.Char8 as BC 22import qualified Data.ByteString.Char8 as BC
@@ -27,7 +31,7 @@ import Numeric
27 31
28-- | Exactly 20 bytes long SHA1 hash. 32-- | Exactly 20 bytes long SHA1 hash.
29newtype InfoHash = InfoHash { getInfoHash :: ByteString } 33newtype InfoHash = InfoHash { getInfoHash :: ByteString }
30 deriving (Eq, Ord) 34 deriving (Eq, Ord, BEncodable)
31 35
32instance Show InfoHash where 36instance Show InfoHash where
33 show = BC.unpack . ppHex 37 show = BC.unpack . ppHex
@@ -36,6 +40,14 @@ instance Serialize InfoHash where
36 put = putByteString . getInfoHash 40 put = putByteString . getInfoHash
37 get = InfoHash <$> getBytes 20 41 get = InfoHash <$> getBytes 20
38 42
43instance BEncodable a => BEncodable (Map InfoHash a) where
44 {-# SPECIALIZE instance BEncodable a => BEncodable (Map InfoHash a) #-}
45 fromBEncode b = M.mapKeys InfoHash <$> fromBEncode b
46 {-# INLINE fromBEncode #-}
47
48 toBEncode = toBEncode . M.mapKeys getInfoHash
49 {-# INLINE toBEncode #-}
50
39hash :: ByteString -> InfoHash 51hash :: ByteString -> InfoHash
40hash = InfoHash . C.hash 52hash = InfoHash . C.hash
41 53