From 8cdfa445d6629ffef4cb84967ff7017654045bc2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 2 Jul 2025 18:36:06 -0600 Subject: chore: add vendor directory --- vendor/nonempty/src/nonzero.rs | 96 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 vendor/nonempty/src/nonzero.rs (limited to 'vendor/nonempty/src/nonzero.rs') diff --git a/vendor/nonempty/src/nonzero.rs b/vendor/nonempty/src/nonzero.rs new file mode 100644 index 00000000..4d611022 --- /dev/null +++ b/vendor/nonempty/src/nonzero.rs @@ -0,0 +1,96 @@ +#[cfg(feature = "arbitrary")] +use arbitrary::Arbitrary; +use std::num::NonZeroUsize; + +/// A non-empty list which statically guarantees certain operations +/// cannot return zero, using [`std::num::NonZeroUsize`]. +/// +/// *Experimental* +/// +#[repr(transparent)] +#[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct NonEmpty(super::NonEmpty); + +impl NonEmpty { + /// Get the length of the list. + pub fn len(&self) -> NonZeroUsize { + unsafe { NonZeroUsize::new_unchecked(self.0.tail.len() + 1) } + } + + /// Get the capacity of the list. + pub fn capacity(&self) -> NonZeroUsize { + unsafe { NonZeroUsize::new_unchecked(self.0.tail.capacity() + 1) } + } + + /// Truncate the list to a certain size. + pub fn truncate(&mut self, len: NonZeroUsize) { + self.tail.truncate(usize::from(len) - 1); + } +} + +impl From> for NonEmpty { + fn from(other: super::NonEmpty) -> NonEmpty { + NonEmpty(other) + } +} + +impl std::ops::Deref for NonEmpty { + type Target = super::NonEmpty; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl std::ops::DerefMut for NonEmpty { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +#[cfg(test)] +mod tests { + use crate::nonzero; + use crate::NonEmpty; + + use std::convert::TryInto; + + #[test] + fn test_nonzero() { + let nonempty: nonzero::NonEmpty<_> = NonEmpty::from((0, vec![1, 2, 3])).into(); + + assert_eq!(nonempty.len(), 4.try_into().unwrap()); + assert_eq!(nonempty.capacity(), 4.try_into().unwrap()); + } + + #[cfg(feature = "arbitrary")] + mod arbitrary { + use crate::nonzero; + use arbitrary::{Arbitrary, Unstructured}; + + use std::convert::TryInto; + + #[test] + fn test_nonzero_arbitrary_empty_tail() -> arbitrary::Result<()> { + let mut u = Unstructured::new(&[1, 2, 3, 4]); + let nonempty: nonzero::NonEmpty<_> = nonzero::NonEmpty::::arbitrary(&mut u)?; + + assert_eq!(nonempty.len(), 1.try_into().unwrap()); + assert_eq!(nonempty.capacity(), 1.try_into().unwrap()); + + Ok(()) + } + + #[test] + fn test_nonzero_arbitrary_with_tail() -> arbitrary::Result<()> { + let mut u = Unstructured::new(&[1, 2, 3, 4, 5, 6, 7, 8]); + let nonempty: nonzero::NonEmpty<_> = nonzero::NonEmpty::::arbitrary(&mut u)?; + + assert_eq!(nonempty.len(), 2.try_into().unwrap()); + assert_eq!(nonempty.capacity(), 5.try_into().unwrap()); + + Ok(()) + } + } +} -- cgit v1.2.3