0

cell-refcell-rc

 1 year ago
source link: https://gist.github.com/jonhoo/7cfdfe581e5108b79c2a4e9fbde38de8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

cell-refcell-rc · GitHub

Hi, Jon, Great great tutorial! love it.

One question:

// TODO: #[may_dangle]
impl<T> Drop for Rc<T> {
    fn drop(&mut self) {
        let inner = unsafe { self.inner.as_ref() };
        let c = inner.refcount.get();
        if c == 1 {
            drop(inner);
            // SAFETY: we are the _only_ Rc left, and we are being dropped.
            // therefore, after us, there will be no Rc's, and no references to T.
            let _ = unsafe { Box::from_raw(self.inner.as_ptr()) };
        } else {
            // there are other Rcs, so don't drop the Box!
            inner.refcount.set(c - 1);
        }
    }
}

why this one have a may_dangle issue?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK