Skip to content

Flatten rcl error type #183

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

Merged
merged 1 commit into from
Jun 4, 2022
Merged

Flatten rcl error type #183

merged 1 commit into from
Jun 4, 2022

Conversation

nnmm
Copy link
Contributor

@nnmm nnmm commented May 29, 2022

As discussed in chat:

  • The flatter error type is easier to match against
  • The flat model is less code and less noise in the package docs from the many types.
  • The "sub-error types" we had before were not actually useful in most cases (e.g. I can't imagine someone wanting to take some specific action in response to all four NodeError variants).
  • Other client libraries do not have a hierarchy for these error codes either.

I removed the tests because they didn't seem terribly useful, but we can discuss this.

@nnmm nnmm requested review from jhdcs and esteve May 29, 2022 14:35
@nnmm nnmm force-pushed the flatten_errors branch from 980cdc0 to 098f3a2 Compare May 29, 2022 19:08
@nnmm nnmm force-pushed the flatten_errors branch from 098f3a2 to 2980490 Compare May 30, 2022 07:53
@@ -46,7 +46,7 @@ impl Drop for rcl_wait_set_t {
fn drop(&mut self) {
// SAFETY: No preconditions for this function (besides passing in a valid wait set).
let rc = unsafe { rcl_wait_set_fini(self) };
if let Err(e) = to_rcl_result(rc) {
if let Err(e) = to_rclrs_result(rc) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given that TryFrom is implemented, TryInto should already be available for free (https://doc.rust-lang.org/std/convert/trait.TryInto.html).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but isn't using to_rclrs_result nicer here, since this function fetches the error message from rcutils?

Using try_into()/try_from() also makes this call site more complicated. It would have to be something like this:

        match RclReturnCode::try_from(rc) {
            Err(e) => panic!("Unable to release WaitSet. Unknown error code {:?}", e),
            Ok(e) if e != RclReturnCode::Ok => panic!("Unable to release WaitSet. {:?}", e),
        }

Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks much nicer and more idiomatic as it leverages TryInto:

if let Err(e) = TryInto::<RclReturnCode>::try_into(rc) {
    panic!("Unable to release WaitSet. {:?}", e)
}

And then we can get rid of to_rclrs_result entirely and incorporate it into TryFrom

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This does not do what you mean. TryInto is just Err if the error code is unknown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you mean. It somehow feels like a code smell that we have this function outside all the other conversion traits and only used in one specific place. If we can find a way to refactor this in the future, I'd be much happier. For now that's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I also hope we can solve this more elegantly. Thanks for reviewing!

@nnmm
Copy link
Contributor Author

nnmm commented Jun 2, 2022

@esteve Could you also take another look at this?

@@ -46,7 +46,7 @@ impl Drop for rcl_wait_set_t {
fn drop(&mut self) {
// SAFETY: No preconditions for this function (besides passing in a valid wait set).
let rc = unsafe { rcl_wait_set_fini(self) };
if let Err(e) = to_rcl_result(rc) {
if let Err(e) = to_rclrs_result(rc) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks much nicer and more idiomatic as it leverages TryInto:

if let Err(e) = TryInto::<RclReturnCode>::try_into(rc) {
    panic!("Unable to release WaitSet. {:?}", e)
}

And then we can get rid of to_rclrs_result entirely and incorporate it into TryFrom

@nnmm nnmm merged commit edc7721 into master Jun 4, 2022
@delete-merged-branch delete-merged-branch bot deleted the flatten_errors branch June 4, 2022 10:55
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.

2 participants