Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
RoDmitry committed Dec 26, 2023
1 parent 61e6f0b commit 85afa6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
44 changes: 20 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,17 @@ pub mod num {
pub mod ord {
use core::cmp;

/// Set `left` as `right` if `left` is Less than `right`, set `right` as Default
#[inline]
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` as `right` if `left` is Greater than `right`, set `right` as Default
#[inline]
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) {
pub fn max<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) {
pub fn min<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);
}
Expand Down Expand Up @@ -311,15 +295,23 @@ pub mod string {
/// Append the contents of right to left.
#[inline]
pub fn append(left: &mut String, right: &mut String) {
let new = core::mem::take(right);
left.push_str(&new);
if left.is_empty() {
core::mem::swap(left, right);
} else {
let new = core::mem::take(right);
left.push_str(&new);
}
}

/// Prepend the contents of right to left.
#[inline]
pub fn prepend(left: &mut String, right: &mut String) {
right.push_str(left);
*left = core::mem::take(right);
if left.is_empty() {
core::mem::swap(left, right);
} else if !right.is_empty() {
right.push_str(left);
*left = core::mem::take(right);
}
}
}

Expand Down Expand Up @@ -351,8 +343,12 @@ pub mod vec {
/// Prepend the contents of right to left.
#[inline]
pub fn prepend<T>(left: &mut Vec<T>, right: &mut Vec<T>) {
right.append(left);
core::mem::swap(left, right);
if left.is_empty() {
core::mem::swap(left, right);
} else if !right.is_empty() {
right.append(left);
core::mem::swap(left, right);
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ fn test_num_saturating_add() {
}

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

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

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

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

0 comments on commit 85afa6c

Please sign in to comment.