// trait Next { // type Tail: Next; // fn next(&mut self) -> (Option, Self::Tail); // } // impl Next for () { // type Tail = (); // fn next(&mut self) -> (Option, Self::Tail) { // (None, ()) // } // } // impl Next for (T,) { // type Tail = (); // fn next(&mut self) -> (Option, Self::Tail) { // (Some(self.0.clone()), ()) // } // } use std::{rc::Rc, mem::{MaybeUninit, transmute_copy}}; struct TupleIter { data: Rc<[MaybeUninit]>, cur: usize, } impl Iterator for TupleIter { type Item = T; fn next(&mut self) -> Option { let current_pos = self.cur; if self.cur >= self.data.len() { return None; } self.cur += 1; Some(unsafe { self.data.get_unchecked(current_pos).assume_init_read() }) } } trait TupleIntoIterator { type Output; fn into_iter(self) -> Self::Output; } trait TupleIterator { type Output; fn iter(&self) -> Self::Output; } impl TupleIntoIterator for (T,T,) { type Output = TupleIter; fn into_iter(self) -> Self::Output { TupleIter { data: Rc::<[MaybeUninit; 2]>::new(unsafe { transmute_copy(&self) }), cur: 0, } } } // impl TupleIterator for (T,T,) { // type Output = TupleIter; // fn iter(&self) -> Self::Output { // TupleIter { // data: Rc::new([self.0, self.1]), // cur: 0, // } // } // } #[test] fn tuple_iter() { let mut tups = (1 as usize, 2 as usize,); // tups.0 = 3 as usize; for i in tups.into_iter() { println!("{:?}", i); } // tups.0 = 4 as usize; println!("{tups:?}"); }