Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
version = "1.12.1"
version = "1.13.0"
authors = ["Philipp Korber <philipp@korber.dev>"]
categories = ["data-structures"]
description = "a std Vec wrapper assuring that it has at least 1 element"
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
25 changes: 24 additions & 1 deletion src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ macro_rules! shared_impl {
self.0.first_mut().unwrap()
}


/// Truncates this vector to given length.
///
/// # Errors
Expand Down Expand Up @@ -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,)?
Expand Down
18 changes: 18 additions & 0 deletions src/smallvec_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down