pub struct Decider<C, S: State, D> { /* private fields */ }Expand description
A decision on a command, given the state of its subject: the events to write, or an error if the command is refused.
Create one from a function that receives the command and the state, and returns the events in a future, so that it can load what else it needs to decide, such as the state of another subject:
use architecturekit::{Decider, DomainError};
let borrow_book = Decider::new(|cmd: BorrowBook, book: Book| async move {
if book.is_borrowed {
return Err(DomainError::new(format!("book {} is already borrowed", cmd.book_id)));
}
Ok(vec![BookEvent::Borrowed(BookBorrowed { reader: cmd.reader })])
});The types of the command, the state and the error come from the function,
so that none of them has to be named. A decider that never refuses names
its error type, as in Decider::<_, _, DomainError>::new(…).
The function takes the command and the state rather than references to them, so that its future can be sent across threads, which a web server needs.
Implementations§
Source§impl<C, S: State, D> Decider<C, S, D>
impl<C, S: State, D> Decider<C, S, D>
Sourcepub fn new<F, Fut>(decide: F) -> Self
pub fn new<F, Fut>(decide: F) -> Self
Creates a decider from a function that decides on a command, given the state of its subject.
Sourcepub async fn decide(
&self,
command: C,
state: S,
) -> Result<Vec<<S as State>::Event>, D>
pub async fn decide( &self, command: C, state: S, ) -> Result<Vec<<S as State>::Event>, D>
Decides on a command, given the state of its subject, with the function the decider was created with, and returns what that function returns.
§Errors
Fails with the error of the function, if it refuses the command.
Trait Implementations§
Auto Trait Implementations§
impl<C, S, D> !RefUnwindSafe for Decider<C, S, D>
impl<C, S, D> !UnwindSafe for Decider<C, S, D>
impl<C, S, D> Freeze for Decider<C, S, D>
impl<C, S, D> Send for Decider<C, S, D>
impl<C, S, D> Sync for Decider<C, S, D>
impl<C, S, D> Unpin for Decider<C, S, D>
impl<C, S, D> UnsafeUnpin for Decider<C, S, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request