I think thats all for context_many1. Just need to start using it.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user