I'm using once_cell::sync::Lazy
and tokio_util::sync::CancellationToken
in the following code and don't understand why CANCEL.is_cancelled()
returns false
.
use once_cell::sync::Lazy;
use tokio_util::sync::{CancellationToken, DropGuard};
const CANCEL: Lazy<CancellationToken> = Lazy::new(CancellationToken::new);
fn main() {
let cancel: &CancellationToken = &*CANCEL;
let cancel2: CancellationToken = cancel.clone();
let cancel3: CancellationToken = cancel2.clone();
let cancel4: CancellationToken = cancel3.clone();
let cancel_guard: DropGuard = cancel4.drop_guard();
drop(cancel_guard);
println!("CANCEL: {}", CANCEL.is_cancelled()); // why does this report `false`?
println!("cancel: {}", cancel.is_cancelled());
println!("cancel2: {}", cancel2.is_cancelled());
println!("cancel3: {}", cancel2.is_cancelled());
}
Output:
CANCEL: false
cancel: true
cancel2: true
cancel3: true