Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic after calling take. #4

Closed
kriomant opened this issue Feb 10, 2023 · 1 comment
Closed

Panic after calling take. #4

kriomant opened this issue Feb 10, 2023 · 1 comment

Comments

@kriomant
Copy link

While trying to implement cell with expiration, I found that following code

/*
[dependencies]
async-once-cell = "*"
tokio = { version = "*", features = ["macros", "rt-multi-thread"] }
*/

#[tokio::main]
async fn main() {
    let mut cell = async_once_cell::OnceCell::new_with(Some(0u64));
    let value = cell.get();
    let _ = cell.take();
    let _ = cell.get_or_init(async { 1 }).await;
    println!("done");
}

playground

causes

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/explorer/.cargo/registry/src/github.com-1ecc6299db9ec823/async-once-cell-0.4.2/src/lib.rs:455:51
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

here:

        // Safety: initialized on all paths
        Ok(unsafe { (&*self.value.get()).as_ref().unwrap() })

It seems not all paths ensures value is initialised.

@danieldg
Copy link
Owner

Please check if master fixes this; I just noticed that there's a commit to fix this issue but it wasn't included in 0.4.2. I'll release 0.4.3 with that later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants