From ea7947d3db0217f31dc507f930f3d9e6c6f437c0 Mon Sep 17 00:00:00 2001 From: Sam T Date: Wed, 24 Apr 2013 07:04:10 +0400 Subject: ~ One less orphan instance. --- src/Network/BitTorrent/PeerWire/Block.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/Network/BitTorrent/PeerWire/Block.hs') diff --git a/src/Network/BitTorrent/PeerWire/Block.hs b/src/Network/BitTorrent/PeerWire/Block.hs index a5bc4f32..8e4a1f24 100644 --- a/src/Network/BitTorrent/PeerWire/Block.hs +++ b/src/Network/BitTorrent/PeerWire/Block.hs @@ -4,12 +4,16 @@ module Network.BitTorrent.PeerWire.Block , defaultBlockSize , pieceIx, blockIx , blockRange, ixRange, isPiece + + , putInt, getInt ) where import Control.Applicative import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Int +import Data.Serialize + type BlockLIx = Int type PieceLIx = Int @@ -26,6 +30,24 @@ data BlockIx = BlockIx { , ixLength :: {-# UNPACK #-} !Int } deriving (Show, Eq) +getInt :: Get Int +getInt = fromIntegral <$> getWord32be +{-# INLINE getInt #-} + +putInt :: Putter Int +putInt = putWord32be . fromIntegral +{-# INLINE putInt #-} + +instance Serialize BlockIx where + {-# SPECIALIZE instance Serialize BlockIx #-} + get = BlockIx <$> getInt <*> getInt <*> getInt + {-# INLINE get #-} + + put ix = do putInt (ixPiece ix) + putInt (ixOffset ix) + putInt (ixLength ix) + {-# INLINE put #-} + data Block = Block { -- ^ Zero-based piece index. -- cgit v1.2.3