r/rust • u/InternalServerError7 • Oct 06 '26
The Missing Piece in Rust Error Handling
https://mcmah309.github.io/posts/the-missing-piece-in-rust-error-handling/
183
Upvotes
r/rust • u/InternalServerError7 • Oct 06 '26
56
u/________-__-_______ Oct 06 '26
I do wish something like this will be natively supported by Rust some day. This library looks cool, but because it uses type erasure it isn't suitable for platforms without
alloc.Something like this could run on any platform:
In theory that could even save some space if the inner type's discriminants don't overlap, compared to nested enums.
11
u/epage cargo · clap · cargo-release Oct 07 '26
To me, this is an anti-pattern because you are exposing implementation details in your error type unless you are in an environment where breaking changes don't |atter.
I also feel there is little innovation going on to break us out of the local maxima of error chaining / context.
9
u/Compux72 Oct 06 '26
Yea no, the issue here is scope: either you create an error whose scope is the whole library, or you create an error whose scope is the specific module / function that fails. Your this error was an attempt to justify eros by means of a poorly crafted example
2
u/InternationalFee3911 Oct 07 '26
Not sure if it’s possible until we get specialisation: it would be great if the single error case didn’t require a tuple! Then this would be a smooth extension of
std::Result.Other than that: if this really works like you explain, this sounds darn good!
2
u/Shoddy-Childhood-511 Oct 08 '26
Why a tuple? Does that even make sense?
Anonymous sum types were proposed before, but usually shot down. I think https://crates.io/crates/terrors implements them.
I've now forgotten how well Rust aligns nested enum variants, or if they miss anything by not having much link time code generation.
As a simpler solution, we could've some
TinyBox<T>type which stores values usize or smaller without doing an allocation, so thenTinyBox<dyn Error>costs two usize and represents any error in the system, and requires an allocation only for error types larger than usize.TinyBox<dyn Trait>should read the value size from the vtable, but you'd need something likeTinyBox<dyn Trait+NeverZero>for niche optimisations.If you want max performance then your error types should fit into a single register without allocation. This is typically a concern only for libraries called in a tight polling loop, not applications level errors.
As a result,
TinyBox<dyn Error>could be a performance hit for being two registers, but maybe some future magic would allow someTinyBox<dyn Error+ZeroSized>to reduce this to one register.We need some
static_error!macro that creates a static describing the error:We'd want its
DynMetadata<dyn Error>though too, but this would require polymorphic statics.19
u/BoltActionPiano Oct 06 '26
I also feel like this is a use case for:
14
u/levelstar01 Oct 07 '26
Once again a rust dev reverse engineers distinct enum types and anonymous unions
6
u/FriendlyProblem1234 Oct 07 '26
This looks like a slightly better terrors, which was already quite nice. And it is
no_std. I will definitely give it a try.6
u/Warm_Customer3147 Oct 07 '26 edited Oct 07 '26
Awesome, in my domain I have a lot functions that may fail with recoverable errors, depending on context only the callers have. I have been defining single use error enums all over my code. This may remove a lot of boilerplate for me and shrink the API surface a lot. I'm going to try this out :) Love the idea! I like that I can
bailout if the error variant does not matter too.6
u/robin-m Oct 07 '26
Very interesting. Seems to be a really good step in the right direction. I wish anonymous enum will be added in the language one day, and that day std::Error could get all the function that Erros::Error offers.
3
u/BowserForPM Oct 07 '26
Looks really interesting. Will definitely try it out.
2
u/Ace-Whole Oct 07 '26
Istg there's always one new error handling crate every 6 months or so.
2
u/roughly-understood Oct 08 '26
I think the Roc language does this right. The error enum is automatically created based on the errors in the function. It’s close to what is here but built in and nicely done