Skip to content

Commit

Permalink
Implement AsFd for &T and &mut T.
Browse files Browse the repository at this point in the history
This ports rust-lang/rust#93888 to io-lifetimes.
  • Loading branch information
sunfishcode committed Feb 15, 2022
1 parent 730d9ed commit 80532f9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
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
48 changes: 48 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,51 @@ pub trait FromSocket {
Self::from_socket(into_owned.into_socket())
}
}

#[cfg(not(windows))]
impl<T: AsFd> AsFd for &T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

#[cfg(not(windows))]
impl<T: AsFd> AsFd for &mut T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

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

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

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

#[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

0 comments on commit 80532f9

Please sign in to comment.