Skip to content

Commit

Permalink
Serialize u128 TraceId as LE bytes (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouzukii authored Mar 30, 2021
1 parent c3efb83 commit db0c778
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tarpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Context {
/// same trace ID.
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TraceId(u128);
pub struct TraceId(#[cfg_attr(feature = "serde1", serde(with = "u128_serde"))] u128);

/// A 64-bit identifier of a span within a trace. The identifier is unique within the span's trace.
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
Expand Down Expand Up @@ -95,3 +95,22 @@ impl fmt::Display for SpanId {
Ok(())
}
}

#[cfg(feature = "serde1")]
mod u128_serde {
pub fn serialize<S>(u: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serde::Serialize::serialize(&u.to_le_bytes(), serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(u128::from_le_bytes(serde::Deserialize::deserialize(
deserializer,
)?))
}
}

0 comments on commit db0c778

Please sign in to comment.