Correctly constrain ValueRef Deserialize impl to allow using #[derive(Deserialize)] on types that contain it - #386
Open
nabijaczleweli wants to merge 1 commit into
Open
Conversation
…(Deserialize)] on types that contain it
The following type
#[derive(Deserialize, Debug, Clone)]
pub struct PartialMsgPackLine<'raw>(pub UuidStr, #[serde(borrow = "'raw")] pub MsgPackExt<'raw>, #[serde(borrow = "'raw")] pub rmpv::ValueRef<'raw>);
fails to typecheck with
error: lifetime may not live long enough
--> src/collector/retina.rs:367:98
|
366 | #[derive(Deserialize, Debug, Clone)]
| ----------- lifetime `'de` defined here
367 | pub struct PartialMsgPackLine<'raw>(pub UuidStr, #[serde(borrow = "'raw")] pub MsgPackExt<'raw>, #[serde(borrow = "'raw")] pub rmpv::ValueRef<'raw>);
| ---- lifetime `'raw` defined here ^ requires that `'raw` must outlive `'de`
|
= help: consider adding the following bound: `'raw: 'de`
This is because the Deserialize impl is overconstrained:
it only allows deserialising into an identical lifetime instead of
identical-or-stronger. For example, it only allows deserialising
&'static [u8] into ValueRef<'static>, but not into ValueRef<'_>.
Relax this appropriately. The code above typechecks and works with this.
Sponsored-by: https://beaverlabs.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This type
fails to typecheck with
This is because the Deserialize impl is overconstrained: it only allows deserialising into an identical lifetime instead of identical-or-stronger. For example, it only allows deserialising &'static [u8] into ValueRef<'static>, but not into ValueRef<'_>. Relax this appropriately. The code above typechecks and works with this.