-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve docs on how to implement LendingIterator
#9
Comments
Curiously there is an example in the documentation of the Nougat crate #[macro_use]
extern crate nougat;
#[gat]
trait LendingIterator {
type Item<'next>
where
Self : 'next,
;
fn next(&mut self) -> Option<Self::Item<'_>>;
}
struct WindowsMut<Slice, const SIZE: usize> {
slice: Slice,
start: usize,
}
#[gat]
impl<Item, const SIZE: usize> LendingIterator for WindowsMut<&mut [Item], SIZE> {
type Item<'next>
where
Self : 'next,
=
&'next mut [Item; SIZE]
;
fn next(&mut self) -> Option<&mut [Item; SIZE]> {
let to_yield =
self.slice
.get_mut(self.start ..)?
.get_mut(.. SIZE)?
.try_into()
.expect("slice has the right SIZE")
;
self.start += 1;
Some(to_yield)
}
} |
I attempted to find documentation on how to use ::lending_iterator::prelude::*;
struct Slicer<'a> {
full: Vec<u32>,
cur_slice: &'a mut [u32]
}
impl<'a> Slicer<'a> {
fn new() -> Slicer<'a> {
let mut full = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Slicer {
full: full,
cur_slice: full.as_mut_slice()
}
}
}
#[gat]
impl<'a> LendingIterator for Slicer<'a>
{
type Item<'next>
where
Self: 'next,
=
&'next [u32]
;
fn next(&mut self) -> Option<&[u32]> {
if self.cur_slice.len() != 0 {
// In the actual program, this is streaming new data into the
// buffer. This is just a minimal example.
self.cur_slice = self.cur_slice[..self.cur_slice.len() - 1].as_mut();
Some(self.cur_slice)
} else {
None
}
}
}
fn main() {
let mut slicer = Slicer::new();
while let Some(x) = slicer.next() {
println!("{x:?}");
}
} I'm still getting a few separate errors, the two mains one being
Am I barking up the wrong tree with lending iterators, or is there a way to accomplish this? |
The README describes how to implement anonymous implementations of
LendingIterator
. Would it be possible to expand a bit and demo how to implement a non-anonymous version?I.e. given
how would
look like if say we want to return chunks of size 8 of the
Vec<u8>
Thanks for the cool crate!
The text was updated successfully, but these errors were encountered: