Skip to content

Commit 34997f0

Browse files
authored
Rollup merge of rust-lang#93888 - sunfishcode:sunfishcode/impl-asfd-for-ref, r=joshtriplett
Implement `AsFd` for `&T` and `&mut T`. Add implementations of `AsFd` for `&T` and `&mut T`, so that users can write code like this: ```rust pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> { ``` with `fd: F` rather than `fd: &F`. And similar for `AsHandle` and `AsSocket` on Windows. Also, adjust the `fchown` example to pass the file by reference. The code can work either way now, but passing by reference is more likely to be what users will want to do. This is an alternative to rust-lang#93869, and is a simpler way to achieve the same goals: users don't need to pass borrowed-`BorrowedFd` arguments, and it prevents a pitfall in the case where users write `fd: F` instead of `fd: &F`. r? ```@joshtriplett```
2 parents db71248 + 1f98ef7 commit 34997f0

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

library/std/src/os/fd/owned.rs

+16
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ pub trait AsFd {
200200
fn as_fd(&self) -> BorrowedFd<'_>;
201201
}
202202

203+
#[unstable(feature = "io_safety", issue = "87074")]
204+
impl<T: AsFd> AsFd for &T {
205+
#[inline]
206+
fn as_fd(&self) -> BorrowedFd<'_> {
207+
T::as_fd(self)
208+
}
209+
}
210+
211+
#[unstable(feature = "io_safety", issue = "87074")]
212+
impl<T: AsFd> AsFd for &mut T {
213+
#[inline]
214+
fn as_fd(&self) -> BorrowedFd<'_> {
215+
T::as_fd(self)
216+
}
217+
}
218+
203219
#[unstable(feature = "io_safety", issue = "87074")]
204220
impl AsFd for BorrowedFd<'_> {
205221
#[inline]

library/std/src/os/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ pub fn chown<P: AsRef<Path>>(dir: P, uid: Option<u32>, gid: Option<u32>) -> io::
966966
///
967967
/// fn main() -> std::io::Result<()> {
968968
/// let f = std::fs::File::open("/file")?;
969-
/// fs::fchown(f, Some(0), Some(0))?;
969+
/// fs::fchown(&f, Some(0), Some(0))?;
970970
/// Ok(())
971971
/// }
972972
/// ```

library/std/src/os/windows/io/handle.rs

+16
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ pub trait AsHandle {
316316
fn as_handle(&self) -> BorrowedHandle<'_>;
317317
}
318318

319+
#[unstable(feature = "io_safety", issue = "87074")]
320+
impl<T: AsHandle> AsHandle for &T {
321+
#[inline]
322+
fn as_handle(&self) -> BorrowedHandle<'_> {
323+
T::as_handle(self)
324+
}
325+
}
326+
327+
#[unstable(feature = "io_safety", issue = "87074")]
328+
impl<T: AsHandle> AsHandle for &mut T {
329+
#[inline]
330+
fn as_handle(&self) -> BorrowedHandle<'_> {
331+
T::as_handle(self)
332+
}
333+
}
334+
319335
impl AsHandle for BorrowedHandle<'_> {
320336
#[inline]
321337
fn as_handle(&self) -> BorrowedHandle<'_> {

library/std/src/os/windows/io/socket.rs

+16
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ pub trait AsSocket {
210210
fn as_socket(&self) -> BorrowedSocket<'_>;
211211
}
212212

213+
#[unstable(feature = "io_safety", issue = "87074")]
214+
impl<T: AsSocket> AsSocket for &T {
215+
#[inline]
216+
fn as_socket(&self) -> BorrowedSocket<'_> {
217+
T::as_socket(self)
218+
}
219+
}
220+
221+
#[unstable(feature = "io_safety", issue = "87074")]
222+
impl<T: AsSocket> AsSocket for &mut T {
223+
#[inline]
224+
fn as_socket(&self) -> BorrowedSocket<'_> {
225+
T::as_socket(self)
226+
}
227+
}
228+
213229
impl AsSocket for BorrowedSocket<'_> {
214230
#[inline]
215231
fn as_socket(&self) -> BorrowedSocket<'_> {

0 commit comments

Comments
 (0)