Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass RawFd to function with trait bound T: AsRawFd #1028

Closed
gatoWololo opened this issue Feb 12, 2019 · 1 comment
Closed

Cannot pass RawFd to function with trait bound T: AsRawFd #1028

gatoWololo opened this issue Feb 12, 2019 · 1 comment

Comments

@gatoWololo
Copy link

gatoWololo commented Feb 12, 2019

Hello! Thank you for the great library I use Nix for all my projects.

I am trying to call nix::sys::statvfs::fstatvfs with signature: pub fn fstatvfs<T: AsRawFd>(fd: &T) -> Result<Statvfs>. I have a RawFd:

let fdref: RawFd = ...;
match nix::sys::statvfs::fstatvfs(& fdref) {
...
}

Error:

error[E0277]: the trait bound `i32: std::os::unix::io::AsRawFd` is not satisfied
   --> src/detfs.rs:992:15
    |
992 |         match nix::sys::statvfs::fstatvfs(& fdref) {
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::os::unix::io::AsRawFd` is not implemented for `i32`
    |
    = note: required by `nix::sys::statvfs::fstatvfs`

So it seems I cannot use a RawFd to call a function with T: AsRawFd trait bound?
I ended up wrapping my RawFd in a struct which implemented AsRawFd:

struct Fd(RawFd);
use std::os::unix::io::AsRawFd;
impl AsRawFd for Fd {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

Which works, but is really weird to have to do. Am I missing something?

@asomers
Copy link
Member

asomers commented Feb 12, 2019

You aren't missing anything. The problem is that RawFd is defined as a type alias for i32, not a struct. So there's no way to add methods to it without also adding those methods to i32.
rust-lang/rust#41035

@asomers asomers closed this as completed Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants