Skip to content

Commit

Permalink
Add push_last to Nested
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Kock committed Sep 4, 2022
1 parent e05b2e8 commit fc647d5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ impl<T: Collection> Nested<T> {
self.indices.push(len);
}

/// Pushes to the last [`Collection`] in this [`Nested`].
///
/// # Panics
/// Panics when the [`Nested`] is empty, i.e. does not contain any [`Collection`].
///
/// # Examples
/// ```
/// # use nested::Nested;
/// let mut n = Nested::<String>::new();
///
/// n.push("123");
///
/// n.push("abc");
/// n.push_last("def");
///
/// assert_eq!(n.iter().collect::<Vec<_>>(), vec!["123", "abcdef"]);
/// ```
pub fn push_last<I: AsRef<Item<T>>>(&mut self, el: I) {
if self.indices.len() < 2 {
panic!("no collection to push to");
}

self.data.extend_from_slice(el.as_ref());
let last_mut = self.indices.last_mut().expect("unreachable: index.len > 2");
*last_mut = self.data.len();
}

/// Removes the last element from a `Nested` and returns it, or None if it is empty.
pub fn pop(&mut self) -> Option<T> {
if self.indices.len() > 1 {
Expand Down

0 comments on commit fc647d5

Please sign in to comment.