[−][src]Struct glsp_engine::Gc
A weak pointer onto the garbage-collected heap.
Gc
's API is very similar to std::rc::Weak
. You can construct a Gc
by calling
Root::downgrade
. A Gc
can't be dereferenced; instead,
you should promote it to a Root
by calling Gc::upgrade
.
You should almost always use Root
rather than Gc
.
The only exception is RData
. If you store a Root
within an RData
, it
will cause a memory leak. If you need to construct an RData
which stores a pointer to another
heap-allocated object, you should:
-
Use
Gc
where you would normally useRoot
. -
Call
RClassBuilder::trace
before constructing theRData
. -
Call
glsp::write_barrier
when theRData
is mutated.
The last two steps are necessary to prevent the pointed-to object from being deallocated.
See also GcVal
(a Val
which stores Gc
pointers rather than Root
pointers), and RGc
(a strongly-typed alternative to Gc<RData>
).
struct Collider { bounds: Rect, obj: Gc<Obj> } impl Collider { fn trace(&self, visitor: &mut GcVisitor) { visitor.visit(&self.obj); } } fn setup() { RClassBuilder::<Collider>::new() .trace(Collider::trace) .build(); } fn new_collider(obj: Root<Obj>) -> GResult<Root<RData>> { let collider = Collider { bounds: obj.get("bounds")?, obj: obj.downgrade() }; Ok(glsp::rdata(collider)) }
Implementations
impl<T: Allocate> Gc<T>
[src]
pub fn upgrade(&self) -> Option<Root<T>>
[src]
Attempts to construct a strong pointer from this weak pointer.
Returns None
if the pointed-to object has been deallocated by the garbage
collector. To prevent this, use glsp::write_barrier
and
RClassBuilder::trace
.
pub fn ptr_eq(gc0: &Gc<T>, gc1: &Gc<T>) -> bool
[src]
Returns true
if both Gc
s point to the same heap-allocated object.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Gc<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> !Send for Gc<T>
[src]
impl<T> !Sync for Gc<T>
[src]
impl<T> Unpin for Gc<T>
[src]
impl<T> UnwindSafe for Gc<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Erased for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,