Closed wezm closed 4 years ago
Taking a quick look, it's probably the ReadCtxt
param in ReadBinaryDep
's read_dep
method that's causing it. The lifetime 'a
appears in a type behind a mutable reference, which according to rust's variance rules, forces the lifetime 'a
to be invariant, which will break rental's covariant feature. In the working example, the lifetime 'a
appears only behind a shared reference, allowing it to remain covariant.
Thanks for taking a look and quick reply. Your insights are helpful. My colleagues and I were a little surprised that a method on a trait might be able influence the variance of the struct, even when the mutable reference supplied to the method is not self
. Nonetheless is it your understanding that this can be the case?
The variance of a struct or trait is determined solely by any lifetime parameters it takes. If 'a
is used in an invariant context, it must be invariant, making the trait invariant over that lifetime, and any structs that implement that trait invariant over that lifetime, and so on. self
has no special meaning or bearing with respect to lifetime variance.
Great, thanks for the explanation, it helps.
I've recently been working on adding rental to allsorts in order to be able to hold font table data alongside its parsed representation in the same struct. I have it working with
#[rental]
but with#[rental(covariant)]
a lifetime error is raised for one of the structs. It's possible that the compiler is identifying an actual issue but my colleagues are a bit confused as to why it's being rejected.I've extracted the code and reduced it to a minimal example as much as possible here: https://github.com/wezm/rental-covariant-lifetime-param
It appears to be something about
ReadArray
that is an issue, as the other examplestruct
works as expected. Any insight would be most appreciated.The error is: