Skip to content

Commit 361773b

Browse files
alokprJEnoch
andauthored
feat: add hash support for Encoding (#1912)
* feat: add hash support for Encoding * fixed formatting * Add Hash trait only for std feature --------- Co-authored-by: Julien Enoch <[email protected]>
1 parent b146561 commit 361773b

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

commons/zenoh-buffers/src/zslice.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ impl<Rhs: AsRef<[u8]> + ?Sized> PartialEq<Rhs> for ZSlice {
222222

223223
impl Eq for ZSlice {}
224224

225+
#[cfg(feature = "std")]
226+
impl std::hash::Hash for ZSlice {
227+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
228+
self.as_slice().hash(state);
229+
}
230+
}
231+
225232
impl fmt::Display for ZSlice {
226233
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
227234
write!(f, "{:02x?}", self.as_slice())
@@ -444,4 +451,24 @@ mod tests {
444451

445452
assert_eq!(buf.as_slice(), zslice.as_slice());
446453
}
454+
455+
#[test]
456+
fn hash() {
457+
use std::{
458+
collections::hash_map::DefaultHasher,
459+
hash::{Hash, Hasher},
460+
};
461+
462+
let buf = vec![1, 2, 3, 4, 5];
463+
let mut buf_hasher = DefaultHasher::new();
464+
buf.hash(&mut buf_hasher);
465+
let buf_hash = buf_hasher.finish();
466+
467+
let zslice: ZSlice = buf.clone().into();
468+
let mut zslice_hasher = DefaultHasher::new();
469+
zslice.hash(&mut zslice_hasher);
470+
let zslice_hash = zslice_hasher.finish();
471+
472+
assert_eq!(buf_hash, zslice_hash);
473+
}
447474
}

commons/zenoh-protocol/src/core/encoding.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub type EncodingId = u16;
2525
/// Nevertheless, it is worth highlighting that Zenoh still provides a default mapping as part
2626
/// of the API as per user convenience. That mapping has no impact on the Zenoh protocol definition.
2727
#[derive(Clone, Debug, PartialEq, Eq)]
28+
#[cfg_attr(feature = "std", derive(Hash))]
2829
pub struct Encoding {
2930
pub id: EncodingId,
3031
pub schema: Option<ZSlice>,

zenoh/src/api/encoding.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use zenoh_protocol::core::EncodingId;
6969
/// assert_eq!("text/plain;utf-8", &encoding2.to_string());
7070
/// ```
7171
#[repr(transparent)]
72-
#[derive(Clone, Debug, PartialEq, Eq)]
72+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7373
pub struct Encoding(zenoh_protocol::core::Encoding);
7474

7575
impl Encoding {
@@ -710,3 +710,33 @@ impl Encoding {
710710
Encoding(zenoh_protocol::core::Encoding { id, schema })
711711
}
712712
}
713+
714+
#[cfg(test)]
715+
mod tests {
716+
use std::{
717+
collections::hash_map::DefaultHasher,
718+
hash::{Hash, Hasher},
719+
};
720+
721+
use super::*;
722+
723+
#[test]
724+
fn hash() {
725+
let encodings = [
726+
Encoding::ZENOH_BYTES,
727+
Encoding::ZENOH_STRING,
728+
Encoding::ZENOH_STRING.with_schema("utf-8"),
729+
Encoding::ZENOH_STRING.with_schema("utf-8"),
730+
];
731+
let hashes = encodings.map(|e| {
732+
let mut hasher = DefaultHasher::new();
733+
e.hash(&mut hasher);
734+
hasher.finish()
735+
});
736+
737+
assert_ne!(hashes[0], hashes[1]);
738+
assert_ne!(hashes[0], hashes[2]);
739+
assert_ne!(hashes[1], hashes[2]);
740+
assert_eq!(hashes[2], hashes[3]);
741+
}
742+
}

0 commit comments

Comments
 (0)