pub struct Flower<S, R>where
    S: Send,
    R: Send,
{ /* private fields */ }
Expand description

Flow loosely and gracefully.

Where:

S = type of sender (channel) value

R = type of Ok value of the Result (Result<R, String>, and Err value always return String)

Quick Example:

 use flowync::{Flower, IOError};
 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(|channel| {
                     // Poll channel
                     if let Some(value) = channel {
                         println!("{}", value);
                     }
                 })
                 .finalize(|result| {
                     match result {
                         Ok(value) => println!("{}", value),
                         Err(err_msg) => println!("{}", err_msg),
                     }
                     // Exit if finalized
                     exit = true;
                 });
         }

         if exit {
             break;
         }
     }
 }

Implementations

Get ID of the flower.

Get handle of the flower.

Cancel current flower handle.

will do nothing if not explicitly configured.

Check if the flower is canceled

Check if the current flower is active

Check if result value of the flower is ready

Check if channel value of the flower is present

👎Deprecated: result is highly deprecated, use try_result fn is instead

Try get the result of the flower and ignore channel value (if any).

Warning! don’t use this fn if channel value is important, use extract fn and then use finalize fn instead.

Try extract channel value of the flower if available, and then finalize (must_use)

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
Executes the destructor for this type. Read more

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.