#managed #traits #clone #dealing #handles #file #filehandle

pakr-managedrawfd

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

1 stable release

1.0.0 Sep 29, 2020

#553 in Operating systems

BSD-2-Clause

9KB
102 lines

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.

Multi-access

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.

Dependencies

~44KB