Unsafe code review request for cycle collector


I've recently published my first crate GitHub - frengor/rust-cc: Rust cycle collector., which implements a cycle collector in Rust.
This is also my first Rust program using unsafe, so any type of advice is welcome!

All tests pass successfully under Miri. However, my greatest worry is about the usage of UnsafeCells and the new_cyclic function. For example:

let returned: Cc<MyData> = Cc::new_cyclic(|not_yet_init_cc: &Cc<MyData>| { // types left for clearness
    // Trying to dereference not_yet_init_cc here will panic, since the data inside is uninit
    not_yet_init_cc.deref(); // Panics!

    // Cloning doesn't panic however
    let cloned = not_yet_init_cc.clone();

    MyData { ... } // Data with which not_yet_init_cc will be initialized
}); // new_cyclic then returns the initialized not_yet_init_cc

My fear is that it may be possible to trigger UB calling other methods like clone inside the closure.

Also, is there any unsoundness?

Thank you in advance!

Just released version 0.1.1. Thanks to @LegionMammal978 who reduced the amount of unsafe converting an UnsafeCell to a safer Cell.

If any of you have other suggestions they're really welcome!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.