ManagedFD

Trait ManagedFD 

Source
pub trait ManagedFD
where Self: AsRawFd + Clone,
{ // Required methods fn wrap(fd: RawFd) -> Self; fn dup_wrap(fd: RawFd) -> Result<Self>; fn dup(&self) -> Result<Self>; }
Expand description

Trait ManagedFD describes a managed std::os::unix::io::RawFd, with primary functionality of auto-closing on drop and performing sensible clone()/dup() operations.

Warning: Clone trait has no way to convey errors, so implementations are forced to panic!().

Required Methods§

Source

fn wrap(fd: RawFd) -> Self

Wrap fd in ManagedFD. You should not use naked handle afterwards, in particular don’t close it.

Source

fn dup_wrap(fd: RawFd) -> Result<Self>

Wrap a dup(2) copy of fd in ManagedFD. You should dispose the original fd properly at your discretion.

Source

fn dup(&self) -> Result<Self>

Create a duplicate of handle in such a way, that dropping of one instance has no influence on the other ones.

It lets you define (required) Clone trait as simply

impl Clone for MyImpl {
    fn clone(&self) -> Self {
        self.dup().unwrap()
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§