shredder
shredder copied to clipboard
Is Gc pointer equality supported?
I was wondering if there was some sort of equivalent to Arc::ptr_eq
for Gc
. I've been converting a project with lots of Arc
references over to Gc
and there are a few places where I want to do an identity check instead of an equality check, but I wasn't able to find anything like this in the docs.
If not, are there any obstacles to adding this check?
I think this should be pretty easy to add--I'll take a look tonight.
You may also be able to work around this with the existing API, by getting the &T references out of the Gc/GcGuard and calling ptr::eq on that
Yeah I had a similar thought. Does something like this look correct to you or is there a chance that a pointer could change between self.get()
and other.get()
?
pub trait GcIs {
fn is(&self, other: &Self) -> bool;
}
impl<T> GcIs for Gc<T>
where
T: Scan,
{
fn is(&self, other: &Self) -> bool {
std::ptr::eq(
self.get().as_ref() as *const T,
other.get().as_ref() as *const T,
)
}
}
That looks correct. It is actually never possible for the pointer returned by .get().deref() to change (if you look at the implementation it's synthesized directly from a static stored pointer)
Ok, awesome! Also, thanks for the quick responses!
I'll just use this trait for now so feel free to close this issue. It'd be great to have something like this in the library eventually, but seems like it's pretty simple to work around.
get
is not free, and actually potentially blocking (if collection is happening). So yeah, this definitely needs to be moved internally. See #72