shredder icon indicating copy to clipboard operation
shredder copied to clipboard

Is Gc pointer equality supported?

Open awestlake87 opened this issue 3 years ago • 5 comments

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?

awestlake87 avatar Nov 01 '21 15:11 awestlake87

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

Others avatar Nov 01 '21 17:11 Others

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,
        )
    }
}

awestlake87 avatar Nov 01 '21 17:11 awestlake87

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)

Others avatar Nov 01 '21 17:11 Others

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.

awestlake87 avatar Nov 01 '21 17:11 awestlake87

get is not free, and actually potentially blocking (if collection is happening). So yeah, this definitely needs to be moved internally. See #72

Others avatar Nov 02 '21 04:11 Others