-
Notifications
You must be signed in to change notification settings - Fork 80
Expose packing and unpacking primitives #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nh2
wants to merge
7
commits into
msgpack:master
Choose a base branch
from
nh2:expose-primitives
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d7273dc
Add documentation to all unpacking parsing functions
nh2 39cb344
Expose all unpacking parsing functions
nh2 1a9ebfc
Add documentation to IsByteString class
nh2 9fa0b9d
Add documentation to all packing functions
nh2 f6d646b
Expose all packing functions
nh2 4edbffb
Remove (a -> b) argument from Unpack.fromString.
nh2 d8e4d09
Remove unnecessary (. id) composition
nh2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -19,6 +19,11 @@ module Data.MessagePack.Pack ( | |
Packable(..), | ||
-- * Simple function to pack a Haskell value | ||
pack, | ||
-- * Packing primitives | ||
fromString, | ||
fromArray, | ||
fromPair, | ||
fromMap, | ||
) where | ||
|
||
import Blaze.ByteString.Builder | ||
|
@@ -106,24 +111,29 @@ cast :: (Storable a, Storable b) => a -> b | |
cast v = SIU.unsafePerformIO $ with v $ peek . castPtr | ||
|
||
instance Packable String where | ||
from = fromString encodeUtf8 B.length fromByteString | ||
from = fromString B.length fromByteString . encodeUtf8 | ||
|
||
instance Packable B.ByteString where | ||
from = fromString id B.length fromByteString | ||
from = fromString B.length fromByteString . id | ||
|
||
instance Packable BL.ByteString where | ||
from = fromString id (fromIntegral . BL.length) fromLazyByteString | ||
from = fromString (fromIntegral . BL.length) fromLazyByteString . id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
|
||
instance Packable T.Text where | ||
from = fromString T.encodeUtf8 B.length fromByteString | ||
from = fromString B.length fromByteString . T.encodeUtf8 | ||
|
||
instance Packable TL.Text where | ||
from = fromString TL.encodeUtf8 (fromIntegral . BL.length) fromLazyByteString | ||
from = fromString (fromIntegral . BL.length) fromLazyByteString . TL.encodeUtf8 | ||
|
||
fromString :: (s -> t) -> (t -> Int) -> (t -> Builder) -> s -> Builder | ||
fromString cnv lf pf str = | ||
let bs = cnv str in | ||
case lf bs of | ||
-- | @fromString lengthFun packFun array@: | ||
-- Transforms an string-like structure (e.g. String, Text) into | ||
-- a MessagePack string. | ||
-- | ||
-- `lengthFun` specifies how to obtain the length of the structure, | ||
-- `packFun` how to pack it. | ||
fromString :: (s -> Int) -> (s -> Builder) -> s -> Builder | ||
fromString lf pf str = | ||
case lf str of | ||
len | len <= 31 -> | ||
fromWord8 $ 0xA0 .|. fromIntegral len | ||
len | len < 0x10000 -> | ||
|
@@ -132,7 +142,7 @@ fromString cnv lf pf str = | |
len -> | ||
fromWord8 0xDB <> | ||
fromWord32be (fromIntegral len) | ||
<> pf bs | ||
<> pf str | ||
|
||
instance Packable a => Packable [a] where | ||
from = fromArray length (Monoid.mconcat . map from) | ||
|
@@ -172,6 +182,12 @@ instance (Packable a1, Packable a2, Packable a3, Packable a4, Packable a5, Packa | |
from = fromArray (const 9) f where | ||
f (a1, a2, a3, a4, a5, a6, a7, a8, a9) = from a1 <> from a2 <> from a3 <> from a4 <> from a5 <> from a6 <> from a7 <> from a8 <> from a9 | ||
|
||
-- | @fromArray lengthFun packFun array@: | ||
-- Transforms an array-like structure (e.g. tuple, list) into | ||
-- a MessagePack array. | ||
-- | ||
-- `lengthFun` specifies how to obtain the length of the structure, | ||
-- `packFun` how to pack it. | ||
fromArray :: (a -> Int) -> (a -> Builder) -> a -> Builder | ||
fromArray lf pf arr = do | ||
case lf arr of | ||
|
@@ -200,9 +216,16 @@ instance Packable v => Packable (IM.IntMap v) where | |
instance (Packable k, Packable v) => Packable (HM.HashMap k v) where | ||
from = fromMap HM.size (Monoid.mconcat . map fromPair . HM.toList) | ||
|
||
-- | Transforms tuple into a MessagePack pair. | ||
fromPair :: (Packable a, Packable b) => (a, b) -> Builder | ||
fromPair (a, b) = from a <> from b | ||
|
||
-- | @fromMap lengthFun packFun array@: | ||
-- Transforms an map-like structure (e.g. Map, HashMap) into | ||
-- a MessagePack map. | ||
-- | ||
-- `lengthFun` specifies how to obtain the length of the structure, | ||
-- `packFun` how to pack it. | ||
fromMap :: (a -> Int) -> (a -> Builder) -> a -> Builder | ||
fromMap lf pf m = | ||
case lf m of | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use id here seems unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, true! I did this transformation quite mechanical. Added fix to pull request.