diff --git a/bluer/src/l2cap.rs b/bluer/src/l2cap.rs index e07c00c..b1192c2 100644 --- a/bluer/src/l2cap.rs +++ b/bluer/src/l2cap.rs @@ -479,6 +479,22 @@ impl Socket { /// Establish a stream connection with a peer at the specified socket address. pub async fn connect(self, sa: SocketAddr) -> Result { self.connect_priv(sa).await?; + Ok(Stream::from_socket(self)) + } + + /// Assume that `bind()` has already been successfully called on this socket, and return a + /// [StreamListener]. + /// + /// Only use this function if the socket was created with [Socket::from_raw_fd]. + pub fn assume_bound(self) -> StreamListener { + StreamListener { socket: self } + } + + /// Assume that `connect()` has already successfully been called on this socket, and return a + /// [Stream]. + /// + /// Only use this function if the socket was created with [Socket::from_raw_fd]. + pub fn assume_connected(self) -> Stream { Stream::from_socket(self) } } @@ -509,6 +525,22 @@ impl Socket { self.connect_priv(sa).await?; Ok(SeqPacket { socket: self }) } + + /// Assume that `bind()` has already been successfully called on this socket, and return a + /// [SeqPacketListener]. + /// + /// Only use this function if the socket was created with [Socket::from_raw_fd]. + pub fn assume_bound(self) -> SeqPacketListener { + SeqPacketListener { socket: self } + } + + /// Assume that `connect()` has already successfully been called on this socket, and return a + /// [SeqPacket]. + /// + /// Only use this function if the socket was created with [Socket::from_raw_fd]. + pub fn assume_connected(self) -> SeqPacket { + SeqPacket { socket: self } + } } impl Socket { @@ -543,13 +575,13 @@ impl StreamListener { /// Accepts a new incoming connection from this listener. pub async fn accept(&self) -> Result<(Stream, SocketAddr)> { let (socket, sa) = self.socket.accept_priv().await?; - Ok((Stream::from_socket(socket)?, sa)) + Ok((Stream::from_socket(socket), sa)) } /// Polls to accept a new incoming connection to this listener. pub fn poll_accept(&self, cx: &mut Context) -> Poll> { let (socket, sa) = ready!(self.socket.poll_accept_priv(cx))?; - Poll::Ready(Ok((Stream::from_socket(socket)?, sa))) + Poll::Ready(Ok((Stream::from_socket(socket), sa))) } } @@ -574,8 +606,8 @@ pub struct Stream { impl Stream { /// Create Stream from Socket. - fn from_socket(socket: Socket) -> Result { - Ok(Self { socket, send_mtu: 0.into() }) + fn from_socket(socket: Socket) -> Self { + Self { socket, send_mtu: 0.into() } } /// Establish a stream connection with a peer at the specified socket address.