Starting to separate the context and parsed tokens into their own modules.
This commit is contained in:
66
src/context/list.rs
Normal file
66
src/context/list.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct List<'parent, T> {
|
||||
data: T,
|
||||
parent: Link<'parent, T>,
|
||||
}
|
||||
|
||||
type Link<'parent, T> = Option<&'parent List<'parent, T>>;
|
||||
|
||||
impl<'parent, T> List<'parent, T> {
|
||||
pub fn new(first_item: T) -> Self {
|
||||
Self {
|
||||
data: first_item,
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_data(&self) -> &T {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn get_parent(&'parent self) -> Link<'parent, T> {
|
||||
self.parent
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> Iter<'_, T> {
|
||||
Iter { next: Some(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ListType<'parent, T> {
|
||||
fn push(&'parent self, item: T) -> List<'parent, T>;
|
||||
}
|
||||
|
||||
impl<'parent, T> ListType<'parent, T> for List<'parent, T> {
|
||||
fn push(&'parent self, item: T) -> Self {
|
||||
Self {
|
||||
data: item,
|
||||
parent: Some(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'parent, T> ListType<'parent, T> for Link<'parent, T> {
|
||||
fn push(&'parent self, item: T) -> List<'parent, T> {
|
||||
List {
|
||||
data: item,
|
||||
parent: *self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'a, T> {
|
||||
next: Link<'a, T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for Iter<'a, T> {
|
||||
type Item = &'a T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let ret = self.next.map(|link| link.get_data());
|
||||
self.next = self.next.map(|link| link.get_parent()).flatten();
|
||||
ret
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user