The ~ Operator Explained
Inside effect!, the prefix ~ has three forms:
| Form | Meaning |
|---|---|
~expr (an effect) | run the sub-effect on this fiber, evaluate to its success value, stop on failure |
~Cap (a capability type) | read capability Cap from the environment R (an &Arc<Cap>) |
~(a, b, ..) | run up to 8 effects in parallel on child fibers and evaluate to the tuple of values |
Binding an effect
#![allow(unused)] fn main() { use id_effect::{Effect, effect, succeed}; struct User { name: String } fn fetch_user(_: u64) -> Effect<User, String> { succeed(User { name: "ada".into() }) } let _: Effect<String, String> = effect!({ let user = ~fetch_user(42); user.name }) ; }
When you don't need the value, use ~ as a statement:
#![allow(unused)] fn main() { use id_effect::{Effect, effect, succeed}; fn log_event(_: &str) -> Effect<(), String> { succeed(()) } fn do_work() -> Effect<u32, String> { succeed(7) } let _: Effect<u32, String> = effect!({ ~log_event("processing started"); let result = ~do_work(); ~log_event("processing done"); result }) ; }
A bound effect may need only part of the block's R (it is narrowed into the block's
environment), and its error is widened into the block's error: Never converts to anything,
any other E2 needs E: From<E2>.
Reading a capability
#![allow(unused)] fn main() { use id_effect::{Effect, Exit, Never, caps, effect}; struct Counter(i32); fn program() -> Effect<i32, Never, caps!(Counter)> { effect!({ ~Counter.0 + 1 }) } assert_eq!(program().provide(Counter(41)).block_on(), Exit::succeed(42)); }
~Counter reads the Counter in R (an &Arc<Counter>); field accesses and method calls after
it are plain Rust on that reference (~Logger.info(msg)). ~X is a capability read only when X
is an upper-case identifier (not followed by ::, (, { or !); anything else after ~ is
bound as an effect. So when a capability's method returns an effect, read the capability first and
bind the call: let repo = ~UserRepo.clone(); let user = ~repo.get_user(id);. Reading a
capability that is not in R is a compile error naming it.
Joining in parallel
#![allow(unused)] fn main() { use id_effect::{Effect, effect, succeed}; struct User; struct Prefs; struct Page; fn fetch_user(_: u64) -> Effect<User, String> { succeed(User) } fn fetch_prefs(_: u64) -> Effect<Prefs, String> { succeed(Prefs) } fn render(_: User, _: Prefs) -> Page { Page } let id = 1; let page: Effect<Page, String> = effect!({ let (user, prefs) = ~(fetch_user(id), fetch_prefs(id)); render(user, prefs) }) ; assert!(page.block_on().into_result().is_ok()); }
The branches run concurrently; the first failure fails the join and interrupts the siblings.
Method chains on effects
~ applies to the whole expression after it, so combinators go before the bind:
#![allow(unused)] fn main() { use std::time::Duration; use id_effect::{Effect, effect, succeed}; struct User { id: u64 } struct Post; struct DbError; enum AppError { Database(DbError) } impl From<DbError> for AppError { fn from(e: DbError) -> Self { AppError::Database(e) } } fn fetch_user(id: u64) -> Effect<User, DbError> { succeed(User { id }) } fn fetch_posts(_: u64) -> Effect<Vec<Post>, DbError> { succeed(vec![]) } let id = 1; let _: Effect<(User, Option<Vec<Post>>), AppError> = effect!({ let user = ~fetch_user(id).map_err(AppError::Database); let posts = ~fetch_posts(user.id).timeout(Duration::from_secs(2)); // Option<Vec<Post>> (user, posts) }) ; }
~ in conditionals and loops
#![allow(unused)] fn main() { use id_effect::{Effect, effect, succeed}; fn compute_a() -> Effect<i32, String> { succeed(1) } fn compute_b() -> Effect<i32, String> { succeed(2) } fn process_user(_: u64) -> Effect<(), String> { succeed(()) } let condition = true; let user_ids = vec![1, 2, 3]; let _: Effect<i32, String> = effect!({ let value = if condition { ~compute_a() } else { ~compute_b() }; for id in user_ids { ~process_user(id); // sequential: one at a time } value }) ; }
Loops are sequential. For concurrent work over a collection use for_each_par or all
(Chapter 9).
What ~ Cannot Do
~ only works inside an effect! (or effect_local!) body:
#![allow(unused)] fn main() { use id_effect::{Effect, effect, succeed}; struct User; struct DbError; fn fetch_user(_: u64) -> Effect<User, DbError> { succeed(User) } // Does not compile: ~ is not valid here let x = ~fetch_user(42); // Inside effect! let x: Effect<User, DbError> = effect!({ ~fetch_user(42) }); }
Inside a from_async(|| async move { .. }) block you are in plain async Rust: use .await there
and ~ outside it.
Binding a generic helper
~ narrows from any sub-environment, so a helper that is generic in its R leaves nothing to fix
that R. Bind it with .same_env(), which ties it to the caller's environment:
#![allow(unused)] fn main() { use id_effect::{Effect, HList, Selector, caps, effect}; struct AppError; struct Db; struct Logger; impl Logger { fn info(&self, _msg: String) {} } fn audit<R, I>(msg: String) -> Effect<(), AppError, R> where R: HList + Selector<Logger, I>, { effect!({ ~Logger.info(msg) }) } fn signup() -> Effect<(), AppError, caps!(Logger, Db)> { effect!({ ~audit("signup".to_string()).same_env() }) } }
A helper with a concrete caps!(..) needs nothing special.