Skip to content

Commit

Permalink
Implement PartialOrd and Ord for Cow and fix Eq impl (#29)
Browse files Browse the repository at this point in the history
Luro02 authored Apr 22, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent da283e0 commit f823643
Showing 2 changed files with 72 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/generic.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
use alloc::borrow::{Borrow, Cow as StdCow};
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
@@ -18,7 +19,6 @@ use crate::wide::internal::Wide;
/// A clone-on-write smart pointer, mostly compatible with [`std::borrow::Cow`](https://doc.rust-lang.org/std/borrow/enum.Cow.html).
///
/// This type is using a generic `U: Capacity`. Use either [`beef::Cow`](../type.Cow.html) or [`beef::lean::Cow`](../lean/type.Cow.html) in your code.
#[derive(Eq)]
pub struct Cow<'a, T: Beef + ?Sized + 'a, U: Capacity> {
/// Pointer to data
ptr: NonNull<T::PointerT>,
@@ -288,6 +288,37 @@ where
}
}

impl<T, U> Eq for Cow<'_, T, U>
where
T: Eq + Beef + ?Sized,
U: Capacity,
{
}

impl<A, B, U, V> PartialOrd<Cow<'_, B, V>> for Cow<'_, A, U>
where
A: Beef + ?Sized + PartialOrd<B>,
B: Beef + ?Sized,
U: Capacity,
V: Capacity,
{
#[inline]
fn partial_cmp(&self, other: &Cow<'_, B, V>) -> Option<Ordering> {
PartialOrd::partial_cmp(self.borrow(), other.borrow())
}
}

impl<T, U> Ord for Cow<'_, T, U>
where
T: Ord + Beef + ?Sized,
U: Capacity,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(self.borrow(), other.borrow())
}
}

impl<'a, T, U> From<&'a T> for Cow<'a, T, U>
where
T: Beef + ?Sized,
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -155,6 +155,46 @@ macro_rules! test { ($tmod:ident => $cow:path) => {
assert_eq!(hash2, hash3);
}

#[test]
fn ord_and_partial_ord() {
use std::cmp::Ordering;

macro_rules! generate_order_tests {
( $f:tt => $order:expr => $left:expr, $right:expr ) => {
assert_eq!(
Cow::<str>::borrowed($left).$f(&Cow::<str>::borrowed($right)),
$order
);

assert_eq!(
Cow::<str>::owned($left.to_owned())
.$f(&Cow::<str>::borrowed($right)),
$order
);

assert_eq!(
Cow::<str>::borrowed($left)
.$f(&Cow::<str>::owned($right.to_owned())),
$order
);

assert_eq!(
Cow::<str>::owned($left.to_owned())
.$f(&Cow::<str>::owned($right.to_owned())),
$order
);
}
}

generate_order_tests!(partial_cmp => Some(Ordering::Equal) => "a", "a");
generate_order_tests!(partial_cmp => Some(Ordering::Less) => "a", "b");
generate_order_tests!(partial_cmp => Some(Ordering::Greater) => "b", "a");

generate_order_tests!(cmp => Ordering::Equal => "a", "a");
generate_order_tests!(cmp => Ordering::Less => "a", "b");
generate_order_tests!(cmp => Ordering::Greater => "b", "a");
}

#[test]
fn from_std_cow() {
let std = std::borrow::Cow::Borrowed("Hello World");

0 comments on commit f823643

Please sign in to comment.