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>
impl<T: Send + Sync + 'static> ArcGc<T, LocalRtGc>
Sourcepub fn new_loc(value: T, handle: &LocalRtGcHandle) -> Self
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>
impl<T: ?Sized + Send + Sync + 'static> ArcGc<T, LocalRtGc>
Sourcepub fn new_unsized_loc(
f: impl FnOnce() -> Arc<T>,
handle: &LocalRtGcHandle,
) -> Self
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>
impl ArcGc<dyn Any + Send + Sync + 'static, LocalRtGc>
Sourcepub fn new_any_loc<T: Send + Sync + 'static>(
value: T,
handle: &LocalRtGcHandle,
) -> Self
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: ?Sized + Send + Sync + 'static> ArcGc<T, GlobalRtGc>
impl<T: ?Sized + Send + Sync + 'static> ArcGc<T, GlobalRtGc>
Sourcepub fn new_unsized(f: impl FnOnce() -> Arc<T>) -> Self
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>,
);Trait Implementations§
impl<T: ?Sized + Send + Sync + 'static> Eq for ArcGc<T>
Auto Trait Implementations§
impl<T, C> Freeze for ArcGc<T, C>
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>
impl<T, C> UnwindSafe for ArcGc<T, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.