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

Introduce Failure type for scheduler replies #465

Merged
merged 2 commits into from
May 24, 2024
Merged

Conversation

lukeyeh
Copy link
Member

@lukeyeh lukeyeh commented May 11, 2024

This PR addresses the issue of handling both Trap and Error within the same try-block in the scheduler reply, resolving #463. Currently, the scheduler reply has the signature Result<Result<T, Error>, Trap>, which does not work well with try-blocks that only support one level of Result.

To resolve this, we introduce a new Failure enum that can represent either a Trap or an Error. The Failure type is defined in failure.rs, along with the Trap function. We also implement From traits for both Trap and Error to allow seamless conversion into the Failure type.

By updating the scheduler reply signature to Result<T, Failure>, we can now use the ? operator for both Trap and Error within the same try-block, making the code more concise and easier to read.

Copy link
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Sorry, I probably used a misleading title in #463. This PR is not fixing the core idea of #463 but just introducing an unused private type, which is not useful on its own. Instead, for this PR to fix #463, it should:

  • Introduce the Failure type and its 2 implementations at top-level (the enabler).
  • Change the SchedulerCall::reply() function to take a Result<_, Failure> instead (the actual change).
  • Fix any compilation error when calling reply() (the consequences).
  • Improve the try-blocks that currently have to juggle to return an Error (the benefit). This juggling can probably be identified by explicit use of Ok and Err within or right after the try-block .

enum Failure {
Trap(Trap),
Error(Error),
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need a new file just for those 3 items. The Failure type and its implementations can simply be in the lib.rs next to the Trap type. In particular we don't need tests. Those are just normal usage of the type within the rest of the crate, so the tests are redundant.

Copy link
Member Author

Choose a reason for hiding this comment

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

moved Trap back into lib.rs and added the Failure enum there too.

@@ -4,6 +4,7 @@

### Minor

- Add `Failure` type
Copy link
Member

Choose a reason for hiding this comment

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

This is not a minor change. This is purely an implementation detail, so a patch change. And the change should be something along the lines of "Simplify try-blocks for API call replies with a common Failure type".

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to patch, but kept the description the same as I plan to do the other parts in following PRs. Please let me know if you would like me to do all of the steps in a single PR.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think it should all be in a single PR, since the changes independently don't make sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @ia0, I changed Scheduler::reply() to take the new Result<_, Failure> type as an argument and fixed all the compiler errors.

@lukeyeh
Copy link
Member Author

lukeyeh commented May 11, 2024

Sorry, I probably used a misleading title in #463. This PR is not fixing the core idea of #463 but just introducing an unused private type, which is not useful on its own. Instead, for this PR to fix #463, it should:

* Introduce the `Failure` type and its 2 implementations at top-level (the enabler).

* Change the `SchedulerCall::reply()` function to take a `Result<_, Failure>` instead (the actual change).

* Fix any compilation error when calling `reply()` (the consequences).

* Improve the try-blocks that currently have to juggle to return an `Error` (the benefit). This juggling can probably be identified by explicit use of `Ok` and `Err` within or right after the try-block .

I mainly wanted to add tests because I wanted to have this PR just add the failure type and following PRs to change ScheduelrCall::reply's signature and improve the try blocks. I didn't want to have to add a #[allow(dead_code)] marker, so added some tests. I moved the Trap and Failure back to lib.rs and left a TODO instead.

Copy link
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good. Most of the comments are the same and I did not mention it in every file, so you'll have to search for them.

crates/scheduler/src/lib.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/platform/update.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/store.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/store.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/store.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/store.rs Outdated Show resolved Hide resolved
@lukeyeh lukeyeh force-pushed the failure branch 2 times, most recently from acd2525 to 672932c Compare May 21, 2024 19:35
@lukeyeh
Copy link
Member Author

lukeyeh commented May 21, 2024

Addressed comments and applied changes to equivalent areas that you didn't leave comments PTAL

Copy link
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Thanks! There's one more pattern I missed: foo().map(|x| x.bar())? can be foo()?.bar().

crates/scheduler/CHANGELOG.md Outdated Show resolved Hide resolved
crates/scheduler/src/call.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/ec.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/timer.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/uart.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/usb/serial.rs Outdated Show resolved Hide resolved
@lukeyeh
Copy link
Member Author

lukeyeh commented May 22, 2024

went through the new pass of comments with the new pattern and other various nits

Copy link
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Thanks! There's 2 occurrences of the map()? that seems to have been missed, otherwise everything else is good to merge now.

crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
crates/scheduler/src/call/crypto/hash.rs Outdated Show resolved Hide resolved
Copy link
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Thanks!

@ia0 ia0 linked an issue May 24, 2024 that may be closed by this pull request
@ia0 ia0 merged commit 7389b22 into google:main May 24, 2024
19 checks passed
@ia0 ia0 added for:maintainability Improves maintainers life crate:scheduler Modifies the platform labels May 24, 2024
@lukeyeh lukeyeh deleted the failure branch May 24, 2024 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate:scheduler Modifies the platform for:maintainability Improves maintainers life
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify try-blocks in scheduler for API call replies
2 participants