Crate swctx

source ·
Expand description

swctx is very similar to a one-shot channel, but with some added semantics.

use std::thread;
use swctx::mkpair;

let (sctx, wctx) = mkpair::<&str, &str, &str>();
let jh = thread::spawn(move || {
  sctx.set_state("in thread");
  sctx.set("hello");
});
jh.join().unwrap();

assert_eq!(wctx.wait().unwrap(), "hello");

In a typical use-case an application or library calls mkpair() to create a pair of linked SetCtx and WaitCtx object. The SetCtx object is transferred to a remote thread/task, and the WaitCtx is used wait for an object to arrive [from the thread/task the SetCtx is sent to].

Once the thread/task has data to send back to the WaitCtx it calls SetCtx::set() to send the data.

The SetCtx has an internal state, settable using SetCtx::set_state() that will be reported back to the WaitCtx, which will return Error::Aborted, if the SetCtx is dropped prematurely.

The SetCtx can also signal a failure by calling SetCtx::fail() and pass along an application-specific error code. This will cause the WaitCtx to unblock and return Error::App.

Structs

  • End-point used to send value to the paired WaitCtx.
  • End-point used to wait for a value to be sent from the paired SetCtx.
  • Used to wait for the paired SetCtx to set a value in an async context.

Enums

  • Errors that can be returned by the swctx library.

Functions