-
Notifications
You must be signed in to change notification settings - Fork 5
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
bootspec(generation): offer TryInto conversion from Generation to any… #109
bootspec(generation): offer TryInto conversion from Generation to any… #109
Conversation
Looks good (just needs a |
It was more like a discussion on whether this PR is a good idea and I made it in 2 minutes so I was not sure of the quality :-) |
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'm generally in favor of making this library easier to use for consumers :)
@@ -33,6 +33,17 @@ impl Generation { | |||
} | |||
} | |||
|
|||
impl TryInto<v1::GenerationV1> for Generation { |
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.
We shouldn't manually implement {Try,}Into
ourselves since we get that for free from {Try,}From
: https://doc.rust-lang.org/std/convert/trait.TryInto.html
Library authors should usually not directly implement this trait, but should prefer implementing the TryFrom trait, which offers greater flexibility and provides an equivalent TryInto implementation for free, thanks to a blanket implementation in the standard library. For more information on this, see the documentation for Into.
fn try_into(self) -> Result<v1::GenerationV1, Self::Error> { | ||
match self { | ||
Self::V1(gen) => Ok(gen), | ||
_ => return Err(format!("Invalid schema version, expected version 1, got: {}", self.version()).to_string()) |
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 like the idea, but I don't like the fact we'd need a separate impl block for each generation. I think switching to TryFrom
will make this point moot, though.
I was wrong about TryFrom making us able to get rid of the "manually add a new impl block for each variant", but there is a |
Description
… schema version
Enables replacing:
by
Checklist
cargo build
cargo fmt
cargo clippy
cargo test