pub struct Flower<SOME, OK> where
SOME: Clone + Send + Sync + 'static,
OK: Clone + Send + Sync + 'static, { /* private fields */ }Expand description
Flow loosely and gracefully.
Where:
SOME: type of sender (channel) value
OK: type of Ok value of the Result (Result<‘OK’, String>, and Err value always return String)
Quick Example:
use flowync::Flower;
fn main() {
let flower = Flower::<i32, String>::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,
// // Return error if the job is failure, for example:
// if i >= 3 {
// return handle.err("Err".to_string());
// }
}
// And return ok if the job successfully completed.
return handle.ok("Ok".to_string());
}
});
let mut exit = false;
loop {
// instead of polling the mutex over and over,
// the fn will be activated automatically if the handle sending or return a value
// and will deactivate itself if the result value successfully received.
// Note: this fn is non-blocking (won't block the current thread).
if flower.is_active() {
flower.try_recv(|channel| {
if let Some(value) = channel {
println!("{}", value);
}
}).on_complete(|result| match result {
Ok(value) => println!("{}", value),
Err(err_msg) => println!("{}", err_msg),
});
}
if exit {
break;
}
}
}Implementations
sourceimpl<SOME, OK> Flower<SOME, OK> where
SOME: Clone + Send + Sync + 'static,
OK: Clone + Send + Sync + 'static,
impl<SOME, OK> Flower<SOME, OK> where
SOME: Clone + Send + Sync + 'static,
OK: Clone + Send + Sync + 'static,
pub fn new(id: usize) -> Self
sourcepub fn handle(&self) -> FlowerHandle<SOME, OK>
pub fn handle(&self) -> FlowerHandle<SOME, OK>
Get handle of the flower.
sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancel current flower handle.
will do nothing if not explicitly configured.
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_recv(&self, f: impl FnOnce(Option<SOME>)) -> &Self
pub fn try_recv(&self, f: impl FnOnce(Option<SOME>)) -> &Self
Try receive the flower channel value
sourcepub fn on_complete(&self, f: impl FnOnce(Result<OK, String>))
pub fn on_complete(&self, f: impl FnOnce(Result<OK, String>))
Process the flower result
Trait Implementations
sourceimpl<SOME, OK> Clone for Flower<SOME, OK> where
SOME: Clone + Send + Sync + 'static,
OK: Clone + Send + Sync + 'static,
impl<SOME, OK> Clone for Flower<SOME, OK> where
SOME: Clone + Send + Sync + 'static,
OK: Clone + Send + Sync + 'static,
Auto Trait Implementations
impl<SOME, OK> !RefUnwindSafe for Flower<SOME, OK>
impl<SOME, OK> Send for Flower<SOME, OK>
impl<SOME, OK> Sync for Flower<SOME, OK>
impl<SOME, OK> Unpin for Flower<SOME, OK>
impl<SOME, OK> !UnwindSafe for Flower<SOME, OK>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more