ArcGc

Struct ArcGc 

Source
pub struct ArcGc<T: ?Sized + Send + Sync + 'static, C: Collector = GlobalRtGc> { /* private fields */ }
Expand description

A realtime-safe wrapper around Arc that when dropped, automatically has its contents collected and later deallocated on another non-realtime thread.

The performance characteristics of ArcGc are equivalant to Arc when reading (but constructing an ArcGc is a bit more expensive).

Equality checking between instances of ArcGc relies only on pointer equivalence. If you need to evaluate the equality of the values contained by ArcGc, you’ll need to be careful to ensure you explicitly take references of the inner data.

§Example

let value: ArcGc<String> = ArcGc::new(String::from("foo"));

// 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;
});

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

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

Implementations§

Source§

impl<T: Send + Sync + 'static> ArcGc<T, LocalRtGc>

Source

pub fn new_loc(value: T, handle: &LocalRtGcHandle) -> Self

Construct a new ArcGc.

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

let value: ArcGc<String, LocalRtGc> = ArcGc::new_loc(String::from("foo"), &handle);
Source§

impl<T: ?Sized + Send + Sync + 'static> ArcGc<T, LocalRtGc>

Source

pub fn new_unsized_loc( f: impl FnOnce() -> Arc<T>, handle: &LocalRtGcHandle, ) -> Self

Construct a new ArcGc with unsized data, such as [T] or dyn Trait.

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

let value_1: ArcGc<[f32], LocalRtGc> = ArcGc::new_unsized_loc(
    || Arc::<[f32]>::from([1.0, 2.0, 3.0]),
    &handle
);

trait Foo: Send + Sync {}
struct Bar {}
impl Foo for Bar {}

let value_2: ArcGc<dyn Foo, LocalRtGc> = ArcGc::new_unsized_loc(
    || Arc::new(Bar {}) as Arc<dyn Foo>,
    &handle
);
Source§

impl ArcGc<dyn Any + Send + Sync + 'static, LocalRtGc>

Source

pub fn new_any_loc<T: Send + Sync + 'static>( value: T, handle: &LocalRtGcHandle, ) -> Self

Construct a type-erased ArcGc.

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

let value: ArcGc<dyn Any + Send + Sync + 'static, LocalRtGc> =
    ArcGc::new_any_loc(String::new(), &handle);
Source§

impl<T: Send + Sync + 'static> ArcGc<T, GlobalRtGc>

Source

pub fn new(value: T) -> Self

Construct a new ArcGc.

let value: ArcGc<String> = ArcGc::new(String::from("foo"));
Source§

impl<T: ?Sized + Send + Sync + 'static> ArcGc<T, GlobalRtGc>

Source

pub fn new_unsized(f: impl FnOnce() -> Arc<T>) -> Self

Construct a new ArcGc with unsized data, such as [T] or dyn Trait.

let value_1: ArcGc<[f32]> = ArcGc::new_unsized(
    || Arc::<[f32]>::from([1.0, 2.0, 3.0]),
);

trait Foo: Send + Sync {}
struct Bar {}
impl Foo for Bar {}

let value_2: ArcGc<dyn Foo> = ArcGc::new_unsized(
    || Arc::new(Bar {}) as Arc<dyn Foo>,
);
Source§

impl ArcGc<dyn Any + Send + Sync + 'static, GlobalRtGc>

Source

pub fn new_any<T: Send + Sync + 'static>(value: T) -> Self

Construct a type-erased ArcGc.

let value: ArcGc<dyn Any + Send + Sync + 'static> =
    ArcGc::new_any(String::from("foo"));
Source§

impl<T: ?Sized + Send + Sync + 'static> ArcGc<T>

Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

A wrapper around std::sync::Arc::ptr_eq.

Trait Implementations§

Source§

impl<T: ?Sized + Send + Sync + 'static> Clone for ArcGc<T>

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<T: Debug + ?Sized + Send + Sync + 'static> Debug for ArcGc<T>

Source§

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

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

impl<T: ?Sized + Send + Sync + 'static> Deref for ArcGc<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized + Send + Sync + 'static, C: Collector> Drop for ArcGc<T, C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: ?Sized + Send + Sync + 'static> PartialEq for ArcGc<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: ?Sized + Send + Sync + 'static> Eq for ArcGc<T>

Auto Trait Implementations§

§

impl<T, C> Freeze for ArcGc<T, C>
where C: Freeze, T: ?Sized,

§

impl<T, C> RefUnwindSafe for ArcGc<T, C>

§

impl<T, C> Send for ArcGc<T, C>
where T: ?Sized,

§

impl<T, C> Sync for ArcGc<T, C>
where T: ?Sized,

§

impl<T, C> Unpin for ArcGc<T, C>
where C: Unpin, T: ?Sized,

§

impl<T, C> UnwindSafe for ArcGc<T, C>
where C: UnwindSafe, T: RefUnwindSafe + ?Sized,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.