Skip to content

Commit

Permalink
Provide helpers for NaN and ±infinity packing (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexmag committed Dec 29, 2020
1 parent 3b93d43 commit 8486a10
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/msgpax.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ defmodule Msgpax do
`false` | boolean | `false`
`-1` | integer | `-1`
`1.25` | float | `1.25`
*N/A* | NaN | `Msgpax.NaN`<sup>1</sup>
*N/A* | +infinity | `Msgpax.Infinity`<sup>1</sup>
*N/A* | -infinity | `Msgpax.NegInfinity`<sup>1</sup>
*N/A*<sup>1</sup> | NaN | `Msgpax.NaN`<sup>2</sup>
*N/A*<sup>1</sup> | +infinity | `Msgpax.Infinity`<sup>2</sup>
*N/A*<sup>1</sup> | -infinity | `Msgpax.NegInfinity`<sup>2</sup>
`:ok` | string | `"ok"`
`Atom` | string | `"Elixir.Atom"`
`"str"` | string | `"str"`
`"\xFF\xFF"` | string | `"\xFF\xFF"`
`#Msgpax.Bin<"\xFF">` | binary | `"\xFF"`<sup>2</sup>
`#Msgpax.Bin<"\xFF">` | binary | `"\xFF"`<sup>3</sup>
`%{foo: "bar"}` | map | `%{"foo" => "bar"}`
`[foo: "bar"]` | map | `%{"foo" => "bar"}`
`[1, true]` | array | `[1, true]`
`#Msgpax.Ext<4, "02:12">` | extension | `#Msgpax.Ext<4, "02:12">`
`#DateTime<2017-12-06 00:00:00Z>` | extension | `#DateTime<2017-12-06 00:00:00Z>`
<sup>1</sup>NaN and ±infinity are not enabled by default. See `unpack/2` for for more information.
<sup>1</sup>`Msgpax.Packer` provides helper functions to facilitate the serialization of natively unsupported data types.
<sup>2</sup>To deserialize back to `Msgpax.Bin` structs see the `unpack/2` options.
<sup>2</sup>NaN and ±infinity are not enabled by default. See `unpack/2` for for more information.
<sup>3</sup>To deserialize back to `Msgpax.Bin` structs see the `unpack/2` options.
"""

alias __MODULE__.Packer
Expand Down
18 changes: 18 additions & 0 deletions lib/msgpax/packer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ defprotocol Msgpax.Packer do
It returns an iodata result.
"""
def pack(term)

@doc """
Returns serizalied NaN in 64-bit format.
"""
Kernel.def(pack_nan(), do: <<0xCB, -1::64>>)

require Bitwise

@doc """
Returns serizalied infinity in 64-bit format.
"""
Kernel.def pack_infinity(:positive) do
<<0xCB, 0::1, -1::11, 0::52>>
end

Kernel.def pack_infinity(:negative) do
<<0xCB, -1::12, 0::52>>
end
end

defimpl Msgpax.Packer, for: Atom do
Expand Down
4 changes: 4 additions & 0 deletions test/msgpax_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ defmodule MsgpaxTest do

assert Msgpax.unpack(packed, nonfinite_floats: true) == {:ok, value}
end

assert Msgpax.Packer.pack_nan() == <<203, -1::64>>
assert Msgpax.Packer.pack_infinity(:positive) == <<203, 0x7FF0::16, 0::48>>
assert Msgpax.Packer.pack_infinity(:negative) == <<203, 0xFFF0::16, 0::48>>
end

test "positive fixint" do
Expand Down

0 comments on commit 8486a10

Please sign in to comment.