LocalRtGc

Struct LocalRtGc 

Source
pub struct LocalRtGc { /* private fields */ }
Expand description

A simple garbage collector which collects resources dropped on a realtime thread and safely deallocates them on another thread.

Unlike GlobalRtGc, this version does not use global statics.

The performance characteristics of the ArcGc, OwnedGc, and OwnedGcUnsized smart pointers are equivalant to Arc when reading (but constructing them is a bit more expensive).

§Example

let mut collector = LocalRtGc::new();
let handle = collector.handle();

let value_1 = ArcGc::new_loc(String::from("foo"), &handle);
// Same as `ArcGc` but for `!Sync` data.
let value_2 = OwnedGc::new_loc(String::from("bar"), &handle);

// A simulated "realtime thread"
let rt_thread = std::thread::spawn(move || {
    std::thread::sleep(Duration::from_millis(15));

    // Dropping the values on the realtime thread is realtime-safe
    // because the contents are automatically collected and
    // deallocated on a separate non-realtime thread.
    let _ = value_1;
    let _ = value_2;
});

// A simulated update loop on the main thread
for _ in 0..4 {
    // Call `collector.collect()` periodically to deallocate any
    // resources that were dropped on the realtime thread.
    collector.collect();

    std::thread::sleep(Duration::from_millis(15));
}

Implementations§

Source§

impl LocalRtGc

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn handle(&self) -> LocalRtGcHandle

Source

pub fn any_dropped(&self) -> bool

Returns true if any allocation has been dropped since the collection cycle.

Source

pub fn num_allocations(&self) -> usize

The total number of allocations currently active in this garbage collector.

Source

pub fn collect(&mut self)

Collect and drop all unused ArcGc resources.

Source

pub fn strong_count(&self) -> usize

The total number of active references to this garbage collector.

Trait Implementations§

Source§

impl Clone for LocalRtGc

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Collector for LocalRtGc

Source§

fn register<T>(&self, data: Arc<T>)
where T: ?Sized + Send + Sync + 'static, Arc<T>: StrongCount,

Register this data with the garbage collector.
Source§

fn remove<T>(&self, data: &Arc<T>)
where T: ?Sized + Send + Sync + 'static, Arc<T>: StrongCount,

Called in ArcGc’s Drop implementation. Read more
Source§

impl Default for LocalRtGc

Source§

fn default() -> Self

Returns the “default value” for a type. 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.