matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.87k stars 109 forks source link

Add poisoning #76

Closed pitdicker closed 4 years ago

pitdicker commented 5 years ago

Again an experiment, to see the effects of https://github.com/rust-lang/rfcs/pull/2788#discussion_r342484357.

This way of adding poisoning to OnceCell and Lazy seems not all that invasive to me, however it is a large breaking change.

The compiler is usually very helpful with its error message:

   --> tests/test.rs:211:19
    |
211 |                 c.get_or_init(|| 92);
    |                   ^^^^^^^^^^^ -- takes 0 arguments
    |                   |
    |                   expected closure that takes 1 argument
    |
help: consider changing the closure to take and ignore the expected argument
    |
211 |                 c.get_or_init(|_| 92);
    |  

Except when there are manual dereferences of a Lazy:

error[E0614]: type `once_cell::sync::Lazy<_, [closure@tests/test.rs:369:27: 372:10 called:_]>` cannot be dereferenced
   --> tests/test.rs:378:25
    |
378 |                 let y = *x - 30;
    |  

I only did the sync types for now, and also didn't do the parking_lot implementation properly.