Skip to content

Commit

Permalink
feat(net): add from_std for UdpSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Sep 26, 2024
1 parent 83228ac commit e66bbcb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compio-net/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compio-net"
version = "0.5.0"
version = "0.5.1"
description = "Networking IO for compio"
categories = ["asynchronous", "network-programming"]
keywords = ["async", "net"]
Expand Down
9 changes: 8 additions & 1 deletion compio-net/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{future::Future, io, net::SocketAddr};

use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_driver::impl_raw_fd;
use socket2::{Protocol, SockAddr, Type};
use socket2::{Protocol, SockAddr, Socket as Socket2, Type};

use crate::{Socket, ToSocketAddrsAsync};

Expand Down Expand Up @@ -116,6 +116,13 @@ impl UdpSocket {
.await
}

/// Creates new UdpSocket from a std::net::UdpSocket.
pub fn from_std(socket: std::net::UdpSocket) -> io::Result<Self> {
Ok(Self {
inner: Socket::from_socket2(Socket2::from(socket))?,
})
}

/// Close the socket. If the returned future is dropped before polling, the
/// socket won't be closed.
pub fn close(self) -> impl Future<Output = io::Result<()>> {
Expand Down

0 comments on commit e66bbcb

Please sign in to comment.