Skip to main content

Resource

Struct Resource 

Source
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

Source

pub fn stdout() -> Self

Available on Unix only.

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...
Source

pub fn stderr() -> Self

Available on Unix only.

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...
Source

pub fn stdin() -> Self

Available on Unix only.

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...
Source

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);
Source

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 again

Trait Implementations§

Source§

impl AsFd for Resource

Available on Unix only.
Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for Resource

Available on Unix only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl AsResource for &Resource

Direct implementation for Resource itself.

This allows using a Resource directly where AsResource is expected.

Source§

fn as_resource(&self) -> &Resource

Returns a reference to the underlying Resource. Read more
Source§

impl AsResource for Resource

Source§

fn as_resource(&self) -> &Resource

Returns a reference to the underlying Resource. Read more
Source§

impl Clone for Resource

Source§

fn clone(&self) -> Resource

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Resource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromRawFd for Resource

Available on Unix only.
Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.