Expand description
Flow loosely and gracefully.
Where:
S
= type of the sender spsc channel value
R
= type of Ok
value of the Result
(Result<R, Cause>
) and Cause
is the Error
cause.
Quick Example:
use flowync::{error::{Cause, IOError}, Flower};
type TestFlower = Flower<u32, String>;
fn fetch_things(id: usize) -> Result<String, IOError> {
let result =
Ok::<String, IOError>(format!("the flower with id: {} successfully completed fetching.", id));
let success = result?;
Ok(success)
}
fn main() {
let flower: TestFlower = Flower::new(1);
std::thread::spawn({
let handle = flower.handle();
// Activate
handle.activate();
move || {
for i in 0..10 {
// Send current value through channel, will block the spawned thread
// until the option value successfully being polled in the main thread.
handle.send(i);
// or handle.send_async(i).await; can be used from any multithreaded async runtime,
}
let result = fetch_things(handle.id());
// Set result and then extract later.
handle.set_result(result)
}
});
let mut exit = false;
loop {
// Check if the flower is_active()
// and will deactivate itself if the result value successfully received.
if flower.is_active() {
// another logic goes here...
// e.g:
// notify_loading_fn();
flower
.extract(|value| println!("{}", value))
.finalize(|result| {
match result {
Ok(value) => println!("{}", value),
Err(Cause::Suppose(msg)) => {
println!("{}", msg)
}
Err(Cause::Panicked(_msg)) => {
// Handle things if stuff unexpectedly panicked at runtime.
}
}
// Exit if finalized
exit = true;
});
}
if exit {
break;
}
}
}
Implementations
sourceimpl<S, R> Flower<S, R>where
S: Send,
R: Send,
impl<S, R> Flower<S, R>where
S: Send,
R: Send,
pub fn new(id: usize) -> Self
sourcepub fn state(&self) -> FlowerState<S, R>
pub fn state(&self) -> FlowerState<S, R>
Get the state
Since Flower
itself is uncloneable to avoid data races, this is an alternative fn
for self.clone()
sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancel Flower
.
will do nothing if not explicitly configured on the Handle
.
sourcepub fn is_canceled(&self) -> bool
pub fn is_canceled(&self) -> bool
Check if the Flower
is canceled
sourcepub fn result_is_ready(&self) -> bool
pub fn result_is_ready(&self) -> bool
Check if Result
value of the Flower
is ready
sourcepub fn channel_is_present(&self) -> bool
pub fn channel_is_present(&self) -> bool
Check if channel value of the Flower
is present
sourcepub fn try_result(&self, f: impl FnOnce(Result<R, Cause>))
pub fn try_result(&self, f: impl FnOnce(Result<R, Cause>))
Try get the Result
of the Flower
and ignore channel value (if any).
Note: (this fn will be called if only Result
is available)
Warning! don’t use this fn if channel value is important, use extract fn
and then use finalize fn
instead.
Trait Implementations
Auto Trait Implementations
impl<S, R> RefUnwindSafe for Flower<S, R>
impl<S, R> Send for Flower<S, R>
impl<S, R> Sync for Flower<S, R>
impl<S, R> Unpin for Flower<S, R>
impl<S, R> UnwindSafe for Flower<S, R>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more