Skip to content

Commit

Permalink
Merge pull request #4 from MrElectrify/list
Browse files Browse the repository at this point in the history
eastl::list
  • Loading branch information
MrElectrify authored Sep 16, 2023
2 parents 03090e2 + 9a7bdec commit a425df0
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A binary-compatible EASTL implementation written in Rust
- `Queue`
- `VectorMap`
- `FixedVector`
- `List`

* there might be some things missing :)

Expand All @@ -22,7 +23,6 @@ A binary-compatible EASTL implementation written in Rust
## Planned containers
- `HashMultimap`
- `HashMultiset`
- `List`
- `Map`
- `Multimap`
- `Set`
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub mod set;
pub mod string;
pub mod vector;
pub mod vector_map;
pub mod list;
78 changes: 78 additions & 0 deletions src/list/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::list::node::{ListNode, ListNodeBase};
use std::marker::PhantomData;

/// Iterator over `eastl::List`, yielding references in the list's order
pub struct Iter<'a, T: 'a> {
sentinel_node: *const ListNodeBase,
current_node: *mut ListNodeBase,
len: usize,
marker: PhantomData<&'a ListNode<T>>,
}

impl<'a, T> Iter<'a, T> {
pub(crate) fn new(sentinel_node: *const ListNodeBase, len: usize) -> Self {
Self {
sentinel_node,
current_node: sentinel_node.cast_mut(),
len,
marker: PhantomData,
}
}
}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

fn next(&mut self) -> Option<Self::Item> {
if unsafe { (*self.current_node).next.cast_const() } == self.sentinel_node {
None
} else {
self.len -= 1;
self.current_node = unsafe { (*self.current_node).next };
let node = self.current_node as *const ListNode<T>;
Some(unsafe { (*node).value() })
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}

/// Iterator over `eastl::List`, yielding mutable references in the list's order
pub struct IterMut<'a, T: 'a> {
sentinel_node: *const ListNodeBase,
current_node: *mut ListNodeBase,
len: usize,
marker: PhantomData<&'a mut ListNode<T>>,
}

impl<'a, T> IterMut<'a, T> {
pub(crate) fn new(sentinel_node: *const ListNodeBase, len: usize) -> Self {
Self {
sentinel_node,
current_node: sentinel_node.cast_mut(),
len,
marker: PhantomData,
}
}
}

impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;

fn next(&mut self) -> Option<Self::Item> {
if unsafe { (*self.current_node).next.cast_const() } == self.sentinel_node {
None
} else {
self.len -= 1;
self.current_node = unsafe { (*self.current_node).next };
let node = self.current_node as *mut ListNode<T>;
Some(unsafe { (*node).value_mut() })
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
Loading

0 comments on commit a425df0

Please sign in to comment.