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
40 changes: 40 additions & 0 deletions crates/slice-dst/src/provided_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::cmp::Ordering;

use super::*;

#[repr(C)]
Expand Down Expand Up @@ -174,6 +176,26 @@ unsafe impl<Header, Item> Erasable for SliceWithHeader<Header, Item> {
const ACK_1_1_0: bool = true;
}

impl<Header, Item> PartialOrd for SliceWithHeader<Header, Item>
where
Header: PartialOrd,
Item: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(&self.header, &self.slice).partial_cmp(&(&other.header, &other.slice))
}
}

impl<Header, Item> Ord for SliceWithHeader<Header, Item>
where
Header: Ord,
Item: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
(&self.header, &self.slice).cmp(&(&other.header, &other.slice))
}
}

#[repr(C)]
#[derive(Debug, Eq, PartialEq, Hash)]
/// A custom str-based DST.
Expand Down Expand Up @@ -246,3 +268,21 @@ unsafe impl<Header> Erasable for StrWithHeader<Header> {

const ACK_1_1_0: bool = true;
}

impl<Header> PartialOrd for StrWithHeader<Header>
where
Header: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(&self.header, &self.str).partial_cmp(&(&other.header, &other.str))
}
}

impl<Header> Ord for StrWithHeader<Header>
where
Header: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
(&self.header, &self.str).cmp(&(&other.header, &other.str))
}
}
125 changes: 125 additions & 0 deletions crates/slice-dst/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,131 @@ fn actual_zst() {
}
}

#[test]
fn str_eq_cmp() {
[
[("*", "AB"), ("*", "ab")],
[("*", "AB"), ("*", "a")],
[("*", "A"), ("*", "ab")],
[("A", "*"), ("a", "*")],
[("a", "*"), ("A", "*")],
[("AB", "*"), ("a", "*")],
[("A", "*"), ("ab", "*")],
]
.iter()
.for_each(|[lt @ (lh, ls), rt @ (rh, rs)]| {
let l: Box<StrWithHeader<&str>> = StrWithHeader::new(*lh, ls);
let r: Box<StrWithHeader<&str>> = StrWithHeader::new(*rh, rs);

assert_eq!(l, l);
assert_eq!(r, r);

assert_ne!(l, r);
assert_ne!(r, l);

assert_eq!(l <= l, lt <= lt, "{lt:?} <= {lt:?}");
assert_eq!(l >= l, lt >= lt, "{lt:?} >= {lt:?}");

assert_eq!(l < l, lt < lt, "{lt:?} < {lt:?}");
assert_eq!(l > l, lt > lt, "{lt:?} > {lt:?}");

assert_eq!(r <= r, rt <= rt, "{rt:?} <= {rt:?}");
assert_eq!(r >= r, rt >= rt, "{rt:?} >= {rt:?}");

assert_eq!(r < r, rt < rt, "{rt:?} < {rt:?}");
assert_eq!(r > r, rt > rt, "{rt:?} > {rt:?}");

assert_eq!(l < r, lt < rt, "{lt:?} < {rt:?}");
assert_eq!(r > l, rt > lt, "{rt:?} > {lt:?}");
})
}

#[test]
fn slice_eq_cmp() {
[
[(0, &[0, 0][..]), (1, &[0, 0][..])],
[(1, &[0, 0][..]), (0, &[0, 0][..])],
[(0, &[0][..]), (0, &[0, 0][..])],
[(0, &[0, 0][..]), (0, &[0][..])],
[(0, &[1, 2][..]), (0, &[10, 20][..])],
]
.iter()
.for_each(|[lt @ (lh, ls), rt @ (rh, rs)]| {
let l: Box<SliceWithHeader<i32, i32>> = SliceWithHeader::from_slice(*lh, ls);
let r: Box<SliceWithHeader<i32, i32>> = SliceWithHeader::from_slice(*rh, rs);

assert_eq!(l, l);
assert_eq!(r, r);

assert_ne!(l, r);
assert_ne!(r, l);

assert_eq!(l <= l, lt <= lt, "{lt:?} <= {lt:?}");
assert_eq!(l >= l, lt >= lt, "{lt:?} >= {lt:?}");

assert_eq!(l < l, lt < lt, "{lt:?} < {lt:?}");
assert_eq!(l > l, lt > lt, "{lt:?} > {lt:?}");

assert_eq!(r <= r, rt <= rt, "{rt:?} <= {rt:?}");
assert_eq!(r >= r, rt >= rt, "{rt:?} >= {rt:?}");

assert_eq!(r < r, rt < rt, "{rt:?} < {rt:?}");
assert_eq!(r > r, rt > rt, "{rt:?} > {rt:?}");

assert_eq!(l < r, lt < rt, "{lt:?} < {rt:?}");
assert_eq!(r > l, rt > lt, "{rt:?} > {lt:?}");
})
}

#[test]
fn slice_partial_eq_cmp() {
[
[(0.0, &[0.0, 0.0][..]), (1.0, &[0.0, 0.0][..])],
[(1.0, &[0.0, 0.0][..]), (0.0, &[0.0, 0.0][..])],
[(0.0, &[0.0][..]), (0.0, &[0.0, 0.0][..])],
[(0.0, &[0.0, 0.0][..]), (0.0, &[0.0][..])],
[(0.0, &[1.0, 2.0][..]), (0.0, &[10.0, 20.0][..])],
]
.iter()
.for_each(|[lt @ (lh, ls), rt @ (rh, rs)]| {
let l: Box<SliceWithHeader<f32, f32>> = SliceWithHeader::from_slice(*lh, ls);
let r: Box<SliceWithHeader<f32, f32>> = SliceWithHeader::from_slice(*rh, rs);

assert_eq!(l, l);
assert_eq!(r, r);

assert_ne!(l, r);
assert_ne!(r, l);

assert_eq!(l <= l, lt <= lt, "{lt:?} <= {lt:?}");
assert_eq!(l >= l, lt >= lt, "{lt:?} >= {lt:?}");

assert_eq!(l < l, lt < lt, "{lt:?} < {lt:?}");
assert_eq!(l > l, lt > lt, "{lt:?} > {lt:?}");

assert_eq!(r <= r, rt <= rt, "{rt:?} <= {rt:?}");
assert_eq!(r >= r, rt >= rt, "{rt:?} >= {rt:?}");

assert_eq!(r < r, rt < rt, "{rt:?} < {rt:?}");
assert_eq!(r > r, rt > rt, "{rt:?} > {rt:?}");

assert_eq!(l < r, lt < rt, "{lt:?} < {rt:?}");
assert_eq!(r > l, rt > lt, "{rt:?} > {lt:?}");
})
}

const fn is_partial_ord<T: ?Sized + PartialOrd>() {}
const fn is_ord<T: ?Sized + Ord>() {}

// compile-time check that PartialOrd/Ord is correctly derived
const _: () = is_partial_ord::<SliceWithHeader<f64, f64>>();
const _: () = is_partial_ord::<SliceWithHeader<f64, u64>>();
const _: () = is_partial_ord::<SliceWithHeader<u64, f64>>();
const _: () = is_ord::<SliceWithHeader<u64, u64>>();

const _: () = is_partial_ord::<StrWithHeader<f64>>();
const _: () = is_ord::<StrWithHeader<u64>>();

type Data = usize;
#[repr(transparent)]
#[derive(Debug, Clone)]
Expand Down