1 stable release
1.0.0 | Sep 29, 2020 |
---|
#553 in Operating systems
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 onclone()
call due to underlying OS error. -
SharedFD
holds a raw handle instd::sync::Arc
and 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
AsRawFd
andClone
traits. - have a
wrap(fd)
constructor that simply packsfd
in managed shell and takes ownership (you shouldn't usefd
afterwards and definitely notclose()
it). - have a
dup_wrap(fd)
constructor that packs dup(2) copy offd
in 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
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