Skip to main content

Promise

Struct Promise 

Source
pub struct Promise(pub Rc<RefCell<PromiseData>>);
Expand description

A single-assignment value that may not be available yet.

A promise is pending until Promise::resolve gives it a value, and resolved afterwards. Callbacks registered with Promise::then run when it resolves.

  • Resolve once. The first call to resolve wins; later calls are ignored.
  • Never rejects. There is no failure state. Resolving with Value::Undefined stores Value::Nothing instead.
  • Synchronous dispatch. Callbacks run on the caller’s stack, inside resolve (if registered while pending) or inside then (if already resolved). There is no scheduler in this crate.
  • Shared identity. Promise is a cheap handle: clones observe the same resolution.
  • No flattening. If a callback returns a Value::Promise, the child promise resolves to that promise; it is not unwrapped.

The inner Rc<RefCell<PromiseData>> is public so that hosts can inspect the state; prefer then and resolve to change it.

§Examples

use swan_core::value::{Promise, Value};

let p = Promise::new();
let child = p.then(|v| match v {
    Value::Numb(n) => Value::Numb(n + 1.0),
    other => other,
});
p.resolve(Value::Numb(41.0));

if let Value::Promise(child) = child {
    assert_eq!(child.0.borrow().value, Some(Value::Numb(42.0)));
}

Tuple Fields§

§0: Rc<RefCell<PromiseData>>

Implementations§

Source§

impl Promise

Source

pub fn new() -> Self

Creates a new pending promise with no value and no queued callbacks.

Source

pub fn then(&self, f: impl Fn(Value) -> Value + 'static) -> Value

Registers a Rust callback and returns a child promise (as Value::Promise) that resolves to the callback’s result.

  • If self is already resolved, f is called immediately with a clone of the value and the returned child is already resolved.
  • If self is pending, f is queued and runs when self is resolved.

f is invoked at most once. If f returns Value::Undefined, the child resolves to Value::Nothing (see Promise::resolve).

Source

pub fn then_apply(&self, f: Value, pos: usize) -> Value

Like Promise::then, but takes a Swan callable instead of a Rust closure.

The callable f is applied to the resolved value via eval_apply, with pos as the source position used in any resulting Undefined (for example, when f is not callable). Such an Undefined result resolves the child to Value::Nothing.

Source

pub fn resolve(&self, v: Value)

Resolves this promise with v and runs all queued callbacks.

  • Only the first call has any effect; if the promise is already resolved, v is ignored and nothing runs.
  • Value::Undefined(_) is replaced by Value::Nothing, so a promise never holds an undefined value.
  • Each queued callback is called in registration order with a clone of the resolved value, and its result resolves the associated child promise. Resolution therefore cascades down the chain synchronously.
  • The internal borrow is released before callbacks run, so callbacks may safely call then on this promise (they will see it already resolved).

Trait Implementations§

Source§

impl Clone for Promise

Source§

fn clone(&self) -> Promise

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Promise

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.