Others / shredder

Garbage collected smart pointers for Rust
MIT License
266 stars 9 forks source link

Cell<T> doesn't implement Scan #38

Closed jrmuizel closed 4 years ago

jrmuizel commented 4 years ago

I think it should be able to.

Others commented 4 years ago

Yep that's an oversight! Should be easy enough to fix

Others commented 4 years ago

Okay thought about this a bit more. An impl could exist here, but I don't know if it'd do what you want. The obvious solution looks like this:

unsafe impl<T: Scan + Copy> Scan for Cell<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        self.get().scan(scanner);
    }
}

But notice the Copy bound. Gc is not copy, and scan is only supposed to note Gcs that you own. So effectively this is equivalent to an empty scan:

unsafe impl<T: Scan + Copy> Scan for Cell<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) { }
}

The other option would be to delve into unsafe code and write an impl based on cell.as_ptr, which could be made safe by placing additional constraints on the implementation of scan.

Do you have a usecase for this, and if so, do you need an impl for non-copy types?

Others commented 4 years ago

(@jrmuizel meant to tag you in the above)

jrmuizel commented 4 years ago

An implementation with a Copy bound is fine. The use case is something like:

#[derive(Scan)]
struct A {
   next: Gc<B>,
   count: Cell<i32>,
} 

This currently fails.

Others commented 4 years ago

Published a release to crates.io, if you update to v0.1.2 you'll pick up this change