Skip to content

Commit 654ee7a

Browse files
committed
feat: Add AsyncListener trait
Which allows us to expose the local address
1 parent bf5cf83 commit 654ee7a

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

examples/axum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl axum::serve::Listener for Listener {
4747
}
4848
}
4949
fn local_addr(&self) -> io::Result<Self::Addr> {
50-
self.0.listener().local_addr()
50+
self.0.local_addr()
5151
}
5252
}
5353

src/accept.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ where
9090
current: first_future,
9191
}
9292
}
93+
94+
///An AsyncListener that can also report its local address
95+
pub trait AsyncListener: AsyncAccept {
96+
/// The local address of the listener, if available.
97+
fn local_addr(&self) -> Result<Self::Address, Self::Error>;
98+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ where
247247
pub fn listener(&self) -> &A {
248248
&self.listener
249249
}
250+
251+
/// Get the local address of the underlying listener
252+
pub fn local_addr(&self) -> Result<A::Address, A::Error> where A: AsyncListener {
253+
self.listener.local_addr()
254+
}
250255
}
251256

252257
impl<A, T> Stream for TlsListener<A, T>

src/net.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::AsyncAccept;
1+
use super::{AsyncAccept, AsyncListener};
22
use std::io;
33
use std::pin::Pin;
44
use std::task::{Context, Poll};
@@ -24,6 +24,14 @@ impl AsyncAccept for TcpListener {
2424
}
2525
}
2626

27+
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
28+
impl AsyncListener for TcpListener {
29+
#[inline]
30+
fn local_addr(&self) -> Result<Self::Address, Self::Error> {
31+
TcpListener::local_addr(self)
32+
}
33+
}
34+
2735
#[cfg(unix)]
2836
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
2937
impl AsyncAccept for UnixListener {
@@ -42,3 +50,11 @@ impl AsyncAccept for UnixListener {
4250
}
4351
}
4452
}
53+
54+
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
55+
impl AsyncListener for UnixListener {
56+
#[inline]
57+
fn local_addr(&self) -> Result<Self::Address, Self::Error> {
58+
UnixListener::local_addr(self)
59+
}
60+
}

0 commit comments

Comments
 (0)