Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub trait AssocRefIterPart {
- type Item;
- type Capture: ?Sized;
- fn next<'a>(&mut self, assoc: &'a Self::Capture) -> Option<&'a Self::Item>;
- }
- pub struct AssocRefIter<'a, T, P>
- where
- T: ?Sized,
- {
- inner: &'a T,
- part: P,
- }
- impl<'a, T, P> AssocRefIter<'a, T, P>
- where
- T: ?Sized,
- {
- pub fn new(inner: &'a T, part: P) -> Self {
- Self { inner, part }
- }
- }
- impl<'a, T, P> Iterator for AssocRefIter<'a, T, P>
- where
- T: ?Sized,
- P: 'a + AssocRefIterPart<Capture = T>,
- {
- type Item = &'a P::Item;
- fn next(&mut self) -> Option<Self::Item> {
- self.part.next(self.inner)
- }
- }
- pub trait ServerExample<ClientId> {
- type ClientsPart: AssocRefIterPart<Item = ClientId, Capture = Self>;
- fn clients<'a>(&'a self) -> AssocRefIter<'a, Self, Self::ClientsPart>;
- }
- pub struct ListOf64 {
- v: Vec<u64>,
- }
- pub struct LO64IterPart {
- pos: std::ops::Range<usize>,
- }
- impl AssocRefIterPart for LO64IterPart {
- type Item = u64;
- type Capture = ListOf64;
- fn next<'a>(&mut self, assoc: &'a Self::Capture) -> Option<&'a Self::Item> {
- unsafe { Some(assoc.v.get_unchecked(self.pos.next()?)) }
- }
- }
- impl ServerExample<u64> for ListOf64 {
- type ClientsPart = LO64IterPart;
- fn clients<'a>(&'a self) -> AssocRefIter<'a, Self, Self::ClientsPart> {
- AssocRefIter::new(
- self,
- LO64IterPart {
- pos: 0..self.v.len(),
- },
- )
- }
- }
- fn main() {
- let k = ListOf64 { v: vec![1, 2, 3, 4, 8, 7, 6, 5] };
- for x in k.clients() {
- println!("{}", x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement