I am facing a weird lifetime compilation error while trying to compile following simple XOR encryption Rust code.
pub struct Encrypt<'a> {
key:&'a[u8],
current_index: usize,
}
impl<'a> Encrypt<'a> {
pub fn new<T: ?Sized + AsRef<[u8]>>(key: &'a T) -> Encrypt<'a> {
Encrypt { key: key.as_ref(), current_index: 0 }
}
pub fn encrypt<T, U>(&'a mut self, data: T) -> impl Iterator<Item = u8>
where T: IntoIterator<Item = U>, U: std::borrow::Borrow<u8> {
let iter = data.into_iter().map(|b| {
let val = b.borrow() ^ self.key[self.current_index];
self.current_index = (self.current_index + 1) % self.key.len();
val
});
iter
}
}
I am getting following bompilation error:
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
8 | impl<'a> Encrypt<'a> {
| -- hidden type `Map<<T as IntoIterator>::IntoIter, [closure@src/lib.rs:36:41: 40:10]>` captures the lifetime `'a` as defined here
...
41 | iter
This is really strange because the method encrypt() does not return any reference, still the compiler appears to be associating the lifetime of self with it.
.map()makes a lazy iterator, thus the closure that is used in it has to capture all the objects used inside. In particular you use&'a selfin it to accessself.keyandself.current_index, so the returning iterator is actually tied to this reference and its lifetime