Skip to content
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

Merged
merged 9 commits into from
Nov 10, 2023
Merged

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Nov 9, 2023

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 of DecodeAsType, but that just means that DecodeAsType doesn't imply IntoVisitor, and so code like the following fails:

#[derive(DecodeAsType)]
#[decode_as_type(trait_bounds = "T: DecodeAsType")]
struct Foo<T> {
    val: Option<T>
}

This fails because for Option<T> to implement DecodeAsType, it must implement IntoVisitor, and it only does so if T: IntoVisitor, T::Visitor::Error: Into<Error>, ie we can't just use DecodeAsType as a trait bound.

With a hardcoded Error type on IntoVisitor, DecodeAsType can imply IntoVisitor, and the above issue goes away. I also added a VisitorWithCrateError adapter struct which makes any visitor whose error is convertible into crate::Error into a visitor returning crate::Error, so that it's easy to still adapt any viable visitor into something that can be used with IntoVisitor.

@jsdw jsdw changed the title Hardcode error type in IntoVisitor, and prep a release Hardcode error type in IntoVisitor, and prep 0.10.0 release Nov 9, 2023
@jsdw jsdw force-pushed the jsdw-simpler-decode-as-type branch from 6e88d16 to 050a0ae Compare November 9, 2023 17:39
@jsdw jsdw changed the title Hardcode error type in IntoVisitor, and prep 0.10.0 release Hardcode error type in IntoVisitor Nov 9, 2023
let res = decode_with_visitor(input, type_id.0, types, self.0).map_err(Into::into);
DecodeAsTypeResult::Decoded(res)
}
}
Copy link
Contributor

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.

Copy link
Collaborator Author

@jsdw jsdw Nov 10, 2023

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 :)

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@lexnv lexnv left a comment

Choose a reason for hiding this comment

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

Nice one!

@jsdw jsdw merged commit 2c59157 into main Nov 10, 2023
8 checks passed
@jsdw jsdw deleted the jsdw-simpler-decode-as-type branch November 10, 2023 14:53
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.

4 participants