pub struct Stack<T> { /* private fields */ }
Expand description
A Stack can be thought of as a linked list with a hole punched in it to mark a single element that currently holds focus (though in practice it is implemented using a VecDeque for efficiency purposes). By convention, the main element is the first element in the stack (regardless of focus). Focusing operations do not reorder the elements of the stack or the resulting Vec that can be obtained from calling Stack::flatten.
This is a zipper over a VecDeque. Many of the methods that mutate the structure of the Stack return back a mutable reference so that they are able to be chained.
Implementations§
source§impl Stack<Box<dyn Layout>>
impl Stack<Box<dyn Layout>>
sourcepub fn run_and_replace<F>(&mut self, f: F) -> Vec<(Xid, Rect)>
pub fn run_and_replace<F>(&mut self, f: F) -> Vec<(Xid, Rect)>
sourcepub fn handle_message<M>(&mut self, m: M)where
M: IntoMessage,
pub fn handle_message<M>(&mut self, m: M)where
M: IntoMessage,
sourcepub fn broadcast_message<M>(&mut self, m: M)where
M: IntoMessage,
pub fn broadcast_message<M>(&mut self, m: M)where
M: IntoMessage,
source§impl<T> Stack<T>
impl<T> Stack<T>
sourcepub fn new<I, J>(up: I, focus: T, down: J) -> Selfwhere
I: IntoIterator<Item = T>,
J: IntoIterator<Item = T>,
pub fn new<I, J>(up: I, focus: T, down: J) -> Selfwhere
I: IntoIterator<Item = T>,
J: IntoIterator<Item = T>,
Create a new Stack specifying the focused element and and elements above and below it.
sourcepub fn try_from_iter<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = T>,
pub fn try_from_iter<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = T>,
For an iterator of at least one element, the first element will be focused and all remaining elements will be placed after it. For an empty iterator, return None.
sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Provide an iterator over this stack iterating over up, focus and then down.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Provide an iterator over this stack iterating over up, focus and then down with mutable references.
sourcepub fn unravel(&self) -> impl Iterator<Item = &T>
pub fn unravel(&self) -> impl Iterator<Item = &T>
Iterate over the clients in this stack from the the focused element down through the stack.
let unraveled: Vec<u8> = stack!([1, 2], 3, [4, 5]).unravel().copied().collect();
assert_eq!(unraveled, vec![3, 4, 5, 1, 2]);
sourcepub fn flatten(self) -> Vec<T>
pub fn flatten(self) -> Vec<T>
Flatten a Stack into a Vector, losing the information of which element is focused.
sourcepub fn swap_focus_and_head(&mut self) -> &mut Self
pub fn swap_focus_and_head(&mut self) -> &mut Self
Swap the current head element with the focused element in the stack order. Focus stays with the original focused element.
sourcepub fn rotate_focus_to_head(&mut self) -> &mut Self
pub fn rotate_focus_to_head(&mut self) -> &mut Self
Rotate the Stack until the current focused element is in the head position
sourcepub fn focus_head(&mut self) -> &mut Self
pub fn focus_head(&mut self) -> &mut Self
Move focus to the element in the head position
sourcepub fn insert(&mut self, t: T) -> &mut Self
pub fn insert(&mut self, t: T) -> &mut Self
Insert the given element in place of the current focus, pushing the current focus down the Stack.
sourcepub fn remove_focused(self) -> (T, Option<Self>)
pub fn remove_focused(self) -> (T, Option<Self>)
Remove the focused element of this Stack. If this was the only element then the stack is dropped and None is returned.
sourcepub fn remove(self, t: &T) -> (Option<T>, Option<Self>)where
T: PartialEq,
pub fn remove(self, t: &T) -> (Option<T>, Option<Self>)where
T: PartialEq,
Remove an element from the stack.
If the element was present it is returned along with the rest of the Stack. If this was the last element in the stack, the stack is dropped and None is returned.
sourcepub fn map<F, U>(self, f: F) -> Stack<U>where
F: Fn(T) -> U,
pub fn map<F, U>(self, f: F) -> Stack<U>where
F: Fn(T) -> U,
Map a function over all elements in this Stack, returning a new one.
sourcepub fn filter<F>(self, f: F) -> Option<Self>
pub fn filter<F>(self, f: F) -> Option<Self>
Retain only elements which satisfy the given predicate. If the focused element is removed then focus shifts to the first remaining element after it, if there are no elements after then focus moves to the first remaining element before. If no elements satisfy the predicate then None is returned.
sourcepub fn reverse(&mut self) -> &mut Self
pub fn reverse(&mut self) -> &mut Self
Reverse the ordering of a Stack (up becomes down) while maintaining focus.
sourcepub fn focus_up(&mut self) -> &mut Self
pub fn focus_up(&mut self) -> &mut Self
Move focus from the current element up the stack, wrapping to the bottom if focus is already at the top.
sourcepub fn focus_down(&mut self) -> &mut Self
pub fn focus_down(&mut self) -> &mut Self
Move focus from the current element down the stack, wrapping to the top if focus is already at the bottom.
sourcepub fn focus_element_by<F>(&mut self, f: F)
pub fn focus_element_by<F>(&mut self, f: F)
Focus the first element found matching the given predicate function.
If no matching elements are found, the Stack will be left in its original state.
sourcepub fn swap_up(&mut self) -> &mut Self
pub fn swap_up(&mut self) -> &mut Self
Swap the focused element with the one above, wrapping from top to bottom. The currently focused element is maintained by this operation.
sourcepub fn swap_down(&mut self) -> &mut Self
pub fn swap_down(&mut self) -> &mut Self
Swap the focused element with the one below, wrapping from top to bottom. The currently focused element is maintained by this operation.
sourcepub fn rotate_up(&mut self) -> &mut Self
pub fn rotate_up(&mut self) -> &mut Self
Rotate all elements of the stack forward, wrapping from top to bottom. The currently focused element in the stack is maintained by this operation.
sourcepub fn rotate_down(&mut self) -> &mut Self
pub fn rotate_down(&mut self) -> &mut Self
Rotate all elements of the stack back, wrapping from bottom to top. The currently focused element in the stack is maintained by this operation.
source§impl<T: Clone> Stack<T>
impl<T: Clone> Stack<T>
sourcepub fn from_filtered<F>(&self, f: F) -> Option<Self>
pub fn from_filtered<F>(&self, f: F) -> Option<Self>
Attempt to create a new Stack from this one by filtering the existing
elements using a predicate function. This will return None
if no
elements match the predicate.