2424-define (ERLANG_TERM , 131 ).
2525-define (TERM_OPTION , [{enable_str ,true },{ext ,? MODULE },{allow_atom ,none }]).
2626
27+ % % @doc experimental
28+ -spec to_binary (term ()) -> binary ().
2729to_binary (Term ) ->
2830 msgpack :pack (Term , ? TERM_OPTION ).
2931
32+ % % @doc experimental
33+ -spec from_binary (binary ()) -> term ().
3034from_binary (Bin ) ->
3135 {ok , Term } = msgpack :unpack (Bin , ? TERM_OPTION ),
3236 Term .
@@ -35,6 +39,15 @@ from_binary(Bin) ->
3539 {ok , {Type :: byte (), Data :: binary ()}} |
3640 {error , any ()}.
3741pack_ext (Term , _Options ) ->
42+ % % there are still much space to improve:
43+ % % for example, pid() can be compressed much
44+ % % more by using msgpack integers.
45+ % % reference type is also bigger, because
46+ % % it uses four bytes per int, which includes
47+ % % four integers. Both types include node name
48+ % % which is atom including two bytes length.
49+ % % usually atom/string less than length 32 can
50+ % % coded as single byte indicating its length.
3851 {ok , {? ERLANG_TERM , erlang :term_to_binary (Term )}}.
3952
4053-spec unpack_ext (Type :: byte (), Data :: binary ()) ->
0 commit comments