Skip to content

Commit c6e71ad

Browse files
committed
Make ApiString and ApiByteSlice comparable.
1 parent 1980f11 commit c6e71ad

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/types.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct Timeout(u32);
8888
/// A Rust UTF-8 string, but compatible with FFI. Assume the lifetime is only
8989
/// valid until the callee returns to the caller. Is not null-terminated.
9090
#[repr(C)]
91-
#[derive(Clone)]
91+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
9292
pub struct ApiString<'a>(ApiByteSlice<'a>);
9393

9494
/// A Rust u8 slice, but compatible with FFI. Assume the lifetime is only valid
@@ -261,6 +261,42 @@ impl<'a> From<&'a [u8]> for ApiByteSlice<'a> {
261261
}
262262
}
263263

264+
impl<'a> core::cmp::PartialEq for ApiByteSlice<'a> {
265+
/// Check if two ApiByteSlices are equal.
266+
///
267+
/// We just make some actual slices and compare then.
268+
fn eq(&self, rhs: &Self) -> bool {
269+
if self.data_len != rhs.data_len {
270+
return false;
271+
}
272+
let this_slice = self.as_slice();
273+
let that_slice = rhs.as_slice();
274+
this_slice == that_slice
275+
}
276+
}
277+
278+
impl<'a> core::cmp::Eq for ApiByteSlice<'a> {}
279+
280+
impl<'a> core::cmp::Ord for ApiByteSlice<'a> {
281+
/// Compare two ApiByteSlices.
282+
///
283+
/// We just make some actual slices and compare then.
284+
fn cmp(&self, rhs: &Self) -> core::cmp::Ordering {
285+
let this_slice = self.as_slice();
286+
let that_slice = rhs.as_slice();
287+
this_slice.cmp(that_slice)
288+
}
289+
}
290+
291+
impl<'a> core::cmp::PartialOrd for ApiByteSlice<'a> {
292+
/// Compare two ApiByteSlices.
293+
///
294+
/// We are `Ord` so we can defer to that.
295+
fn partial_cmp(&self, rhs: &Self) -> core::option::Option<core::cmp::Ordering> {
296+
Some(self.cmp(rhs))
297+
}
298+
}
299+
264300
// ApiBuffer
265301

266302
impl<'a> ApiBuffer<'a> {

0 commit comments

Comments
 (0)