I think thats all for context_many1. Just need to start using it.

This commit is contained in:
Tom Alexander
2022-12-16 00:47:33 -05:00
parent 1f1a18782e
commit 6459cc64d0
3 changed files with 53 additions and 14 deletions

View File

@@ -27,7 +27,7 @@ impl<T> Node<T> {
}
// TODO: This Debug is only needed because of the try_unwrap+expect
impl<T: Debug> List<T> {
impl<T> List<T> {
pub fn new() -> Self {
List { head: None }
}
@@ -45,8 +45,10 @@ impl<T: Debug> List<T> {
match self.head.take() {
None => (None, List::new()),
Some(popped_node) => {
let extracted_node =
Rc::try_unwrap(popped_node).expect("TODO I should handle this better");
let extracted_node = match Rc::try_unwrap(popped_node) {
Ok(node) => node,
Err(e) => panic!("try_unwrap failed on Rc in pop_front on List."),
};
(
Some(extracted_node.data),
List {
@@ -91,6 +93,13 @@ impl<T: Debug> List<T> {
stop: &other.head,
}
}
pub fn into_iter_until<'a>(self, other: &'a List<T>) -> impl Iterator<Item = T> + 'a {
NodeIntoIterUntil {
position: self,
stop: &other,
}
}
}
pub struct NodeIter<'a, T> {
@@ -144,3 +153,21 @@ impl<'a, T> Iterator for NodeIterUntil<'a, T> {
Some(return_value)
}
}
pub struct NodeIntoIterUntil<'a, T> {
position: List<T>,
stop: &'a List<T>,
}
impl<'a, T> Iterator for NodeIntoIterUntil<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.position.ptr_eq(self.stop) {
return None;
}
let (popped_element, new_position) = self.position.pop_front();
self.position = new_position;
popped_element
}
}