From e66bbcb71df016fcf52d52dddc2ceeeb1b4b3649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Fri, 27 Sep 2024 00:19:54 +0900 Subject: [PATCH] feat(net): add from_std for UdpSocket --- compio-net/Cargo.toml | 2 +- compio-net/src/udp.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/compio-net/Cargo.toml b/compio-net/Cargo.toml index b98d2195..ca8662b6 100644 --- a/compio-net/Cargo.toml +++ b/compio-net/Cargo.toml @@ -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"] diff --git a/compio-net/src/udp.rs b/compio-net/src/udp.rs index 33f39c2d..2ca41c79 100644 --- a/compio-net/src/udp.rs +++ b/compio-net/src/udp.rs @@ -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}; @@ -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 { + 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> {