pub struct Resource(/* private fields */);Expand description
A reference-counted, platform-independent wrapper around OS I/O resources.
See the module documentation for usage examples and details.
Implementations§
Source§impl Resource
impl Resource
Sourcepub fn stdout() -> Self
Available on Unix only.
pub fn stdout() -> Self
Returns a Resource for standard output (stdout).
This creates a duplicate of the stdout file descriptor, so the returned
Resource can be safely dropped without affecting the actual stdout.
§Panics
Panics if duplicating the file descriptor fails.
§Examples
use lio::api::resource::Resource;
let stdout = Resource::stdout();
// Use stdout for async I/O...Sourcepub fn stderr() -> Self
Available on Unix only.
pub fn stderr() -> Self
Returns a Resource for standard error (stderr).
This creates a duplicate of the stderr file descriptor, so the returned
Resource can be safely dropped without affecting the actual stderr.
§Panics
Panics if duplicating the file descriptor fails.
§Examples
use lio::api::resource::Resource;
let stderr = Resource::stderr();
// Use stderr for async I/O...Sourcepub fn stdin() -> Self
Available on Unix only.
pub fn stdin() -> Self
Returns a Resource for standard input (stdin).
This creates a duplicate of the stdin file descriptor, so the returned
Resource can be safely dropped without affecting the actual stdin.
§Panics
Panics if duplicating the file descriptor fails.
§Examples
use lio::api::resource::Resource;
let stdin = Resource::stdin();
// Use stdin for async I/O...Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Returns the number of strong references to this resource.
This counts how many Resource instances point to the same underlying
file descriptor or handle. When the count reaches zero, the resource
will be automatically closed.
§Returns
The number of Resource instances sharing this resource.
§Examples
use lio::api::resource::Resource;
// Create a resource (using stdout as an example)
let resource = Resource::stdout();
assert_eq!(resource.count(), 1);
let clone = resource.clone();
assert_eq!(resource.count(), 2);
assert_eq!(clone.count(), 2);
drop(clone);
assert_eq!(resource.count(), 1);Sourcepub fn will_close(&self) -> bool
pub fn will_close(&self) -> bool
Checks if this is the last reference to the resource.
When this returns true, dropping this Resource will cause the underlying
file descriptor or handle to be closed. This is useful for determining when
cleanup will occur.
§Returns
true if this is the only remaining reference (count == 1), meaning the
resource will be closed when this Resource is dropped.
§Examples
use lio::api::resource::Resource;
let resource = Resource::stdout();
assert!(resource.will_close()); // Only reference
let clone = resource.clone();
assert!(!resource.will_close()); // Multiple references
assert!(!clone.will_close());
drop(clone);
assert!(resource.will_close()); // Last reference againTrait Implementations§
Source§impl AsFd for Resource
Available on Unix only.
impl AsFd for Resource
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsResource for &Resource
Direct implementation for Resource itself.
impl AsResource for &Resource
Direct implementation for Resource itself.
This allows using a Resource directly where AsResource is expected.