diff --git a/examples/flexible-apis.rs b/examples/flexible-apis.rs index b3a51c5..023eadc 100644 --- a/examples/flexible-apis.rs +++ b/examples/flexible-apis.rs @@ -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: &Fd) { +fn use_fd_b(fd: Fd) { let _ = fd.as_fd(); } @@ -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(); diff --git a/src/traits.rs b/src/traits.rs index e6464c8..0956d99 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -218,3 +218,51 @@ pub trait FromSocket { Self::from_socket(into_owned.into_socket()) } } + +#[cfg(not(windows))] +impl AsFd for &T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[cfg(not(windows))] +impl AsFd for &mut T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[cfg(windows)] +impl AsHandle for &T { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + T::as_handle(self) + } +} + +#[cfg(windows)] +impl AsHandle for &mut T { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + T::as_handle(self) + } +} + +#[cfg(windows)] +impl AsSocket for &T { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + T::as_socket(self) + } +} + +#[cfg(windows)] +impl AsSocket for &mut T { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + T::as_socket(self) + } +} diff --git a/tests/api.rs b/tests/api.rs index bf1d56d..4645fc8 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -11,7 +11,7 @@ use io_lifetimes::{ struct Tester {} impl Tester { - fn use_file(filelike: &Filelike) { + fn use_file(filelike: Filelike) { let filelike = filelike.as_filelike(); let _ = filelike.as_filelike_view::(); let _ = unsafe { @@ -24,7 +24,7 @@ impl Tester { let _ = dbg!(filelike); } - fn use_socket(socketlike: &Socketlike) { + fn use_socket(socketlike: Socketlike) { let socketlike = socketlike.as_socketlike(); let _ = socketlike.as_socketlike_view::(); let _ = unsafe { @@ -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::()); - Tester::use_file(&file.as_filelike_view::().as_filelike()); + Tester::use_file(file.as_filelike_view::().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::()); Tester::use_socket( - &socket + socket .as_socketlike_view::() .as_socketlike(), );