indices iterator for layers
This commit is contained in:
@@ -138,10 +138,10 @@ impl<Ctx> SensorModule<Ctx> {
|
||||
|
||||
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
|
||||
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
let mut layers = std::mem::take(&mut ctx.ui().layers);
|
||||
let layers = std::mem::take(&mut ctx.ui().layers);
|
||||
let mut module = std::mem::take(ctx.ui().modules.get_mut::<Self>());
|
||||
|
||||
for (i, _) in layers.iter_mut().rev() {
|
||||
for i in layers.indices().rev() {
|
||||
let Some(list) = module.active.get_mut(&i) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -95,15 +95,11 @@ impl Layers {
|
||||
}
|
||||
|
||||
pub fn iter_mut(&mut self) -> LayerIteratorMut<'_> {
|
||||
let mut last = 0;
|
||||
while let Some(c) = self.vec[last].child {
|
||||
last = c.tail;
|
||||
}
|
||||
LayerIteratorMut {
|
||||
next: Some(0),
|
||||
next_back: Some(last),
|
||||
vec: &mut self.vec,
|
||||
}
|
||||
LayerIteratorMut::new(&mut self.vec)
|
||||
}
|
||||
|
||||
pub fn indices(&self) -> LayerIndexIterator<'_> {
|
||||
LayerIndexIterator::new(&self.vec)
|
||||
}
|
||||
|
||||
pub fn write<P: Primitive>(
|
||||
@@ -157,18 +153,51 @@ impl LayerNode {
|
||||
}
|
||||
|
||||
pub struct LayerIteratorMut<'a> {
|
||||
next: Option<usize>,
|
||||
next_back: Option<usize>,
|
||||
vec: &'a mut Vec<LayerNode>,
|
||||
inner: LayerIndexIterator<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for LayerIteratorMut<'a> {
|
||||
type Item = (usize, &'a mut Layer);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let i = self.inner.next()?;
|
||||
// SAFETY: requires index iterator to work properly
|
||||
#[allow(mutable_transmutes)]
|
||||
let layer: &mut Layer = unsafe { std::mem::transmute(&self.inner.vec[i]) };
|
||||
Some((i, layer))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DoubleEndedIterator for LayerIteratorMut<'a> {
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
let i = self.inner.next_back()?;
|
||||
// SAFETY: requires index iterator to work properly
|
||||
#[allow(mutable_transmutes)]
|
||||
let layer: &mut Layer = unsafe { std::mem::transmute(&self.inner.vec[i]) };
|
||||
Some((i, layer))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LayerIteratorMut<'a> {
|
||||
fn new(vec: &'a mut Vec<LayerNode>) -> Self {
|
||||
Self {
|
||||
inner: LayerIndexIterator::new(vec),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LayerIndexIterator<'a> {
|
||||
next: Option<usize>,
|
||||
next_back: Option<usize>,
|
||||
vec: &'a Vec<LayerNode>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for LayerIndexIterator<'a> {
|
||||
type Item = usize;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let ret_i = self.next?;
|
||||
// chat are we cooked? (if it's not set up correctly this could be catastrophic)
|
||||
let node: &mut LayerNode = unsafe { std::mem::transmute(&mut self.vec[ret_i]) };
|
||||
let node = &self.vec[ret_i];
|
||||
self.next = if let Some(c) = node.child {
|
||||
Some(c.head)
|
||||
} else if let Ptr::Next(i) = node.next {
|
||||
@@ -187,14 +216,14 @@ impl<'a> Iterator for LayerIteratorMut<'a> {
|
||||
self.next = None;
|
||||
self.next_back = None;
|
||||
}
|
||||
Some((ret_i, &mut node.data))
|
||||
Some(ret_i)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DoubleEndedIterator for LayerIteratorMut<'a> {
|
||||
impl<'a> DoubleEndedIterator for LayerIndexIterator<'a> {
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
let ret_i = self.next_back?;
|
||||
let node: &mut LayerNode = unsafe { std::mem::transmute(&mut self.vec[ret_i]) };
|
||||
let node = &self.vec[ret_i];
|
||||
self.next_back = if let Ptr::Next(mut i) = node.prev {
|
||||
while let Some(c) = self.vec[i].child {
|
||||
i = c.tail
|
||||
@@ -209,6 +238,20 @@ impl<'a> DoubleEndedIterator for LayerIteratorMut<'a> {
|
||||
self.next = None;
|
||||
self.next_back = None;
|
||||
}
|
||||
Some((ret_i, &mut node.data))
|
||||
Some(ret_i)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LayerIndexIterator<'a> {
|
||||
fn new(vec: &'a Vec<LayerNode>) -> Self {
|
||||
let mut last = 0;
|
||||
while let Some(c) = vec[last].child {
|
||||
last = c.tail;
|
||||
}
|
||||
Self {
|
||||
next: Some(0),
|
||||
next_back: Some(last),
|
||||
vec,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user