Search results

There are no results.

std.sync.Promise

type pub Promise[T]

The writing half of a future.

A Promise is used to write a value to a future, such that a corresponding Future can be resolved into this value.

Instance methods

set

Show source code
Hide source code
fn pub move set(value: uni T) -> Option[uni T] {
  let val = Option.Some(value)
  let fut = lock

  match fut.status {
    case NoFuture -> {
      fut.unlock

      # Ensure the shared state isn't dropped.
      _INKO.moved(fut)
      return val
    }
    case _ -> {}
  }

  let waiter = fut.waiter := NO_WAITER as Pointer[UInt8]

  fut.value = val
  fut.unlock

  # Ensure the shared state isn't dropped.
  _INKO.moved(fut)

  # If the waiter is waiting for a value, we have to reschedule it.
  if waiter as Int != NO_WAITER {
    inko_process_reschedule_for_value(_INKO.state, _INKO.process, waiter)
  }

  Option.None
}
fn pub move set(value: uni T) -> Option[uni T]

Sets the value of the Future that belongs to this Promise to the given value.

This method consumes self as to ensure a value can only be written once.

This method never blocks the calling process.

Disconnected writes

If the corresponding Future is dropped, this method returns the value wrapped in an Option.Some, otherwise an Option.None is returned. This allows callers to detect a disconnected Promise and act accordingly, such as by storing the value elsewhere.

Examples

Resolving a Future using a Promise:

import std.sync (Future)

match Future.new {
  case (future, promise) -> {
    promise.set(42) # => Option.None
    future.get      # => 42
  }
}

Trying to resolve a dropped Future:

import std.sync (Future)

match Future.new {
  case (future, promise) -> {
    drop(future)
    promise.set(42) # => Option.Some(42)
  }
}

Implemented traits

std.drop.

Drop

impl Drop for Promise