use std::iter::Iterator;
trait ListTerm<'a> {
type Iter: Iterator<Item = &'a u32>;
fn iter(&'a self) -> Self::Iter;
}
enum TermValue<'a, LT>
where
LT: ListTerm<'a> + Sized + 'a,
{
Str(LT),
}
error[E0392]: parameter `'a` is never used
--> src/main.rs:8:16
|
8 | enum TermValue<'a, LT>
| ^^ unused type parameter
|
= help: consider removing `'a` or using a marker such as `std::marker::PhantomData`
'a
clearly is being used. Is this a bug, or are parametric enums just not really finished? rustc --explain E0392
recommends the use of PhantomData<&'a _>
, but I don't think there's any opportunity to do that in my use case.
If you aren't using an associated type (like
<LT as ListTerm<'a>>::Iter
) in the enum definition, you probably don't need to make'a
a parameter at all.I assume you want the
LT: ListTerm<'a>
bound so that you can write one or morefn
s or animpl
that usesLT
as aListTerm
. In which case, you can easily parameterize the type with just<LT>
, and put the'a
generic and trait bound only on the items that require it:Some standard library types like
std::collections::HashMap<K, V>
do this: theK: Hash + Eq
bound isn't on the type itself. Alternatively, you could have awhere
clause on each method where the bound is needed. The difference between awhere
clause on animpl
and one on afn
is not significant unless you're implementing a trait (see this question).The main reason for using
PhantomData
is that you want to express some constraint that the compiler can't figure out by itself. You don't needPhantomData
to express "AnyTermData<LT>
is only valid as long as its containedLT
is valid", because the compiler already enforces that (by "peering inside" the type, as in Matthieu's answer).Not as far as the compiler is concerned. All it cares about is that all of your generic parameters are used somewhere in the body of the
struct
orenum
. Constraints do not count.What you might want is to use a higher-ranked lifetime bound:
In other situations, you might want to use
PhantomData
to indicate that you want a type to act as though it uses the parameter:And just to be clear: you can use
PhantomData
in anenum
; put it in one of the variants:@DK. answered how to circumvent the issue (just use
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 system to identifies 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:&T
&mut T
, and it excludes concurrent&T
When you parameterize your
struct
orenum
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.