DK. answered how to circumvent the issue (by using PhantomData
as suggested), and hinted that the issue was that 'a
was unused in the definition, but why would the compiler care about that?
'a
is a lifetime marker. It is used by the borrow-checker to identify the relationship between the lifetime of different objects, as well as their borrow status.
When borrowing an object, you may borrow it either mutably (&mut T
) or immutably (&T
), and in accordance with the Mutability XOR Aliasing principle underpinning Rust's memory safety it changes everything:
- You can have multiple concurrent
&T
- You can only have a single
&mut T
, and it excludes concurrent &T
When you parameterize your struct
or enum
with 'a
, you announce your intention to borrow something whose lifetime will be some 'a
. You do not, however, announce whether you will be borrowing mutably or immutably, and this detail is critical.
The compiler, therefore, will peer at the internals of your data type and check whether you use a mutable or immutable reference to deduce, by itself, which kind of borrow will occur when you use the data type.
And here, because 'a
is unused, it cannot find any such use and therefore cannot compile your code.
It is arguable whether the compiler peering inside the data type is a good thing or not, since changing the internals of this data type (from &T
to &mut T
) could lead to compilation failures without changing the type interface.
It is important, thus, to remember that how you use the generic parameters (owning, borrowing mutably or borrowing immutably) is NOT an implementation detail.