pub struct Documents<T> { /* private fields */ }state and storage only.Expand description
An ordered list of documents with one active document.
Removing a document keeps the same document active when possible: removing a document before the active one shifts the index, removing the active one selects its successor (or the new last document).
struct Doc(&'static str);
impl Document for Doc {
fn title(&self) -> &str { self.0 }
}
let mut docs = Documents::with_items(vec![Doc("a"), Doc("b")]);
assert_eq!(docs.active().map(Document::title), Some("a"));
docs.select_next();
assert_eq!(docs.active().map(Document::title), Some("b"));
docs.remove(0);
assert_eq!(docs.active_index(), 0);Implementations§
Source§impl<T> Documents<T>
impl<T> Documents<T>
Sourcepub fn with_items(items: Vec<T>) -> Self
pub fn with_items(items: Vec<T>) -> Self
A list with the first document active.
Sourcepub fn active_index(&self) -> usize
pub fn active_index(&self) -> usize
The active index, or 0 when empty.
Sourcepub fn active_mut(&mut self) -> Option<&mut T>
pub fn active_mut(&mut self) -> Option<&mut T>
The active document, mutably.
Sourcepub fn set_active(&mut self, index: usize) -> bool
pub fn set_active(&mut self, index: usize) -> bool
Select an existing document. Returns false when the index is out of range.
Sourcepub fn push(&mut self, item: T) -> usize
pub fn push(&mut self, item: T) -> usize
Append a document. Returns its index. The active document does not change.
Sourcepub fn push_active(&mut self, item: T) -> usize
pub fn push_active(&mut self, item: T) -> usize
Append a document and select it. Returns its index.
Sourcepub fn insert(&mut self, index: usize, item: T) -> usize
pub fn insert(&mut self, index: usize, item: T) -> usize
Insert a document at index (clamped to the end). Returns its index.
When inserting at or before the active document, the active document stays selected and its index shifts by one.
Sourcepub fn remove(&mut self, index: usize) -> Option<T>
pub fn remove(&mut self, index: usize) -> Option<T>
Remove a document and return it. Returns None when the index is out of range.
Sourcepub fn close_active(&mut self) -> Option<T>
pub fn close_active(&mut self) -> Option<T>
Remove the active document and return it.
Sourcepub fn select_next(&mut self) -> bool
pub fn select_next(&mut self) -> bool
Select the next document, wrapping around. Returns false when empty.
Sourcepub fn select_previous(&mut self) -> bool
pub fn select_previous(&mut self) -> bool
Select the previous document, wrapping around. Returns false when empty.
Sourcepub fn select_by(&mut self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
pub fn select_by(&mut self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
Select the first document matching predicate. Returns its index.
Sourcepub fn active_title(&self) -> Option<&str>where
T: Document,
pub fn active_title(&self) -> Option<&str>where
T: Document,
The title of the active document.
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Documents<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Documents<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl<T: Eq> Eq for Documents<T>
impl<T: PartialEq> StructuralPartialEq for Documents<T>
Auto Trait Implementations§
impl<T> Forget for Documents<T>
impl<T> Freeze for Documents<T>
impl<T> Move for Documents<T>
impl<T> RefUnwindSafe for Documents<T>where
T: RefUnwindSafe,
impl<T> Send for Documents<T>
impl<T> Sync for Documents<T>
impl<T> Unpin for Documents<T>
impl<T> UnsafeUnpin for Documents<T>
impl<T> UnwindSafe for Documents<T>where
RawVecInner: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more