From f23f3a227fb2275b46b625e40da06f7d7a8bb7b7 Mon Sep 17 00:00:00 2001 From: JasterV <49537445+JasterV@users.noreply.github.com> Date: Wed, 3 Jun 2026 15:27:08 +0200 Subject: [PATCH 1/2] implement shared `min` and `max` functions --- src/lib.rs | 18 ++++++++++++++++++ src/shared.rs | 25 ++++++++++++++++++++++++- src/smallvec_v1.rs | 18 ++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1db1631..aef43a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1365,6 +1365,24 @@ mod test { assert_eq!(first, 12); } + #[test] + fn min() { + let a = vec1![12u8, 13, 5, 20, 9]; + assert_eq!(a.min(), &5u8); + + let b = vec1![42u8]; + assert_eq!(b.min(), &42u8); + } + + #[test] + fn max() { + let a = vec1![12u8, 13, 5, 20, 9]; + assert_eq!(a.max(), &20u8); + + let b = vec1![42u8]; + assert_eq!(b.max(), &42u8); + } + #[test] fn from_vec_push() { assert_eq!(Vec1::from_vec_push(std::vec![], 1u8), vec1![1]); diff --git a/src/shared.rs b/src/shared.rs index 9762e28..6f20e86 100644 --- a/src/shared.rs +++ b/src/shared.rs @@ -174,7 +174,6 @@ macro_rules! shared_impl { self.0.first_mut().unwrap() } - /// Truncates this vector to given length. /// /// # Errors @@ -680,6 +679,30 @@ macro_rules! shared_impl { } } + impl<$t> $name<$t> + where + $item_ty: Ord, + $($tb : $trait,)? + { + /// Returns a reference to the minimum element in the vector. + /// + /// As this vector always contains at least one element, this always returns a value + /// instead of an `Option` (unlike `Iterator::min`). + pub fn min(&self) -> &$item_ty { + //UNWRAP_SAFE: len is at least 1 + self.0.iter().min().unwrap() + } + + /// Returns a reference to the maximum element in the vector. + /// + /// As this vector always contains at least one element, this always returns a value + /// instead of an `Option` (unlike `Iterator::max`). + pub fn max(&self) -> &$item_ty { + //UNWRAP_SAFE: len is at least 1 + self.0.iter().max().unwrap() + } + } + impl<$t> From<$name<$t>> for $wrapped<$t> where $($tb : $trait,)? diff --git a/src/smallvec_v1.rs b/src/smallvec_v1.rs index a6e9dc4..d5706ed 100644 --- a/src/smallvec_v1.rs +++ b/src/smallvec_v1.rs @@ -1021,6 +1021,24 @@ mod tests { .is_err()); } + #[test] + fn min() { + let a: SmallVec1<[u8; 4]> = smallvec1![12, 13, 5, 20, 9]; + assert_eq!(a.min(), &5); + + let b: SmallVec1<[u8; 4]> = smallvec1![42]; + assert_eq!(b.min(), &42); + } + + #[test] + fn max() { + let a: SmallVec1<[u8; 4]> = smallvec1![12, 13, 5, 20, 9]; + assert_eq!(a.max(), &20); + + let b: SmallVec1<[u8; 4]> = smallvec1![42]; + assert_eq!(b.max(), &42); + } + #[cfg(feature = "serde")] mod serde { use super::super::super::*; From 54950a5e137d8496bdb38893be82c28464b0b4a5 Mon Sep 17 00:00:00 2001 From: JasterV <49537445+JasterV@users.noreply.github.com> Date: Wed, 3 Jun 2026 15:30:15 +0200 Subject: [PATCH 2/2] update version --- Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 886faef..1b907f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -version = "1.12.1" +version = "1.13.0" authors = ["Philipp Korber "] categories = ["data-structures"] description = "a std Vec wrapper assuring that it has at least 1 element" @@ -41,7 +41,9 @@ smallvec-v1-write = ["std", "smallvec_v1_/write"] [dependencies] # Is a feature! -serde = { version = "1.0", optional = true, features = ["derive"], default-features=false } +serde = { version = "1.0", optional = true, features = [ + "derive", +], default-features = false } # In the future we will support smallvec v1 and v2 so if we had # a optional dependency called smallvec people might acidentally # pull it in as feature and create anoyences wrt. backward compatibility.