Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lib] Add the implementation of AsFd #92

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/example_0.13.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ jobs:
- name: Install WasmEdge
run: |
VERSION=0.13.5
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- -e all --version=$VERSION --tf-version=$VERSION --tf-deps-version=$VERSION --tf-tools-version=$VERSION --image-version=$VERSION -p /usr/local
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- --version=$VERSION -p /usr/local
# Disable this example due to it relies on wasmedge_http_req, which is a cyclic dependence
# - name: HTTP client example
# run: |
Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
use std::{
io::{self, Read, Write},
net::{SocketAddrV4, SocketAddrV6},
os::wasi::prelude::{AsRawFd, FromRawFd, IntoRawFd},
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd},
};

#[derive(Debug)]
Expand All @@ -28,6 +28,12 @@ impl AsMut<socket::Socket> for TcpStream {
}
}

impl AsFd for TcpStream {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

#[derive(Debug)]
pub struct TcpListener {
s: socket::Socket,
Expand All @@ -47,6 +53,12 @@ impl AsMut<socket::Socket> for TcpListener {
}
}

impl AsFd for TcpListener {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

#[derive(Debug)]
pub struct UdpSocket {
s: socket::Socket,
Expand All @@ -64,6 +76,12 @@ impl AsMut<socket::Socket> for UdpSocket {
}
}

impl AsFd for UdpSocket {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

impl TcpStream {
/// Create TCP socket and connect to the given address.
///
Expand Down
8 changes: 7 additions & 1 deletion src/socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use std::mem::MaybeUninit;
use std::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::os::wasi::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};

#[derive(Copy, Clone, Debug)]
#[repr(u8, align(1))]
Expand Down Expand Up @@ -1307,6 +1307,12 @@ impl AsRawFd for Socket {
}
}

impl AsFd for Socket {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

impl IntoRawFd for Socket {
fn into_raw_fd(self) -> RawFd {
let fd = self.fd;
Expand Down
Loading