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

Deriving Arbitrary on a field containing a Cow<'static, str> fails. #86

Open
teymour-aldridge opened this issue Jun 5, 2021 · 1 comment

Comments

@teymour-aldridge
Copy link

For example, the following code (using version 1.0.1) fails to compile.

use std::borrow::Cow;

#[derive(arbitrary::Arbitrary)]
pub struct WithACow {
    cow: Cow<'static, str>,
}

fn main() {
    println!("Hello, world!");
}

With the error message

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime `'arbitrary` as defined on the impl at 3:10...
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
note: ...so that the expression is assignable
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  = note: expected `&mut Unstructured<'_>`
             found `&mut Unstructured<'arbitrary>`
  = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
 --> src/main.rs:3:10
  |
3 | #[derive(arbitrary::Arbitrary)]
  |          ^^^^^^^^^^^^^^^^^^^^
  = note: expected `Cow<'static, _>`
             found `Cow<'_, _>`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

But something like this does compile:

use std::borrow::Cow;

use arbitrary::Arbitrary;

pub struct WithACow {
    cow: Cow<'static, str>,
}

impl<'a> Arbitrary<'a> for WithACow {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(WithACow {
            cow: Arbitrary::arbitrary(u).map(Cow::Owned)?,
        })
    }
}

fn main() {
    println!("Hello, world!");
}
This was referenced Aug 25, 2021
@greyblake
Copy link
Contributor

@teymour-aldridge

        Ok(WithACow {
            cow: Arbitrary::arbitrary(u).map(Cow::Owned)?,
        })

works, because you're always constructing an owned data.

To generate the Borrowed variant, Arbitrary would need data with 'static lifetime, but there is no place where it can get from. So the error makes sense, it's the case where one would need to construct Cow manually.
Once this #129 gets merged, you'll be able to implement custom arbitrary for single fields, which may help to workaround this problem.

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 a pull request may close this issue.

2 participants