Skip to content

Commit

Permalink
[eclipse-iceoryx#110] Implement Ord and PartialOrd for `ServiceNa…
Browse files Browse the repository at this point in the history
…me` and `FixedByteString`
  • Loading branch information
elfenpiff committed Feb 7, 2024
1 parent 78e8e17 commit eec8168
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* `MessagingPattern` implements `Display` [#64](https://github.com/eclipse-iceoryx/iceoryx2/issues/64)
* Introduce traits for all ports (`Listen`, `Notify`, `Publish`, `DefaultLoan`, `UninitLoan`, `Subscribe`)
and for samples (`PayloadMut`, `Payload`) [#69](https://github.com/eclipse-iceoryx/iceoryx2/issues/69)
* Implement `Ord` and `PartialOrd` for `FixedSizeByteString` and `ServiceName` [#110](https://github.com/eclipse-iceoryx/iceoryx2/issues/110)

### API Breaking Changes

Expand Down
23 changes: 23 additions & 0 deletions iceoryx2-bb/container/src/byte_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//! ```
use std::{
cmp::Ordering,
fmt::{Debug, Display},
hash::Hash,
mem::MaybeUninit,
Expand Down Expand Up @@ -73,6 +74,28 @@ pub struct FixedSizeByteString<const CAPACITY: usize> {
unsafe impl<const CAPACITY: usize> Send for FixedSizeByteString<CAPACITY> {}
unsafe impl<const CAPACITY: usize> Sync for FixedSizeByteString<CAPACITY> {}

impl<const CAPACITY: usize, const CAPACITY_OTHER: usize>
PartialOrd<FixedSizeByteString<CAPACITY_OTHER>> for FixedSizeByteString<CAPACITY>
{
fn partial_cmp(&self, other: &FixedSizeByteString<CAPACITY_OTHER>) -> Option<Ordering> {
for i in 0..std::cmp::min(self.len, other.len) {
match unsafe { *self.data[i].as_ptr() }.cmp(unsafe { &*other.data[i].as_ptr() }) {
Ordering::Less => return Some(Ordering::Less),
Ordering::Greater => return Some(Ordering::Greater),
_ => (),
}
}

Some(self.len.cmp(&other.len))
}
}

impl<const CAPACITY: usize> Ord for FixedSizeByteString<CAPACITY> {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}

impl<const CAPACITY: usize> Hash for FixedSizeByteString<CAPACITY> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write(self.as_bytes())
Expand Down
11 changes: 11 additions & 0 deletions iceoryx2-bb/container/tests/byte_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,14 @@ fn fixed_size_byte_string_strip_suffix_works() {
assert_that!(sut_clone.strip_suffix(b"msla"), eq false);
assert_that!(sut_clone, eq sut);
}

#[test]
fn fixed_size_byte_ordering_works() {
unsafe {
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuubla")), eq std::cmp::Ordering::Equal );
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuvbla")), eq std::cmp::Ordering::Less );
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuubaa")), eq std::cmp::Ordering::Greater );
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuubla123")), eq std::cmp::Ordering::Less );
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuu")), eq std::cmp::Ordering::Greater );
}
}
2 changes: 1 addition & 1 deletion iceoryx2/src/service/service_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use serde::{de::Visitor, Deserialize, Serialize};

const SERVICE_NAME_LENGTH: usize = 255;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ServiceName {
value: iceoryx2_bb_container::byte_string::FixedSizeByteString<SERVICE_NAME_LENGTH>,
}
Expand Down

0 comments on commit eec8168

Please sign in to comment.