5 releases
0.2.2 | Jan 14, 2024 |
---|---|
0.2.1 | Aug 14, 2023 |
0.2.0 | Aug 10, 2023 |
0.1.1 | Aug 8, 2023 |
0.1.0 | Jul 30, 2023 |
#668 in Concurrency
117 downloads per month
Used in 6 crates
(4 directly)
13KB
222 lines
swctx
Set/Wait Context (swctx) is a one-shot channel-like construct with some special semantics.
lib.rs
:
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
.
Dependencies
~0.4–6.5MB
~11K SLoC