0
error[E0621]: explicit lifetime required in the type of `is`
  --> src/bee.rs:76:45
   |
75 | fn compute_instruction_set<'a>(is: &'a mut InstructionSet) {
   |                                    ---------------------- help: add explicit lifetime `'a` to the type of `is`: `&'a mut [OpCode<'a>; 255]`
76 |     let mut isb = InstructionSetBuilder{is: is, pos: 0};
   |                                             ^^ lifetime `'a` required

This is the code:

type InstructionSet<'a> = [OpCode<'a>; 0xff];
...
struct InstructionSetBuilder<'a> {
    is: &'a mut [OpCode<'a>],
    pos: usize,
}
...
fn compute_instruction_set<'a>(is: &'a mut InstructionSet) {
    let mut isb = InstructionSetBuilder{is: is, pos: 0};
...
}

Why do I need to set yet another 'a lifetime param?

Where?

I tried several combinations, all broke the syntax...

1
  • 2
    By the way, &'a mut T<'a> is almost never what you want (the real way to go would be &'a mut InstructionSet<'b>, with restriction on 'b: 'a), since by using &'a mut T<'a> we render the object locked behind the exclusuve reference for the whole its existence.
    – Cerberus
    Oct 20, 2022 at 2:24

1 Answer 1

0
type InstructionSet<'a> = [OpCode<'a>; 0xff];

This type alias is generic over a lifetime 'a. So you must specify it. Try this instead.

fn compute_instruction_set<'a>(is: &'a mut InstructionSet<'a>) {
    let mut isb = InstructionSetBuilder{is: is, pos: 0};
...
}
2
  • 2
    Alternatively, remove the lifetimes from the function signature altogether, since this is a case where it's possible for the compiler to infer them. Oct 20, 2022 at 0:20
  • Thank you all! In the end the trick was to use different named lifetimes for the references of things or the containers of references. Then those lifetimes can be omitted in places as the compiler can infer them. Oct 20, 2022 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.