ZipList

Struct ZipList 

Source
pub struct ZipList<T> { /* private fields */ }
Expand description

A ZipList 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 ZipList::flatten.

This is a zipper over a VecDeque. Many of the methods that mutate the structure of the ZipList return back a mutable reference so that they are able to be chained.

Implementations§

Source§

impl<T> ZipList<T>

Source

pub fn new<I, J>(up: I, focus: T, down: J) -> Self
where I: IntoIterator<Item = T>, J: IntoIterator<Item = T>,

Create a new ZipList specifying the focused element and and elements above and below it.

Source

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.

Source

pub fn len(&self) -> usize

The number of elements in this ZipList.

Source

pub fn is_empty(&self) -> bool

Always false: a ZipList always has at least one focused element.

Source

pub fn iter(&self) -> Iter<'_, T>

Provide an iterator over this stack iterating over up, focus and then down.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Provide an iterator over this stack iterating over up, focus and then down with mutable references.

Source

pub fn unravel(&self) -> impl Iterator<Item = &T>

Iterate over the clients in this stack from the the focused element down through the stack.

Source

pub fn flatten(self) -> Vec<T>

Flatten a ZipList into a Vector, losing the information of which element is focused.

Source

pub fn head(&self) -> &T

Return a reference to the first element in this ZipList

Source

pub fn focused(&self) -> &T

Return a reference to the focused element in this ZipList

Source

pub fn last(&self) -> &T

Return a reference to the last element in this ZipList

Source

pub fn last_mut(&mut self) -> &mut T

Source

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.

Source

pub fn rotate_focus_to_head(&mut self) -> &mut Self

Rotate the ZipList until the current focused element is in the head position

Source

pub fn focus_head(&mut self) -> &mut Self

Move focus to the element in the head position

Source

pub fn focus_tail(&mut self) -> &mut Self

Move focus to the element in the head position

Source

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 ZipList.

Source

pub fn insert_at(&mut self, pos: Position, t: T) -> &mut Self

Insert the given element at the requested position in the ZipList. See Position for the semantics of each case. For all cases, the existing elements in the ZipList are pushed down to make room for the new one.

Source

pub fn remove_focused(self) -> (T, Option<Self>)

Remove the focused element of this ZipList. If this was the only element then the stack is dropped and None is returned.

Source

pub fn remove_focused_unchecked(&mut self) -> T

Removed the focused element of this Ziplist.

§Panics

Panics if the focus was the only element

Source

pub fn remove_where_with_default( &mut self, pred: impl Fn(&T) -> bool, default: impl Fn() -> T, ) -> Option<T>

Remove the first element that matches the given predicate function. If doing so would remove the last element of the ZipList then default_focus is called to ensure that there is a single element remaining as the current focus.

Source

pub fn map<F, U>(self, f: F) -> ZipList<U>
where F: Fn(T) -> U,

Map a function over all elements in this ZipList, returning a new one.

Source

pub fn filter<F>(self, f: F) -> Option<Self>
where F: Fn(&T) -> bool,

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.

Source

pub fn filter_unchecked<F>(&mut self, f: F)
where F: Fn(&T) -> bool,

Source

pub fn reverse(&mut self) -> &mut Self

Reverse the ordering of a ZipList (up becomes down) while maintaining focus.

Source

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.

Source

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.

Source

pub fn focus_element_by<F>(&mut self, f: F) -> bool
where F: Fn(&T) -> bool,

Focus the first element found matching the given predicate function.

Returns true if the focused element changed. If no matching elements are found, the ZipList will be left in its original state and false is returned.

Source

pub fn focus_element_by_mut<F>(&mut self, f: F) -> bool
where F: Fn(&mut T) -> bool,

Focus the first element found matching the given predicate function.

If no matching elements are found, the ZipList will be left in its original state.

Source

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.

Source

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.

Source

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.

Source

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> ZipList<T>

Source

pub fn extract<F>(&self, f: F) -> (Option<Self>, Vec<T>)
where F: Fn(&T) -> bool,

Extract elements satisfying a predicate into a Vec, leaving remaining elements in their original stack position.

Source§

impl<T: PartialEq> ZipList<T>

Source

pub fn contains(&self, t: &T) -> bool

Check whether a given element is in this ZipList

Source

pub fn focus_element(&mut self, t: &T)

Attempt to focus a given element in the ZipList if it is present.

If the requested element is not found, the ZipList will be left in its original state.

Source

pub fn remove(self, t: &T) -> (Option<T>, Option<Self>)

Remove an element from the stack.

If the element was present it is returned along with the rest of the ZipList. If this was the last element in the stack, the stack is dropped and None is returned.

Trait Implementations§

Source§

impl<T: Clone> Clone for ZipList<T>

Source§

fn clone(&self) -> ZipList<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ZipList<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for ZipList<T>

Source§

fn default() -> ZipList<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Display> Display for ZipList<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> IntoIterator for &'a ZipList<T>

Source§

type Item = (bool, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut ZipList<T>

Source§

type Item = (bool, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for ZipList<T>

Source§

type Item = (bool, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for ZipList<T>

Source§

fn eq(&self, other: &ZipList<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for ZipList<T>

Source§

impl<T> StructuralPartialEq for ZipList<T>

Auto Trait Implementations§

§

impl<T> Freeze for ZipList<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ZipList<T>
where T: RefUnwindSafe,

§

impl<T> Send for ZipList<T>
where T: Send,

§

impl<T> Sync for ZipList<T>
where T: Sync,

§

impl<T> Unpin for ZipList<T>
where T: Unpin,

§

impl<T> UnwindSafe for ZipList<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<I> IntoStreamingIterator for I
where I: IntoIterator,

Source§

fn into_streaming_iter(self) -> Convert<Self::IntoIter>

Source§

fn into_streaming_iter_ref<'a, T>(self) -> ConvertRef<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a T>, T: ?Sized,

Turns an IntoIterator of references into a StreamingIterator. Read more
Source§

fn into_streaming_iter_mut<'a, T>(self) -> ConvertMut<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a mut T>, T: ?Sized,

Turns an IntoIterator of mutable references into a StreamingIteratorMut. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more