Trait std::os::wasi::io::FromRawFd

1.1.0 · source · []
pub trait FromRawFd {
    unsafe fn from_raw_fd(fd: RawFd) -> Self;
Available on WASI only.
Expand description

A trait to express the ability to construct an object from a raw file descriptor.

Required Methods

Constructs a new instance of Self from the given raw file descriptor.

This function is typically used to consume ownership of the specified file descriptor. When used in this way, the returned object will take responsibility for closing it when the object goes out of scope.

However, consuming ownership is not strictly required. Use a From<OwnedFd>::from implementation for an API which strictly consumes ownership.


The fd passed in must be a valid an open file descriptor.

use std::fs::File;
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{FromRawFd, IntoRawFd, RawFd};

let f = File::open("foo.txt")?;
let raw_fd: RawFd = f.into_raw_fd();
// SAFETY: no other functions should call `from_raw_fd`, so there
// is only one owner for the file descriptor.
let f = unsafe { File::from_raw_fd(raw_fd) };