Skip to content

Commit

Permalink
✨ zv: impl Hash for Value<'_>
Browse files Browse the repository at this point in the history
  • Loading branch information
sivizius authored and mguentner committed Oct 31, 2023
1 parent 31834d4 commit 41882d0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion zvariant/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
///
/// [`Value`]: enum.Value.html#variant.Array
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct Array<'a> {
element_signature: Signature<'a>,
elements: Vec<Value<'a>>,
Expand Down
4 changes: 2 additions & 2 deletions zvariant/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{value_display_fmt, Basic, DynamicType, Error, Signature, Type, Value
///
/// [`Value`]: enum.Value.html#variant.Dict
/// [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct Dict<'k, 'v> {
entries: BTreeSet<DictEntry<'k, 'v>>,
key_signature: Signature<'k>,
Expand Down Expand Up @@ -261,7 +261,7 @@ where

// TODO: Conversion of Dict from/to BTreeMap

#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
struct DictEntry<'k, 'v> {
key: Value<'k>,
value: Value<'v>,
Expand Down
2 changes: 1 addition & 1 deletion zvariant/src/maybe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{value_display_fmt, Error, Signature, Type, Value};
/// API is provided to convert from, and to `Option<T>`.
///
/// [`Value`]: enum.Value.html
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Maybe<'a> {
value: Box<Option<Value<'a>>>,
value_signature: Signature<'a>,
Expand Down
2 changes: 1 addition & 1 deletion zvariant/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'de> Visitor<'de> for StructureVisitor<'de> {
/// API is provided to convert from, and to tuples.
///
/// [`Value`]: enum.Value.html
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Structure<'a> {
fields: Vec<Value<'a>>,
signature: Signature<'a>,
Expand Down
34 changes: 33 additions & 1 deletion zvariant/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use core::str;
use core::{
hash::{Hash, Hasher},
mem::discriminant,
str,
};
use std::{
fmt::{Display, Write},
marker::PhantomData,
Expand Down Expand Up @@ -100,6 +104,34 @@ pub enum Value<'a> {
Fd(Fd),
}

impl Hash for Value<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
discriminant(self).hash(state);
match self {
Self::U8(inner) => inner.hash(state),
Self::Bool(inner) => inner.hash(state),
Self::I16(inner) => inner.hash(state),
Self::U16(inner) => inner.hash(state),
Self::I32(inner) => inner.hash(state),
Self::U32(inner) => inner.hash(state),
Self::I64(inner) => inner.hash(state),
Self::U64(inner) => inner.hash(state),
Self::F64(inner) => inner.to_le_bytes().hash(state),
Self::Str(inner) => inner.hash(state),
Self::Signature(inner) => inner.hash(state),
Self::ObjectPath(inner) => inner.hash(state),
Self::Value(inner) => inner.hash(state),
Self::Array(inner) => inner.hash(state),
Self::Dict(inner) => inner.hash(state),
Self::Structure(inner) => inner.hash(state),
#[cfg(feature = "gvariant")]
Self::Maybe(inner) => inner.hash(state),
#[cfg(unix)]
Self::Fd(inner) => inner.hash(state),
}
}
}

impl Eq for Value<'_> {}

impl Ord for Value<'_> {
Expand Down

0 comments on commit 41882d0

Please sign in to comment.