Skip to content

Commit

Permalink
More strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
RoDmitry committed Dec 25, 2023
1 parent 0703c6e commit 7895f00
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
33 changes: 29 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ pub trait Merge {
fn merge(&mut self, other: &mut Self);
}

// Merge strategies applicable to any types
pub mod any {
/// Swap `left` and `right` regardless of their values.
#[inline]
pub fn swap<T>(left: &mut T, right: &mut T) {
core::mem::swap(left, right);
}
}

// Merge strategies applicable to types implementing Default
pub mod default {
/// Overwrite `left` with `right` regardless of their values.
Expand Down Expand Up @@ -252,21 +261,37 @@ pub mod num {
pub mod ord {
use core::cmp;

/// Set left to the maximum of left and right.
/// Set `left` as `right` if `left` is Less than `right`, set `right` as Default
#[inline]
pub fn max<T: cmp::PartialOrd + Default>(left: &mut T, right: &mut T) {
pub fn max_def<T: cmp::PartialOrd + Default>(left: &mut T, right: &mut T) {
if cmp::PartialOrd::partial_cmp(left, right) == Some(cmp::Ordering::Less) {
*left = core::mem::take(right);
}
}

/// Set left to the minimum of left and right.
/// Set `left` as `right` if `left` is Greater than `right`, set `right` as Default
#[inline]
pub fn min<T: cmp::PartialOrd + Default>(left: &mut T, right: &mut T) {
pub fn min_def<T: cmp::PartialOrd + Default>(left: &mut T, right: &mut T) {
if cmp::PartialOrd::partial_cmp(left, right) == Some(cmp::Ordering::Greater) {
*left = core::mem::take(right);
}
}

/// Swap elements if `left` is Less than `right`.
#[inline]
pub fn max_swap<T: cmp::PartialOrd>(left: &mut T, right: &mut T) {
if cmp::PartialOrd::partial_cmp(left, right) == Some(cmp::Ordering::Less) {
core::mem::swap(left, right);
}
}

/// Swap elements if `left` is Greater than `right`.
#[inline]
pub fn min_swap<T: cmp::PartialOrd>(left: &mut T, right: &mut T) {
if cmp::PartialOrd::partial_cmp(left, right) == Some(cmp::Ordering::Greater) {
core::mem::swap(left, right);
}
}
}

#[cfg(feature = "std")]
Expand Down
19 changes: 15 additions & 4 deletions tests/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ fn test<T: std::fmt::Debug + Merge + PartialEq>(expected: T, mut left: T, mut ri
assert_eq!(expected, left);
}

#[test]
fn test_any_swap() {
#[derive(Debug, Merge, PartialEq)]
struct S(#[merge(strategy = ::merge2::any::swap)] u8);

test(S(2), S(1), S(2));
test(S(2), S(0), S(2));
test(S(0), S(1), S(0));
test(S(0), S(0), S(0));
}

#[test]
fn test_default_overwrite_any() {
#[derive(Debug, Merge, PartialEq)]
Expand Down Expand Up @@ -100,9 +111,9 @@ fn test_num_overwrite_zero() {
}

#[test]
fn test_ord_max() {
fn test_ord_max_def() {
#[derive(Debug, Merge, PartialEq)]
struct S(#[merge(strategy = ::merge2::ord::max)] u8);
struct S(#[merge(strategy = ::merge2::ord::max_def)] u8);

test(S(2), S(1), S(2));
test(S(2), S(2), S(1));
Expand All @@ -113,9 +124,9 @@ fn test_ord_max() {
}

#[test]
fn test_ord_min() {
fn test_ord_min_def() {
#[derive(Debug, Merge, PartialEq)]
struct S(#[merge(strategy = ::merge2::ord::min)] u8);
struct S(#[merge(strategy = ::merge2::ord::min_def)] u8);

test(S(1), S(1), S(2));
test(S(1), S(2), S(1));
Expand Down

0 comments on commit 7895f00

Please sign in to comment.