Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create replies directly out of handle_cmd using closure #219

Merged
merged 9 commits into from Apr 10, 2024

Conversation

jrudolph
Copy link
Collaborator

@jrudolph jrudolph commented Apr 9, 2024

We discussed a similar solution before in #201 (comment)

It now came up again that make_reply is hard to use when you need access to the event. In that case the previous solution required manual correlation of the event created in handle_cmd with the event passed to make_reply. The only way would to unpack the right event type would be to panic on all the other event cases which is inconvenient (in simple cases) and a potential for runtime panics (in more complex cases).

/cc @qrilka

eventsourced/src/lib.rs Outdated Show resolved Hide resolved
eventsourced/src/lib.rs Outdated Show resolved Hide resolved
eventsourced/src/lib.rs Outdated Show resolved Hide resolved

fn make_reply(&self, id: &E::Id, state: &E, evt: E::Evt) -> BoxedAny {
Box::new(self.make_reply(id, state, evt))
fn handle_cmd(self: Box<Self>, id: &E::Id, state: &E) -> BoxedCmdEffect<E> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL self: Box<Self> to allow owning Box<dyn ...> values in trait functions.

@hseeberger
Copy link
Owner

Works fine here, too: hseeberger/rusty-accounts#25

@hseeberger
Copy link
Owner

Looking good! Even though for replies one still might have to match on state (and panic on the wrong variants), there are some significant improvements, e.g. the handlers consuming cmd/evt.

@hseeberger hseeberger merged commit 9672c1b into hseeberger:main Apr 10, 2024
7 checks passed
@github-actions github-actions bot mentioned this pull request Apr 10, 2024
@jrudolph jrudolph deleted the feat/simpler-replies branch April 10, 2024 15:10
@jrudolph
Copy link
Collaborator Author

Even though for replies one still might have to match on state (and panic on the wrong variants),

Ugh, that's still ugly but probably little we can do about that.

This was referenced Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants