Service Traits — Defining Interfaces
The first step in defining a service is the trait: the contract between implementations and callers.
Define the interface
#![allow(unused)] fn main() { use id_effect::Effect; pub struct User; pub struct DbError; pub trait UserRepository: Send + Sync + 'static { fn get_user(&self, id: u64) -> Effect<User, DbError>; fn save_user(&self, user: User) -> Effect<(), DbError>; } }
Conventions:
- Methods return
Effect<A, E>—Rdefaults toHNil: the method needs nothing from the environment; callers carry the service itself in theirR. A sub-effect withR = HNilbinds inside anyeffect!. Send + Sync + 'staticon the trait, soArc<dyn UserRepository>can live inRand cross fibers (effects areSend).- Small, verb-oriented methods (
get_user, notusers).
Define the capability type
A capability is any Send + Sync + 'static type; R stores it behind an Arc. For a trait
object, wrap it in a newtype and deref to the trait (this is how
id_effect_platform::http::HttpClientService is built):
#![allow(unused)] fn main() { use std::ops::Deref; use std::sync::Arc; use id_effect::Effect; pub struct User; pub struct DbError; pub trait UserRepository: Send + Sync + 'static { fn get_user(&self, id: u64) -> Effect<User, DbError>; } #[derive(Clone)] pub struct UserRepo(pub Arc<dyn UserRepository>); impl UserRepo { pub fn new(repo: impl UserRepository) -> Self { Self(Arc::new(repo)) } } impl Deref for UserRepo { type Target = dyn UserRepository; fn deref(&self) -> &Self::Target { &*self.0 } } }
The type is the key: there is no separate tag, and two services with the same implementation type are told apart by their newtypes.
Use ~UserRepo in callers
#![allow(unused)] fn main() { pub struct User { pub name: String } pub struct DbError; pub trait UserRepository: Send + Sync + 'static { fn get_user(&self, id: u64) -> id_effect::Effect<User, DbError>; } #[derive(Clone)] pub struct UserRepo(pub std::sync::Arc<dyn UserRepository>); impl std::ops::Deref for UserRepo { type Target = dyn UserRepository; fn deref(&self) -> &Self::Target { &*self.0 } } pub struct UserProfile { pub name: String } impl From<User> for UserProfile { fn from(u: User) -> Self { UserProfile { name: u.name } } } pub enum AppError { Db(DbError) } use id_effect::{Effect, caps, effect}; fn get_user_profile(id: u64) -> Effect<UserProfile, AppError, caps!(UserRepo)> { effect!({ let repo = ~UserRepo; // &Arc<UserRepo>, derefs to dyn UserRepository let user = ~repo.get_user(id).map_err(AppError::Db); UserProfile::from(user) }) } }
Inside effect!, ~UserRepo reads the capability and ~expr binds a sub-effect. ~expr also
widens the sub-effect's error with From, so with impl From<DbError> for AppError the
.map_err can go. Outside the macro, need::<UserRepo>() is the same read as an effect; a
flat_map keeps one R, so lift the method's HNil effect with .narrow():
need::<UserRepo>().flat_map(move |repo| repo.get_user(id).narrow()).
Keep traits focused
use std::sync::Arc; use id_effect::Effect; struct User; struct AppError; mod bad { use super::*; // BAD — one god trait trait AppService { fn get_user(&self, id: u64) -> Effect<User, AppError>; fn send_email(&self, to: &str, body: &str) -> Effect<(), AppError>; } } // GOOD — separate capabilities trait UserRepository { /* … */ } trait EmailService { /* … */ } struct UserRepo(Arc<dyn UserRepository>); struct Email(Arc<dyn EmailService>); fn main() {}
Functions declare exactly what they need: caps!(UserRepo, Email).