[][src]Crate pakr_managedrawfd

A Trait and two Impls dealing with auto-closing RawFd file handles with a sensible Clone trait implementations.

  • DuplicatingFD holds a raw handle directly and duplicates itself by calls to dup(2) - that is each instance has its own handle that is individually closed on end-of-life of that instance. May panic on clone() call due to underlying OS error.

  • SharedFD holds a raw handle in std::sync::Arc and duplicates itself by duplicating said Arc - that is each instance shares a single handle, which is closed after the last instance reaches end-of-life.

Common functionality

Both implementations...

  • implement AsRawFd and Clone traits.
  • have a wrap(fd) constructor that simply packs fd in managed shell and takes ownership (you shouldn't use fd afterwards and definitely not close() it).
  • have a dup_wrap(fd) constructor that packs dup(2) copy of fd in managed shell. It doesn't take the ownership of the original fd, which you should dispose-of properly.
  • have a dup() method that clones handle accordingly, returning eventual errors.


Both are not multi-access safe, with SharedFD being even less safe.

  • Each of the related DuplicatingFD instances has its own read/write pointer (still stepping on each other's toes during writes)
  • All the related SharedFD instances have a single, shared read/write pointer.



Implements Clone trait that calls dup(2) on underlying handle and returns new instance wrapping dup-ped handle.


Implements Clone trait that creates new SharedFD with Arc::clone of the embedded handle.



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