Skip to content

Commit 4c946af

Browse files
authored
feat(server): add AddrIncoming::from_listener constructor (#2439)
1 parent a602808 commit 4c946af

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/server/tcp.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ impl AddrIncoming {
3636
.set_nonblocking(true)
3737
.map_err(crate::Error::new_listen)?;
3838
let listener = TcpListener::from_std(std_listener).map_err(crate::Error::new_listen)?;
39+
AddrIncoming::from_listener(listener)
40+
}
41+
42+
/// Creates a new `AddrIncoming` binding to provided socket address.
43+
pub fn bind(addr: &SocketAddr) -> crate::Result<Self> {
44+
AddrIncoming::new(addr)
45+
}
46+
47+
/// Creates a new `AddrIncoming` from an existing `tokio::net::TcpListener`.
48+
pub fn from_listener(listener: TcpListener) -> crate::Result<Self> {
3949
let addr = listener.local_addr().map_err(crate::Error::new_listen)?;
4050
Ok(AddrIncoming {
4151
listener,
@@ -47,11 +57,6 @@ impl AddrIncoming {
4757
})
4858
}
4959

50-
/// Creates a new `AddrIncoming` binding to provided socket address.
51-
pub fn bind(addr: &SocketAddr) -> crate::Result<Self> {
52-
AddrIncoming::new(addr)
53-
}
54-
5560
/// Get the local address bound to this listener.
5661
pub fn local_addr(&self) -> SocketAddr {
5762
self.addr

0 commit comments

Comments
 (0)