-
Notifications
You must be signed in to change notification settings - Fork 3
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
Hardcode error type in IntoVisitor #41
Conversation
6e88d16
to
050a0ae
Compare
let res = decode_with_visitor(input, type_id.0, types, self.0).map_err(Into::into); | ||
DecodeAsTypeResult::Decoded(res) | ||
} | ||
} |
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.
Nice, this will allow for all sorts of easy conversions.
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.
Yeah; it basically replaces the "auto conversion" we did before but has the nice effect of simplifying the code anyway and making DecodeAsType
work better, so I'm quite happy with this solution in the end :)
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.
LGTM
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.
Nice one!
This PR hardcodes the error type, because, in a nutshell, we run into this issue in some places otherwise:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51e845f1f73ed72b0eb9d3d29172089a
We can avoid that specific issue by not having any bounds on
DecodeAsType
and instaed only bounding implementations ofDecodeAsType
, but that just means thatDecodeAsType
doesn't implyIntoVisitor
, and so code like the following fails:This fails because for
Option<T>
to implementDecodeAsType
, it must implementIntoVisitor
, and it only does so ifT: IntoVisitor, T::Visitor::Error: Into<Error>
, ie we can't just useDecodeAsType
as a trait bound.With a hardcoded
Error
type onIntoVisitor
,DecodeAsType
can implyIntoVisitor
, and the above issue goes away. I also added aVisitorWithCrateError
adapter struct which makes any visitor whose error is convertible intocrate::Error
into a visitor returningcrate::Error
, so that it's easy to still adapt any viable visitor into something that can be used withIntoVisitor
.