Skip to content

Commit

Permalink
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion zvariant/src/array.rs
Original file line number Diff line number Diff line change
@@ -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>>,
4 changes: 2 additions & 2 deletions zvariant/src/dict.rs
Original file line number Diff line number Diff line change
@@ -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>,
@@ -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>,
2 changes: 1 addition & 1 deletion zvariant/src/maybe.rs
Original file line number Diff line number Diff line change
@@ -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>,
2 changes: 1 addition & 1 deletion zvariant/src/structure.rs
Original file line number Diff line number Diff line change
@@ -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>,
30 changes: 30 additions & 0 deletions zvariant/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::{
cmp::Ordering,
fmt::{Display, Write},
hash::{Hash, Hasher},
marker::PhantomData,
mem::discriminant,
str,
};

@@ -101,6 +103,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<'_> {

0 comments on commit 2211eec

Please sign in to comment.