feat(server): add AddrIncoming::from_listener constructor (#2439)

This commit is contained in:
ty
2021-02-23 08:18:38 +08:00
committed by GitHub
parent a60280873b
commit 4c946af49c

View File

@@ -36,6 +36,16 @@ impl AddrIncoming {
.set_nonblocking(true) .set_nonblocking(true)
.map_err(crate::Error::new_listen)?; .map_err(crate::Error::new_listen)?;
let listener = TcpListener::from_std(std_listener).map_err(crate::Error::new_listen)?; let listener = TcpListener::from_std(std_listener).map_err(crate::Error::new_listen)?;
AddrIncoming::from_listener(listener)
}
/// Creates a new `AddrIncoming` binding to provided socket address.
pub fn bind(addr: &SocketAddr) -> crate::Result<Self> {
AddrIncoming::new(addr)
}
/// Creates a new `AddrIncoming` from an existing `tokio::net::TcpListener`.
pub fn from_listener(listener: TcpListener) -> crate::Result<Self> {
let addr = listener.local_addr().map_err(crate::Error::new_listen)?; let addr = listener.local_addr().map_err(crate::Error::new_listen)?;
Ok(AddrIncoming { Ok(AddrIncoming {
listener, listener,
@@ -47,11 +57,6 @@ impl AddrIncoming {
}) })
} }
/// Creates a new `AddrIncoming` binding to provided socket address.
pub fn bind(addr: &SocketAddr) -> crate::Result<Self> {
AddrIncoming::new(addr)
}
/// Get the local address bound to this listener. /// Get the local address bound to this listener.
pub fn local_addr(&self) -> SocketAddr { pub fn local_addr(&self) -> SocketAddr {
self.addr self.addr