-
Notifications
You must be signed in to change notification settings - Fork 76
Add TryFrom<&[u8]> bound to Encoding::Repr
#261
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
Conversation
| + Copy | ||
| + Clone | ||
| + Sized | ||
| + for<'a> TryFrom<&'a [u8], Error = core::array::TryFromSliceError>; |
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 don't think the error type is particularly relevant here? Do we really need to mandate it?
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.
It helps a lot with writing down trait bounds in the user code. See rust-lang/rust#113517 for example.
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.
To add to that, even with the workaround in the issue above, it does not work when another level is added. That is, if the foo() in the issue is a serde-standard deserialize() function, and I want to make a custom deserializer using serde_with as
impl<'de, const N: usize> serde_with::DeserializeAs<'de, Uint<N>> for UintWrapper<N>
where
Uint<N>: Encoding,there is no way to write trait bounds that would tell the compiler that the returned error is Display (so that it could be wrapped in the serde error).
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.
Okay, if there's a legitimate use case I guess we can include it.
It's just a completely useless error that includes no relevant information, so it's easily handled without bounding on it by using .ok() or .map_err(...).
But if it helps with serde, I guess that's fine.
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 one is pretty useless, I agree. But if you have a function that is generic on something that's TryFrom<&[u8]> and you want the error to be Display (because it may be useful for some types, so you don't want to discard it), fixing the error in this specific case helps with the bounds. I don't particularly like it either, but unfortunately Rust compiler is currently not smart enough to handle bounds without it. I think it should be pretty harmless, since it's essentially an internal trait that's not supposed to be used outside of the crate.
|
Also note: this is a breaking change, so we can't merge it until the next breaking release. |
|
Bumped version to v0.6.0-pre in #295. Merging |
This helps with deserialization - one doesn't need to create a mutable buffer and use a special purpose
deserializemutating its argument (likeserdectdoes currently), but instead one can just use a standarddeserializewith the API compatible with that of#[serde(with)]that returns theReprobject.