Skip to content

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

Closed
@gatoWololo

Description

@gatoWololo

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions