Lifetimes do not match type in trait

pub struct BertVocab {
    pub values: BTreeMap<String, i64>,
    pub indices: BTreeMap<i64, String>,
    pub special_value_indices: BTreeMap<String, i64>,
    pub special_indices: BTreeMap<i64, String>,
}
pub struct BaseTokenizer<T: Vocab> {
    vocab: T,
    lower_case: bool,
    strip_accents: bool,
}
pub struct BertTokenizer {
    vocab: BertVocab,
    base_tokenizer: BaseTokenizer<BertVocab>,
}
impl Transform<(Tensor<u8>, Tensor<u8>)> for Tokenizers {
    type Output<'a> = (Tensor<i32>, Tensor<i32>, Tensor<i32>, Tensor<&'a str>);

    fn transform (&mut self, s: (Tensor<u8>, Tensor<u8>)) -> (Tensor<i32>, Tensor<i32>, Tensor<i32>, Tensor<&str>) { 
        todo!
    } 

I'm getting this error:

error[E0658]: generic associated types are unstable
  --> src/lib.rs:45:5
   |
45 |     type Output<'a> = (Tensor<i32>, Tensor<i32>, Tensor<i32>, Tensor<&'a str>);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information

error[E0195]: lifetime parameters or bounds on type `Output` do not match the trait declaration
  --> src/lib.rs:45:16
   |
45 |     type Output<'a> = (Tensor<i32>, Tensor<i32>, Tensor<i32>, Tensor<&'a str>);
   |                ^^^^ lifetimes do not match type in trait

Some errors have detailed explanations: E0195, E0658.
For more information about an error, try `rustc --explain E0195`.

Trait Definition:

I'm new to Rust and trying to understand lifetimes. Could someone please help me understand why I'm getting this error? How can I fix this? I'm not sure if this much data is sufficient to understand the error, please let me know if anything else is needed from my side.

type Output and type Output<'a> are different things (and the latter syntax is not even stable yet).

You can't just add lifetimes where you want them, because their existence changes what kind of code is allowed. The relationship between inputs and outputs is fixed and restricted upfront by the trait's requirements. Use String instead of &str if necessary. Or if the strings are compile-time literals, then &'static str may work.

3 Likes

The error messages are hiding the real problem. If you try writing a free-standing function

fn transform_implementation (this: &mut Tokenizers, s: (Tensor<u8>, Tensor<u8>)) -> (Tensor<i32>, Tensor<i32>, Tensor<i32>, Tensor<&str>) { 
    todo!()
} 

you probably won’t succeed either, unless the &str is static or borrows from self/this (the latter would be somewhat questionable, too, because it’s often an anti-pattern and can lead to problems if you create a shared reference from a mutable borrow). Try if you can work with an owned return type, e.g. Tensor<String> instead of Tensor<&str>, or if the &strs are always literals, maybe Tensor<&'static str>.

I’m not familiar with the crates involved, so I can’t tell you which one of Tensor<String> and Tensor<&str> would be more typical/promising.

2 Likes

Thanks, @kornel, @steffahn, for your reply.
The trait doesn't allow the use of Tensor<String> but using Tensor<&'static str> solved my issue.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.