A Trait and two Impls dealing with auto-closing RawFd file handles with a
sensible Clone trait implementations.
-
DuplicatingFDholds 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 onclone()call due to underlying OS error. -
SharedFDholds a raw handle instd::sync::Arcand duplicates itself by duplicating saidArc- that is each instance shares a single handle, which is closed after the last instance reaches end-of-life.
Common functionality
Both implementations...
- implement
AsRawFdandClonetraits. - have a
wrap(fd)constructor that simply packsfdin managed shell and takes ownership (you shouldn't usefdafterwards and definitely notclose()it). - have a
dup_wrap(fd)constructor that packs dup(2) copy offdin managed shell. It doesn't take the ownership of the originalfd, 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
DuplicatingFDinstances has its own read/write pointer (still stepping on each other's toes during writes) - All the related
SharedFDinstances have a single, shared read/write pointer.