Struct fuze::Fuze[][src]

pub struct Fuze<T> { /* fields omitted */ }
Expand description

A mechanism to wait for a single signal which can be checked at any time.

A Fuze can be checked synchronously, and will show up as burnt only once it’s been burnt. It can be awaited while it’s unburnt, and it can be Cloned and used without mut at any time.

Useful for exit conditions and as a one-off no-payload channel.

Example

Basic usage:

use fuze::Fuze;
use async_std::task;
use std::time::Duration;

let f1 = Fuze::new();
assert_eq!(f1.burnt(), false);

let f2 = f1.clone();
task::block_on(async move {
	assert_eq!(f2.burnt(), false);

	let f3 = f2.clone();
	task::spawn(async move {
		assert_eq!(f3.burnt(), false);

		task::sleep(Duration::from_secs(1)).await;

		// this first burn unblocks anything that wait()s on the fuze:
		f3.burn();

		assert_eq!(f3.burnt(), true);

		// this second burn does nothing:
		f3.burn();

		assert_eq!(f3.burnt(), true);
	});

	// this call will block until the f3.burn() call above:
	println!("first");
	f2.wait().await;
	assert_eq!(f2.burnt(), true);

	// now that the fuze is burnt, this call returns immediately:
	println!("second");
	f2.wait().await;
});

assert_eq!(f1.burnt(), true);

With an existing channel:

use fuze::Fuze;
use async_std::{channel, task};
use std::time::Duration;

let (s, r) = channel::unbounded::<bool>();
let f1 = Fuze::with_channel(s, r);
let f2 = f1.clone();
task::block_on(async move {
	assert_eq!(f2.burnt(), false);

	let f3 = f2.clone();
	task::spawn(async move {
		assert_eq!(f3.burnt(), false);
		task::sleep(Duration::from_secs(1)).await;
		f3.burn();
		assert_eq!(f3.burnt(), true);
	});

	f2.wait().await;
	assert_eq!(f2.burnt(), true);
});

assert_eq!(f1.burnt(), true);

Implementations

Creates a new unburnt fuze.

Creates a fuze from an existing channel.

Note that the fuze can then be burnt if the channel is closed outside of the fuze’s purview.

Burns the fuze, unblocking everything that is waiting on it.

Checks whether the fuze is burnt or not.

Blocks (async-ly) until the fuze is burnt, then never blocks again.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Creates a new unburnt fuze.

Examples

Basic usage:

let fuze = Fuze::<bool>::default();
assert_eq!(fuze.burnt(), false);
fuze.burn();
assert_eq!(fuze.burnt(), true);

For Fuze<()> you should probably use Fuze::new() instead.

let fuze = Fuze::new();
assert_eq!(fuze.burnt(), false);
fuze.burn();
assert_eq!(fuze.burnt(), true);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.