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. Please suggest a solution for this problem. Thanks in advance!
This is a common problem that can arise from incorrect lifetime annotations. The issue is that you're binding the returned iterator's lifetime to the lifetime of self
which is 'a
. But you don't care about the lifetime of self
, you only care about the lifetime of the reference you're borrowing to it, &mut self
, let's call that lifetime 's
.
Now we just need to tell the compiler:
impl<'a> Encrypt<'a> {
pub fn encrypt<'s, T, U>(&'s mut self, data: T) -> impl Iterator<Item = u8> + 's
// ^^ ^^ ^^^^
// tie the return value's lifetime to the lifetime for which we burrow `self`
// instead of the whole lifetime of `self`.
where
T: IntoIterator<Item = U> + 's,
// ^^^^
// That'll transitively require this bound
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.