6

`impl<T: AsRawFd> AsRawFd for {Arc,Box}<T>` by jyn514 · Pull Request...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/97437
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Member

@jyn514 jyn514 commented on May 26

edited

This allows implementing traits that require a raw FD on Arc and Box.

Previously, you'd have to add the function to the trait itself:

trait MyTrait {
    fn as_raw_fd(&self) -> RawFd;
}

impl<T: MyTrait> MyTrait for Arc<T> {
    fn as_raw_fd(&self) -> RawFd {
        (**self).as_raw_fd()
    }
}

In particular, this leads to lots of "multiple applicable items in scope" errors because you have to disambiguate MyTrait::as_raw_fd from AsRawFd::as_raw_fd at each call site. In generic contexts, when passing the type to a function that takes impl AsRawFd it's also sometimes required to use T: MyTrait + AsRawFd, which wouldn't be necessary if I could write MyTrait: AsRawFd.

After this PR, the code can be simpler:

trait MyTrait: AsRawFd {}

impl<T: MyTrait> MyTrait for Arc<T> {}
All reactions

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK