Skip to content

Commit 1494877

Browse files
committed
Add BinarySize class for structures whose serializations are of a bounded (min,max) size.
1 parent 2e3a3a5 commit 1494877

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

System/TPFS/Device.hs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{-# LANGUAGE MultiParamTypeClasses #-}
22

3-
-- | Defines a readable and writable random-access device class.
4-
module System.TPFS.Device (Address, Device(..), Interleave(..)) where
3+
-- | Defines a readable and writable random-access device class, and some
4+
-- supporting classes.
5+
module System.TPFS.Device (Address, Device(..), Interleave(..), BinarySize(..)) where
56

67
import Control.Applicative
8+
import Data.Binary
79
import Data.ByteString.Lazy (ByteString,hGet,hPut)
810
import qualified Data.ByteString.Lazy as B
911
import Data.Word
@@ -53,3 +55,17 @@ hGetI h l
5355

5456
instance Interleave IO where
5557
interleave = unsafeInterleaveIO
58+
59+
class Binary a => BinarySize a where
60+
-- | Returns a minimum and maximum size (in bytes) for an encoded object of
61+
-- this type. The actual value of the object itself should be discarded; this
62+
-- should be a constant.
63+
binarySize :: a -> (Integer, Integer)
64+
65+
instance BinarySize Word8 where binarySize = const (1,1)
66+
67+
instance BinarySize Word16 where binarySize = const (2,2)
68+
69+
instance BinarySize Word32 where binarySize = const (4,4)
70+
71+
instance BinarySize Word64 where binarySize = const (8,8)

0 commit comments

Comments
 (0)