Skip to content

Commit 5ce835a

Browse files
committed
feat(socket): req-socket tracing
1 parent 679edcb commit 5ce835a

File tree

6 files changed

+181
-133
lines changed

6 files changed

+181
-133
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

msg-common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ rand.workspace = true
1515
futures.workspace = true
1616
tokio.workspace = true
1717
tokio-util.workspace = true
18+
derive_more.workspace = true
19+
tracing.workspace = true

msg-common/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
time::SystemTime,
1010
};
1111

12+
use derive_more::{Deref, DerefMut};
1213
use futures::future::BoxFuture;
1314

1415
mod channel;
@@ -52,14 +53,17 @@ pub trait SocketAddrExt: Sized {
5253
}
5354

5455
impl SocketAddrExt for SocketAddr {
56+
#[inline]
5557
fn unspecified_v4() -> Self {
5658
Self::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
5759
}
5860

61+
#[inline]
5962
fn unspecified_v6() -> Self {
6063
Self::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0))
6164
}
6265

66+
#[inline]
6367
fn as_unspecified(&self) -> Self {
6468
match self {
6569
Self::V4(_) => Self::unspecified_v4(),
@@ -75,6 +79,7 @@ pub trait IpAddrExt: Sized {
7579
}
7680

7781
impl IpAddrExt for IpAddr {
82+
#[inline]
7883
fn as_localhost(&self) -> Self {
7984
match self {
8085
Self::V4(_) => Self::V4(Ipv4Addr::LOCALHOST),
@@ -91,18 +96,21 @@ impl IdBase58 {
9196
const ALPHABET: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
9297

9398
/// Generates a new random Base58 short ID.
99+
#[inline]
94100
pub fn new() -> Self {
95101
let raw: u64 = rand::random();
96102
Self::to_base58(raw)
97103
}
98104

99105
/// Generates a new random Base58 short ID using the given RNG.
106+
#[inline]
100107
pub fn new_with_rng<T: Rng>(rng: &mut T) -> Self {
101108
let raw: u64 = rng.r#gen();
102109
Self::to_base58(raw)
103110
}
104111

105112
/// Converts the given u64 to a Base58 short ID.
113+
#[inline]
106114
pub fn to_base58(mut x: u64) -> Self {
107115
let mut out = [b'1'; 6]; // '1' is the first in base58
108116

@@ -127,3 +135,34 @@ impl std::fmt::Display for IdBase58 {
127135
f.write_str(unsafe { core::str::from_utf8_unchecked(&self.0) })
128136
}
129137
}
138+
139+
/// A container with a [`tracing::Span`] attached that can be entered.
140+
#[derive(Debug, Clone, Deref, DerefMut)]
141+
pub struct Spanned<T> {
142+
#[deref]
143+
#[deref_mut]
144+
pub inner: T,
145+
pub span: tracing::Span,
146+
}
147+
148+
impl<T> Spanned<T> {
149+
#[inline]
150+
pub fn new(inner: T) -> Self {
151+
Self { inner, span: tracing::Span::current() }
152+
}
153+
154+
#[inline]
155+
pub fn new_with_span(inner: T, span: tracing::Span) -> Self {
156+
Self { inner, span }
157+
}
158+
159+
#[inline]
160+
pub fn into_parts(self) -> (T, tracing::Span) {
161+
(self.inner, self.span)
162+
}
163+
164+
#[inline]
165+
pub fn into_inner(self) -> T {
166+
self.inner
167+
}
168+
}

0 commit comments

Comments
 (0)