Skip to content

Correctly constrain ValueRef Deserialize impl to allow using #[derive(Deserialize)] on types that contain it - #386

Open
nabijaczleweli wants to merge 1 commit into
3Hren:masterfrom
nabijaczleweli:deserialize
Open

Correctly constrain ValueRef Deserialize impl to allow using #[derive(Deserialize)] on types that contain it#386
nabijaczleweli wants to merge 1 commit into
3Hren:masterfrom
nabijaczleweli:deserialize

Conversation

@nabijaczleweli

Copy link
Copy Markdown

This 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.

…(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
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.

1 participant