-
Notifications
You must be signed in to change notification settings - Fork 159
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
Conversation
@@ -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) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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),
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
@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) { |
There was a problem hiding this comment.
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
As discussed in chat:
NodeError
variants).I removed the tests because they didn't seem terribly useful, but we can discuss this.