1

This issue is best described with this playground.

struct Foo;

impl From<&[u8]> for Foo {
    fn from(_: &[u8]) -> Foo {
        Foo
    }
}

What's in main() is proven to work, but I don't know how I can express that within the generic function without triggering compiler error E0637.

fn main() {
    let v = vec![0u8]; // <-- works!
    Foo::from(v.as_slice());
}

fn trigger_lifetime_issue_but_not_e637<'a, T>() 
where
    T: From<&'a[u8]> {
    let v = vec![0u8];
    T::from(v.as_slice()); // <-- Doesn't work because 'a is enforced by E0637
}

When trying to learn more about E0637 in search for a solution/alternative, I have to read between the lines.

a screenshot of the rust compiler error help page

This issue occours in a real work program, and I work around this with unnecessary owned values.

How can I use T: From<&[u8]> in a trait bound without triggering lifetime issues while avoiding E0637?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Byron
  • 3,908
  • 3
  • 26
  • 35
  • The provided Playground triggers E0597 with the latest Stable toolchain, not E0637. Would you like to double-check on this? Or are you saying that some other version of your code can trigger the latter? – E_net4 Feb 29 '20 at 13:01
  • @SCappella Yes :)! This answers the question. Here is a playground link which applies the locally scoped lifetime: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4ff94277e6d146b87e8640e4bee8644d – Byron Mar 01 '20 at 00:28

0 Answers0