diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-07-04 04:47:13 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-07-04 04:47:13 +0400 |
commit | 8f9796f613a844abd3e5018eba8d33eae7a278e5 (patch) | |
tree | 0d7c9cc9b1a3017d5975d9f2750a8356422d908e /src/Data/Torrent.hs | |
parent | a34f903abae190a7f3d6b7b469cf1911164dd451 (diff) |
+ Add Torrent Hashable instance.
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index bdd38630..8e6f9088 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -50,7 +50,8 @@ module Data.Torrent | |||
50 | 50 | ||
51 | #if defined (TESTING) | 51 | #if defined (TESTING) |
52 | -- * Internal | 52 | -- * Internal |
53 | , hash, hashlazy | 53 | , Data.Torrent.hash |
54 | , Data.Torrent.hashlazy | ||
54 | #endif | 55 | #endif |
55 | ) where | 56 | ) where |
56 | 57 | ||
@@ -71,6 +72,7 @@ import qualified Data.ByteString.Lazy as Lazy | |||
71 | import qualified Data.ByteString.Lazy.Builder as B | 72 | import qualified Data.ByteString.Lazy.Builder as B |
72 | import qualified Data.ByteString.Lazy.Builder.ASCII as B | 73 | import qualified Data.ByteString.Lazy.Builder.ASCII as B |
73 | import qualified Data.List as L | 74 | import qualified Data.List as L |
75 | import Data.Hashable as Hashable | ||
74 | import Data.Text (Text) | 76 | import Data.Text (Text) |
75 | import Data.Serialize as S hiding (Result) | 77 | import Data.Serialize as S hiding (Result) |
76 | import Text.PrettyPrint | 78 | import Text.PrettyPrint |
@@ -127,6 +129,9 @@ data Torrent = Torrent { | |||
127 | -- the encrypted SHA-1 hash of the info dictionary). | 129 | -- the encrypted SHA-1 hash of the info dictionary). |
128 | } deriving (Show, Eq) | 130 | } deriving (Show, Eq) |
129 | 131 | ||
132 | instance Hashable Torrent where | ||
133 | hash = Hashable.hash . tInfoHash | ||
134 | |||
130 | {- note that info hash is actually reduntant field | 135 | {- note that info hash is actually reduntant field |
131 | but it's better to keep it here to avoid heavy recomputations | 136 | but it's better to keep it here to avoid heavy recomputations |
132 | -} | 137 | -} |
@@ -377,7 +382,7 @@ checkPiece :: ContentInfo -> Int -> ByteString -> Bool | |||
377 | checkPiece ci ix piece @ (PS _ off si) | 382 | checkPiece ci ix piece @ (PS _ off si) |
378 | | traceShow (ix, off, si) True | 383 | | traceShow (ix, off, si) True |
379 | = B.length piece == ciPieceLength ci | 384 | = B.length piece == ciPieceLength ci |
380 | && hash piece == InfoHash (pieceHash ci ix) | 385 | && C.hash piece == pieceHash ci ix |
381 | 386 | ||
382 | -- | Read and decode a .torrent file. | 387 | -- | Read and decode a .torrent file. |
383 | fromFile :: FilePath -> IO Torrent | 388 | fromFile :: FilePath -> IO Torrent |
@@ -395,6 +400,9 @@ fromFile filepath = do | |||
395 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } | 400 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } |
396 | deriving (Eq, Ord) | 401 | deriving (Eq, Ord) |
397 | 402 | ||
403 | instance Hashable InfoHash where | ||
404 | hash = Hashable.hash . getInfoHash | ||
405 | |||
398 | instance BEncodable InfoHash where | 406 | instance BEncodable InfoHash where |
399 | toBEncode = toBEncode . getInfoHash | 407 | toBEncode = toBEncode . getInfoHash |
400 | fromBEncode be = InfoHash <$> fromBEncode be | 408 | fromBEncode be = InfoHash <$> fromBEncode be |