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
// Copyright (c) 2020 Timo Savola.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

//! Binding implementation support.

use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;

use lep::{Obj, Res};

pub enum FutState {
    Pending(Pin<Box<dyn Future<Output = Res>>>),
    Done(Res),
    Unknown,
}

/// Asynchronous result.
pub type Fut = Cell<FutState>;

/// Wrap an asynchronous result into a Lep object.
pub fn future_obj<T: Future<Output = Res> + 'static>(future: T) -> Obj {
    Rc::new(Cell::new(FutState::Pending(Box::pin(future))) as Fut)
}

/// Unwrap an asynchronous result if the object contains one.
///
/// Otherwise the original object is returned.
pub async fn obj_future(x: &Obj) -> Res {
    if let Some(cell) = x.downcast_ref::<Fut>() {
        match cell.replace(FutState::Unknown) {
            FutState::Pending(mut b) => {
                let res = b.as_mut().await;
                cell.set(FutState::Done(res.clone()));
                res
            }
            FutState::Done(res) => {
                cell.set(FutState::Done(res.clone()));
                res
            }
            FutState::Unknown => Err("future is unknown".to_string()),
        }
    } else {
        Ok(x.clone())
    }
}