+-spec to_bin(t()) ->
+ binary().
+to_bin(
+ ?T
+ { path = Path
+ , value = Value
+ , timestamp = Timestamp
+ }
+) ->
+ PathBin = bin_join(Path, <<".">>),
+ ValueBin = integer_to_binary(Value),
+ TimestampInt = timestamp_to_integer(Timestamp),
+ TimestampBin = integer_to_binary(TimestampInt),
+ <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
+
+%% ============================================================================
+%% Helpers
+%% ============================================================================
+
+-spec bin_join([binary()], binary()) ->
+ binary().
+bin_join([] , <<_/binary>> ) -> <<>>;
+bin_join([<<B/binary>> | []] , <<_/binary>> ) -> B;
+bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
+ BinsBin = bin_join(Bins, Sep),
+ <<B/binary, Sep/binary, BinsBin/binary>>.
+
+-spec timestamp_to_integer(erlang:timestamp()) ->
+ non_neg_integer().
+timestamp_to_integer({Megaseconds, Seconds, _}) ->
+ Megaseconds * 1000000 + Seconds.
+