Struct filedesc::FileDesc

source ·
pub struct FileDesc { /* private fields */ }
Expand description

Thin wrapper around an open file descriptor.

The wrapped file descriptor will be closed when the wrapper is dropped.

Implementations§

source§

impl FileDesc

source

pub fn new(fd: OwnedFd) -> Self

Create FileDesc from an owned file descriptor.

This does not do anything to the file descriptor other than wrap it. Notably, it does not set the close-on-exec flag.

source

pub unsafe fn from_raw_fd(fd: RawFd) -> Self

Wrap a raw file descriptor in a FileDesc.

This does not do anything to the file descriptor other than wrapping it. Notably, it does not set the close-on-exec flag.

Safety

The input must be a valid file descriptor. The file descriptor must not be closed as long as it is managed by the created FileDesc.

source

pub fn duplicate_from<T: AsFd>(other: T) -> Result<Self>

Duplicate a file descriptor from an object that has a file descriptor.

The new file descriptor will have the close-on-exec flag set. If the platform supports it, the flag will be set atomically.

The duplicated FileDesc will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.

source

pub unsafe fn duplicate_raw_fd(fd: RawFd) -> Result<Self>

Duplicate a raw file descriptor and wrap it in a FileDesc.

The new file descriptor will have the close-on-exec flag set. If the platform supports it, the flag will be set atomically.

The duplicated FileDesc will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.

Safety

The file descriptor must be valid, and duplicating it must not violate the safety requirements of any object already using the file descriptor.

source

pub fn as_fd(&self) -> BorrowedFd<'_>

Get the file descriptor.

This function does not release ownership of the underlying file descriptor. The file descriptor will still be closed when the FileDesc is dropped.

source

pub fn into_fd(self) -> OwnedFd

Release and get the raw file descriptor.

This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.

source

pub fn as_raw_fd(&self) -> RawFd

Get the raw file descriptor.

This function does not release ownership of the underlying file descriptor. The file descriptor will still be closed when the FileDesc is dropped.

source

pub fn into_raw_fd(self) -> RawFd

Release and get the raw file descriptor.

This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.

source

pub fn duplicate(&self) -> Result<Self>

Try to duplicate the file descriptor.

The duplicated FileDesc will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.

The new file descriptor will have the close-on-exec flag set. If the platform supports it, the flag will be set atomically.

source

pub fn set_close_on_exec(&self, close_on_exec: bool) -> Result<()>

Change the close-on-exec flag of the file descriptor.

You should always try to create file descriptors with the close-on-exec flag already set atomically instead of changing it later with this function. Setting the flag later on introduces a race condition if another thread forks before the call to set_close_on_exec finishes.

You can use this without any race condition to disable the close-on-exec flag after forking but before executing a new program.

source

pub fn get_close_on_exec(&self) -> Result<bool>

Check the close-on-exec flag of the file descriptor.

Trait Implementations§

source§

impl AsFd for FileDesc

source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
source§

impl AsRawFd for &FileDesc

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl AsRawFd for FileDesc

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for FileDesc

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<FileDesc> for OwnedFd

source§

fn from(value: FileDesc) -> Self

Converts to this type from the input type.
source§

impl From<OwnedFd> for FileDesc

source§

fn from(value: OwnedFd) -> Self

Converts to this type from the input type.
source§

impl FromRawFd for FileDesc

source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for FileDesc

source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.