Hello
While poling a future, I want to progressively shorten a slice until is is empty:
slice = &slice[n..]
When it's a mutable slice the compiler complains about lifetime:
slice = &mut slice[n..]
Complete example and playground:
use std::{mem::transmute, pin::Pin};
#[derive(Debug)]
struct Foo<'a> {
buf: &'a [u8],
}
impl<'a> Foo<'a> {
fn skip(mut self: Pin<&mut Self>, n: usize) {
// This is fine:
self.buf = &self.buf[n..];
}
}
#[derive(Debug)]
struct FooMut<'a> {
buf: &'a mut [u8],
}
impl<'a> FooMut<'a> {
fn skip(mut self: Pin<&mut Self>, n: usize) {
// This ain't:
// self.buf = &mut self.buf[n..];
// I have to cheat:
self.buf = unsafe { transmute(&mut self.buf[n..]) };
}
}
fn main() {
let mut data = [4, 8, 15, 16, 23, 42];
let mut foo = Foo { buf: &data };
let mut foo = Pin::new(&mut foo);
foo.as_mut().skip(3);
println!("{:?}", foo);
let mut foo = FooMut { buf: &mut data };
let mut foo = Pin::new(&mut foo);
foo.as_mut().skip(3);
println!("{:?}", foo);
}