-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
ref(core): Refactor core:fs error mapping to use unified format #17719
Conversation
Related: #17526 (comment) |
runtime/ops/fs.rs
Outdated
})?; | ||
let std_file = open_options | ||
.open(&path) | ||
.map_err(create_err_mapper(format!("open '{}'", path.display())))?; |
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 will always allocate a string for error even if error is not returned. Could you instead change these to use closure instead so they are lazily evaluated?
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.
On the one hand, this looks ugly as hell:
.map_err(|err| create_err_mapper(format!("open '{}'", path.display()))(err))?;
But this one isn't any better as well :(
fn create_err_mapper(desc_fn: impl Fn() -> String) -> impl Fn(Error) -> Error {
move |err: Error| Error::new(err.kind(), format!("{err}, {}", desc_fn()))
}
// ...
.map_err(create_err_mapper(|| format!("open '{}'", path.display())))?;
Any preferences?
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.
The first one would be fairly pretty if it were not for the create_err_mapper
usage which here seems a bit redundant as it's being created inline in a lambda anyway.
Would it be possible to write a secondary err_mapper
function for use in these "inline" cases when there is no real benefit in creating an error mapper once and using it in many places?
.map_err(|err| default_err_mapper(format!("open '{}'", path.display()), err))
Done, this should do the work. |
Nice work, thank you very much! |
Follow-up to #17710
I double-checked the format and name, and there should be no errors during refactoring. Trimmed down the error mapping code by 1/3rd.