1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#![deny(unsafe_code)]
use core::{
    fmt::{self, Debug, Formatter},
    future::Future,
    pin::Pin,
    sync::atomic::{AtomicBool, Ordering},
    task::{Context, Poll, Waker},
};
use std::{
    error::Error,
    mem,
    sync::{Condvar, Mutex},
};
use std::{sync::Arc, thread};

#[cfg(feature = "compact")]
mod compact;
#[cfg(feature = "compact")]
pub use compact::*;

use error::Cause;
pub mod error;
enum TypeOpt<S, R>
where
    S: Send,
    R: Send,
{
    Channel(S),
    Success(R),
    Error(Cause),
    None,
}

impl<S, R> Default for TypeOpt<S, R>
where
    S: Send,
    R: Send,
{
    fn default() -> Self {
        Self::None
    }
}

impl<S, R> Debug for TypeOpt<S, R>
where
    S: Send + Debug,
    R: Send + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Channel(s) => f.debug_tuple("Channel").field(s).finish(),
            Self::Success(r) => f.debug_tuple("Success").field(r).finish(),
            Self::Error(e) => f.debug_tuple("Error").field(e).finish(),
            Self::None => write!(f, "None"),
        }
    }
}

impl<S, R> TypeOpt<S, R>
where
    S: Send,
    R: Send,
{
    fn take(&mut self) -> Self {
        mem::take(self)
    }
}

struct InnerState<S, R>
where
    S: Send,
    R: Send,
{
    activated: AtomicBool,
    result_ready: AtomicBool,
    channel_present: AtomicBool,
    mtx: Mutex<TypeOpt<S, R>>,
    cvar: Condvar,
    canceled: AtomicBool,
}

impl<S, R> Debug for InnerState<S, R>
where
    S: Send + Debug,
    R: Send + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("InnerState")
            .field("result_ready", &self.result_ready)
            .field("channel_present", &self.channel_present)
            .field("mtx", &self.mtx)
            .field("cvar", &self.cvar)
            .field("canceled", &self.canceled)
            .field("activated", &self.activated)
            .finish()
    }
}

impl<S, R> Drop for InnerState<S, R>
where
    S: Send,
    R: Send,
{
    fn drop(&mut self) {}
}

/// State of the `Flower`
pub struct FlowerState<S, R>
where
    S: Send,
    R: Send,
{
    state: Arc<InnerState<S, R>>,
    async_suspender: Arc<(Mutex<Option<Waker>>, AtomicBool)>,
    id: usize,
}

impl<S, R> Debug for FlowerState<S, R>
where
    S: Send + Debug,
    R: Send + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("FlowerState")
            .field("state", &self.state)
            .field("async_suspender", &self.async_suspender)
            .field("id", &self.id)
            .finish()
    }
}

impl<S, R> FlowerState<S, R>
where
    S: Send,
    R: Send,
{
    /// Get ID of the `Flower`.
    pub fn id(&self) -> usize {
        self.id
    }

    /// Cancel `Flower`.
    ///
    /// will do nothing if not explicitly configured on the `Handle`.
    pub fn cancel(&self) {
        self.state.canceled.store(true, Ordering::Relaxed);
    }

    /// Check if the `Flower` is canceled
    pub fn is_canceled(&self) -> bool {
        self.state.canceled.load(Ordering::Relaxed)
    }

    /// Check if the current `Flower` is active
    pub fn is_active(&self) -> bool {
        self.state.activated.load(Ordering::Relaxed)
    }
}

impl<S, R> Clone for FlowerState<S, R>
where
    S: Send,
    R: Send,
{
    fn clone(&self) -> Self {
        Self {
            state: Clone::clone(&self.state),
            async_suspender: Clone::clone(&self.async_suspender),
            id: self.id,
        }
    }
}

impl<S, R> Drop for FlowerState<S, R>
where
    S: Send,
    R: Send,
{
    fn drop(&mut self) {}
}

struct AsyncSuspender {
    inner: Arc<(Mutex<Option<Waker>>, AtomicBool)>,
}

impl Future for AsyncSuspender {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut mtx = self.inner.0.lock().unwrap();
        if !self.inner.1.load(Ordering::Relaxed) {
            Poll::Ready(())
        } else {
            *mtx = Some(cx.waker().clone());
            Poll::Pending
        }
    }
}

/// A handle for the Flower
pub struct Handle<S, R>
where
    S: Send,
    R: Send,
{
    state: Arc<InnerState<S, R>>,
    async_suspender: Arc<(Mutex<Option<Waker>>, AtomicBool)>,
    id: usize,
}

impl<S, R> Handle<S, R>
where
    S: Send,
    R: Send,
{
    /// Get ID of the `Flower`.
    pub fn id(&self) -> usize {
        self.id
    }

    /// Activate current `Flower`
    pub fn activate(&self) {
        self.state.activated.store(true, Ordering::Relaxed);
    }

    /// Check if the current `Flower` is active
    pub fn is_active(&self) -> bool {
        self.state.activated.load(Ordering::Relaxed)
    }

    /// Check if the current `Flower` should be canceled
    pub fn should_cancel(&self) -> bool {
        self.state.canceled.load(Ordering::Relaxed)
    }

    /// Send current progress value
    pub fn send(&self, s: S) {
        let mut mtx = self.state.mtx.lock().unwrap();
        {
            *mtx = TypeOpt::Channel(s);
            self.state.channel_present.store(true, Ordering::Relaxed);
            self.async_suspender.1.store(false, Ordering::Relaxed);
        }
        drop(self.state.cvar.wait(mtx));
    }

    /// Send current progress value asynchronously.
    pub async fn send_async(&self, s: S) {
        {
            *self.state.mtx.lock().unwrap() = TypeOpt::Channel(s);
            self.async_suspender.1.store(true, Ordering::Relaxed);
            self.state.channel_present.store(true, Ordering::Relaxed);
        }
        AsyncSuspender {
            inner: self.async_suspender.clone(),
        }
        .await
    }

    /// Set `Result` value with verboser error message.
    ///
    /// (for more easier to keep in track with the real cause of the error)
    pub fn set_result(&self, r: Result<R, Box<dyn Error>>) {
        match r {
            Ok(val) => self.success(val),
            Err(e) => self.error_verbose(e),
        }
    }

    /// Set `Result` value with no verbose (simpler error message)
    pub fn set_result_no_verbose(&self, r: Result<R, Box<dyn Error>>) {
        match r {
            Ok(val) => self.success(val),
            Err(e) => self.error(e),
        }
    }

    /// Set the `Ok` value of the `Result`.
    pub fn success(&self, r: R) {
        *self.state.mtx.lock().unwrap() = TypeOpt::Success(r);
        self.state.result_ready.store(true, Ordering::Relaxed);
    }

    /// Set the `Err` value of the `Result`.
    pub fn error(&self, e: impl ToString) {
        *self.state.mtx.lock().unwrap() = TypeOpt::Error(Cause::Suppose(e.to_string()));
        self.state.result_ready.store(true, Ordering::Relaxed);
    }

    /// Set the `Err` value of the `Result` with more verboser error message.
    pub fn error_verbose(&self, e: Box<dyn Error>) {
        let err_kind = format!("{:?}", e);
        *self.state.mtx.lock().unwrap() = TypeOpt::Error(Cause::Suppose(err_kind));
        self.state.result_ready.store(true, Ordering::Relaxed);
    }
}

impl<S, R> Drop for Handle<S, R>
where
    S: Send,
    R: Send,
{
    fn drop(&mut self) {
        if thread::panicking() && !self.state.result_ready.load(Ordering::Relaxed) {
            self.state.channel_present.store(false, Ordering::Relaxed);
            let err = format!("the flower handle with id: {} error panicked!", self.id);
            *self.state.mtx.lock().unwrap() = TypeOpt::Error(Cause::Panicked(err));
            self.state.result_ready.store(true, Ordering::Relaxed);
        }
    }
}

impl<S, R> Debug for Handle<S, R>
where
    S: Send + Debug,
    R: Send + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Handle")
            .field("state", &self.state)
            .field("awaiting", &self.async_suspender)
            .field("id", &self.id)
            .finish()
    }
}

pub enum Finalizer<'a, S: Send, R: Send> {
    Try(&'a Flower<S, R>),
}

impl<S, R> Finalizer<'_, S, R>
where
    S: Send,
    R: Send,
{
    /// Try finalize `Result` of the `Flower` (this fn will be called if only `Result` is available).
    pub fn finalize(self, f: impl FnOnce(Result<R, Cause>)) {
        let Self::Try(flower) = self;
        if flower.state.result_ready.load(Ordering::Relaxed) {
            let result = move || {
                let result = flower.state.mtx.lock().unwrap().take();
                flower.state.result_ready.store(false, Ordering::Relaxed);
                flower.state.activated.store(false, Ordering::Relaxed);
                result
            };
            let result = result();
            if let TypeOpt::Success(value) = result {
                f(Ok(value))
            } else if let TypeOpt::Error(err_type) = result {
                f(Err(err_type))
            }
        }
    }
}

/// 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;
///         }
///     }
/// }
/// ```
pub struct Flower<S, R>
where
    S: Send,
    R: Send,
{
    state: Arc<InnerState<S, R>>,
    async_suspender: Arc<(Mutex<Option<Waker>>, AtomicBool)>,
    id: usize,
}

impl<S, R> Flower<S, R>
where
    S: Send,
    R: Send,
{
    pub fn new(id: usize) -> Self {
        Self {
            state: Arc::new(InnerState {
                activated: AtomicBool::new(false),
                result_ready: AtomicBool::new(false),
                channel_present: AtomicBool::new(false),
                mtx: Mutex::new(TypeOpt::None),
                cvar: Condvar::new(),
                canceled: AtomicBool::new(false),
            }),
            async_suspender: Arc::new((Mutex::new(None), AtomicBool::new(false))),
            id,
        }
    }

    /// Get the ID.
    pub fn id(&self) -> usize {
        self.id
    }

    /// Get the handle.
    pub fn handle(&self) -> Handle<S, R> {
        self.state.canceled.store(false, Ordering::Relaxed);
        Handle {
            state: Clone::clone(&self.state),
            async_suspender: Clone::clone(&self.async_suspender),
            id: self.id,
        }
    }

    /// Get the state
    ///
    /// Since `Flower` itself is uncloneable to avoid data races, this is an alternative `fn` for `self.clone()`
    pub fn state(&self) -> FlowerState<S, R> {
        self.state.canceled.store(false, Ordering::Relaxed);
        FlowerState {
            state: Clone::clone(&self.state),
            async_suspender: Clone::clone(&self.async_suspender),
            id: self.id,
        }
    }

    /// Cancel `Flower`.
    ///
    /// will do nothing if not explicitly configured on the `Handle`.
    pub fn cancel(&self) {
        self.state.canceled.store(true, Ordering::Relaxed);
    }

    /// Check if the `Flower` is canceled
    pub fn is_canceled(&self) -> bool {
        self.state.canceled.load(Ordering::Relaxed)
    }

    /// Check if the current `Flower` is active
    pub fn is_active(&self) -> bool {
        self.state.activated.load(Ordering::Relaxed)
    }

    /// Check if `Result` value of the `Flower` is ready
    pub fn result_is_ready(&self) -> bool {
        self.state.result_ready.load(Ordering::Relaxed)
    }

    /// Check if channel value of the `Flower` is present
    pub fn channel_is_present(&self) -> bool {
        self.state.channel_present.load(Ordering::Relaxed)
    }

    /// 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.
    pub fn try_result(&self, f: impl FnOnce(Result<R, Cause>)) {
        if self.state.channel_present.load(Ordering::Relaxed) {
            self.state.cvar.notify_all();
            self.state.channel_present.store(false, Ordering::Relaxed)
        }
        if self.state.result_ready.load(Ordering::Relaxed) {
            let result = move || {
                let result = self.state.mtx.lock().unwrap().take();
                self.state.result_ready.store(false, Ordering::Relaxed);
                self.state.activated.store(false, Ordering::Relaxed);
                result
            };
            let result = result();
            if let TypeOpt::Success(value) = result {
                f(Ok(value))
            } else if let TypeOpt::Error(err_type) = result {
                f(Err(err_type))
            }
        }
    }

    /// Try extract channel value of the `Flower` (this fn will be called if only channel value is available),
    ///
    /// and then `finalize` (must_use)
    pub fn extract(&self, f: impl FnOnce(S)) -> Finalizer<'_, S, R> {
        if self.state.channel_present.load(Ordering::Relaxed) {
            let channel = move || {
                let channel = self.state.mtx.lock().unwrap().take();
                self.state.channel_present.store(false, Ordering::Relaxed);
                if self.async_suspender.1.load(Ordering::Relaxed) {
                    let mut mg_opt_waker = self.async_suspender.0.lock().unwrap();
                    self.async_suspender.1.store(false, Ordering::Relaxed);
                    if let Some(waker) = mg_opt_waker.take() {
                        waker.wake();
                    }
                } else {
                    self.state.cvar.notify_all();
                }
                channel
            };

            if let TypeOpt::Channel(value) = channel() {
                f(value)
            }
        }

        Finalizer::Try(self)
    }

    /// Poll channel value of the `Flower`, and then `finalize` (must_use)
    pub fn poll(&self, f: impl FnOnce(Option<S>)) -> Finalizer<'_, S, R> {
        if self.state.channel_present.load(Ordering::Relaxed) {
            let channel = move || {
                let channel = self.state.mtx.lock().unwrap().take();
                self.state.channel_present.store(false, Ordering::Relaxed);
                if self.async_suspender.1.load(Ordering::Relaxed) {
                    let mut mg_opt_waker = self.async_suspender.0.lock().unwrap();
                    self.async_suspender.1.store(false, Ordering::Relaxed);
                    if let Some(waker) = mg_opt_waker.take() {
                        waker.wake();
                    }
                } else {
                    self.state.cvar.notify_all();
                }
                if let TypeOpt::Channel(value) = channel {
                    Some(value)
                } else {
                    None
                }
            };
            let channel = channel();
            f(channel)
        } else {
            f(None)
        }

        Finalizer::Try(self)
    }
}

impl<S, R> Debug for Flower<S, R>
where
    S: Send + Debug,
    R: Send + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Flower")
            .field("state", &self.state)
            .field("async_suspender", &self.async_suspender)
            .field("id", &self.id)
            .finish()
    }
}

impl<S, R> Drop for Flower<S, R>
where
    S: Send,
    R: Send,
{
    fn drop(&mut self) {}
}

/// A converter to convert `Option<T>` into `Result<T, E>` using `catch` fn.
pub trait IntoResult<T> {
    /// Convert `Option<T>` into `Result<T, E>`
    fn catch(self, error_msg: impl ToString) -> Result<T, Box<dyn Error>>;
}

impl<T> IntoResult<T> for Option<T> {
    fn catch(self, error_msg: impl ToString) -> Result<T, Box<dyn Error>> {
        let message: String = error_msg.to_string();
        match self {
            Some(val) => Ok(val),
            None => Err(message.into()),
        }
    }
}