|
1 | 1 | {-# LANGUAGE MultiParamTypeClasses #-}
|
2 | 2 |
|
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 |
5 | 6 |
|
6 | 7 | import Control.Applicative
|
| 8 | +import Data.Binary |
7 | 9 | import Data.ByteString.Lazy (ByteString,hGet,hPut)
|
8 | 10 | import qualified Data.ByteString.Lazy as B
|
9 | 11 | import Data.Word
|
@@ -53,3 +55,17 @@ hGetI h l
|
53 | 55 |
|
54 | 56 | instance Interleave IO where
|
55 | 57 | 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