Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split everything up into seperate modules
Signed-off-by: Adam Smith <adam@tablethotels.com>
- Loading branch information
Adam Smith
committed
Jan 17, 2012
1 parent
224ddd7
commit eb4b61d
Showing
6 changed files
with
322 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-} | ||
|
||
module Database.CDB.Packable ( | ||
Packable, | ||
Unpackable, | ||
pack, | ||
unpack | ||
) where | ||
|
||
import Data.Array | ||
import Data.Bits | ||
import qualified Data.ByteString as ByteString | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as ByteString.Char8 | ||
import Data.Word | ||
|
||
-- |An instance of 'Packable' can be losslessly transformed into a 'ByteString'. | ||
class Packable k where | ||
pack :: k -> ByteString | ||
|
||
-- |An instance of 'Unpackable' can be losslessly transformed from a 'ByteString'. | ||
class Unpackable v where | ||
unpack :: ByteString -> v | ||
|
||
instance Packable ByteString where | ||
pack = id | ||
|
||
instance Unpackable ByteString where | ||
unpack = id | ||
|
||
instance Packable [Char] where | ||
pack = ByteString.Char8.pack | ||
|
||
instance Unpackable [Char] where | ||
unpack = ByteString.Char8.unpack | ||
|
||
instance Packable [Word8] where | ||
pack = ByteString.pack | ||
|
||
instance Unpackable [Word8] where | ||
unpack = ByteString.unpack | ||
|
||
instance Packable Word32 where | ||
pack n = ByteString.pack $ map fromIntegral | ||
[n .&. 0xFF, | ||
(n `shiftR` 8) .&. 0xFF, | ||
(n `shiftR` 16) .&. 0xFF, | ||
(n `shiftR` 24) .&. 0xFF] | ||
|
||
instance Packable (Array Word32 Word32) where | ||
pack t = pack $ ByteString.concat $ map pack $ elems t | ||
|
Oops, something went wrong.