owned_fd

Struct OwnedFd

Source
pub struct OwnedFd { /* private fields */ }
Expand description

OwnedFd is an RAII wrapper around RawFd: it automatically closes on drop & provides borrow() functionality to support lifetimes & borrow checking around the use of file descriptors

Compared to using File, TcpStream, etc as a wrapper to close on drop, OwnedFd:

  • allows any type of filedescriptor to be used safely
  • has no overhead greater than a RawFd (no buffer, metadata, or other allocations)
  • allows use of the borrow system to ensure drop (close) happens only when all users of an ownedfd have released it.

Implementations§

Source§

impl OwnedFd

Source

pub unsafe fn from_unowned_raw(i: RawFd) -> Result<OwnedFd>

Given a raw file descriptor that may be owned by another (ie: another data structure might close it), create a Owned version that we have control over (via dup())

For taking ownership, see FromRawFd::from_raw_fd().

Unsafety:

  • @i must be a valid file descriptor (of any kind)
Source

pub fn dup(&self) -> Result<OwnedFd>

Duplicate this OwnedFd, and allow the error to be detected.

Clone uses this, but panics on error

Source

pub fn from<T: IntoRawFd>(i: T) -> Self

Given a type that impliments IntoRawFd construct an OwnedFd.

This is safe because we assume the promises of IntoRawFd are followed.

NOTE: ideally, we’d impl this as From, but current rust doesn’t allow that. Revisit when specialization stabilizes.

Trait Implementations§

Source§

impl AsRawFd for OwnedFd

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Borrow<FdRef> for OwnedFd

Source§

fn borrow(&self) -> &FdRef

Immutably borrows from an owned value. Read more
Source§

impl Clone for OwnedFd

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Deref for OwnedFd

Source§

type Target = FdRef

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for OwnedFd

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl FromRawFd for OwnedFd

Source§

unsafe fn from_raw_fd(fd: RawFd) -> OwnedFd

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

impl IntoRawFd for OwnedFd

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 T
where T: "static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.