Trait std::os::fd::AsFd

1.63.0 · source ·
pub trait AsFd {
    fn as_fd(&self) -> BorrowedFd<'_>;
Expand description

A trait to borrow the file descriptor from an underlying object.

This is only available on unix platforms and must be imported in order to call the method. Windows platforms have a corresponding AsHandle and AsSocket set of traits.

Required Methods§

Borrows the file descriptor.

use std::fs::File;

let mut f = File::open("foo.txt")?;
let borrowed_fd: BorrowedFd<'_> = f.as_fd();


This impl allows implementing traits that require AsFd on Arc.

use std::net::UdpSocket;
use std::sync::Arc;

trait MyTrait: AsFd {}
impl MyTrait for Arc<UdpSocket> {}
impl MyTrait for Box<UdpSocket> {}