Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZeroVec::take_last and ::take_first #4651

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions utils/zerovec/src/zerovec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,91 @@ where
*self = Self::new_borrowed(&[])
}

/// Removes the first element of the ZeroVec. The ZeroVec remains in the same
/// borrowed or owned state.
///
/// # Examples
///
/// ```
/// # use crate::zerovec::ule::AsULE;
/// use zerovec::ZeroVec;
///
/// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
/// let mut zerovec: ZeroVec<u16> =
/// ZeroVec::parse_byte_slice(bytes).expect("infallible");
/// assert!(!zerovec.is_owned());
///
/// let first = zerovec.take_first().unwrap();
/// assert_eq!(first, 0x00D3);
/// assert!(!zerovec.is_owned());
///
/// let mut zerovec = zerovec.into_owned();
/// assert!(zerovec.is_owned());
/// let first = zerovec.take_first().unwrap();
/// assert_eq!(first, 0x0119);
/// assert!(zerovec.is_owned());
/// ```
pub fn take_first(&mut self) -> Option<T> {
match core::mem::take(self).into_cow() {
Cow::Owned(mut vec) => {
if vec.is_empty() {
return None;
}
let ule = vec.remove(0);
let rv = T::from_unaligned(ule);
*self = ZeroVec::new_owned(vec);
Some(rv)
}
Cow::Borrowed(b) => {
let (ule, remainder) = b.split_first()?;
let rv = T::from_unaligned(*ule);
*self = ZeroVec::new_borrowed(remainder);
Some(rv)
}
}
}

/// Removes the last element of the ZeroVec. The ZeroVec remains in the same
/// borrowed or owned state.
///
/// # Examples
///
/// ```
/// # use crate::zerovec::ule::AsULE;
/// use zerovec::ZeroVec;
///
/// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
/// let mut zerovec: ZeroVec<u16> =
/// ZeroVec::parse_byte_slice(bytes).expect("infallible");
/// assert!(!zerovec.is_owned());
///
/// let last = zerovec.take_last().unwrap();
/// assert_eq!(last, 0x01CD);
/// assert!(!zerovec.is_owned());
///
/// let mut zerovec = zerovec.into_owned();
/// assert!(zerovec.is_owned());
/// let last = zerovec.take_last().unwrap();
/// assert_eq!(last, 0x01A5);
/// assert!(zerovec.is_owned());
/// ```
pub fn take_last(&mut self) -> Option<T> {
match core::mem::take(self).into_cow() {
Cow::Owned(mut vec) => {
let ule = vec.pop()?;
let rv = T::from_unaligned(ule);
*self = ZeroVec::new_owned(vec);
Some(rv)
}
Cow::Borrowed(b) => {
let (ule, remainder) = b.split_last()?;
let rv = T::from_unaligned(*ule);
*self = ZeroVec::new_borrowed(remainder);
Some(rv)
}
}
}

/// Converts the type into a `Cow<'a, [T::ULE]>`, which is
/// the logical equivalent of this type's internal representation
#[inline]
Expand Down
Loading