Skip to content
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
4 changes: 2 additions & 2 deletions examples/flexible-apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn use_fd_a(fd: BorrowedFd<'_>) {
/// it has the advantage of allowing users to pass in any type implementing
/// `AsFd` directly, without having to call `.as_fd()` themselves.
#[cfg(all(feature = "close", not(windows)))]
fn use_fd_b<Fd: AsFd>(fd: &Fd) {
fn use_fd_b<Fd: AsFd>(fd: Fd) {
let _ = fd.as_fd();
}

Expand Down Expand Up @@ -63,7 +63,7 @@ fn main() {
use_fd_b(&f);

// Of course, users can still pass in `BorrowedFd` values if they want to.
use_fd_b(&f.as_fd());
use_fd_b(f.as_fd());

let a = std::fs::File::open("Cargo.toml").unwrap();
let b = std::fs::File::open("Cargo.toml").unwrap();
Expand Down
54 changes: 54 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,57 @@ pub trait FromSocket {
Self::from_socket(into_owned.into_socket())
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFd for &T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFd for &mut T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(windows)]
impl<T: AsHandle> AsHandle for &T {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
T::as_handle(self)
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(windows)]
impl<T: AsHandle> AsHandle for &mut T {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
T::as_handle(self)
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(windows)]
impl<T: AsSocket> AsSocket for &T {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
T::as_socket(self)
}
}

#[cfg(not(io_lifetimes_use_std))]
#[cfg(windows)]
impl<T: AsSocket> AsSocket for &mut T {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
T::as_socket(self)
}
}
12 changes: 6 additions & 6 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use io_lifetimes::{

struct Tester {}
impl Tester {
fn use_file<Filelike: AsFilelike>(filelike: &Filelike) {
fn use_file<Filelike: AsFilelike>(filelike: Filelike) {
let filelike = filelike.as_filelike();
let _ = filelike.as_filelike_view::<std::fs::File>();
let _ = unsafe {
Expand All @@ -24,7 +24,7 @@ impl Tester {
let _ = dbg!(filelike);
}

fn use_socket<Socketlike: AsSocketlike>(socketlike: &Socketlike) {
fn use_socket<Socketlike: AsSocketlike>(socketlike: Socketlike) {
let socketlike = socketlike.as_socketlike();
let _ = socketlike.as_socketlike_view::<std::net::TcpStream>();
let _ = unsafe {
Expand Down Expand Up @@ -64,16 +64,16 @@ impl Tester {
fn test_api() {
let file = std::fs::File::open("Cargo.toml").unwrap();
Tester::use_file(&file);
Tester::use_file(&file.as_filelike());
Tester::use_file(file.as_filelike());
Tester::use_file(&*file.as_filelike_view::<std::fs::File>());
Tester::use_file(&file.as_filelike_view::<std::fs::File>().as_filelike());
Tester::use_file(file.as_filelike_view::<std::fs::File>().as_filelike());

let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
Tester::use_socket(&socket);
Tester::use_socket(&socket.as_socketlike());
Tester::use_socket(socket.as_socketlike());
Tester::use_socket(&*socket.as_socketlike_view::<std::net::TcpListener>());
Tester::use_socket(
&socket
socket
.as_socketlike_view::<std::net::TcpListener>()
.as_socketlike(),
);
Expand Down