thirdparty: Implement Borrow/Hash/Eq for ThirdPartyIdentifier

This commit is contained in:
Mikoto 2024-06-18 21:35:19 +00:00 committed by GitHub
parent afb5ae0102
commit 629b06e1e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View File

@ -10,6 +10,8 @@ Improvements:
- Add the `InvalidHeaderValue` variant to the `DeserializationError` struct, for
cases where we receive a HTTP header with an unexpected value.
- Implement `Eq`/`Hash`/`PartialEq` for `ThirdPartyIdentifier`, to check whether
a `ThirdPartyIdentifier` has already been added by another user.
# 0.13.0

View File

@ -2,7 +2,10 @@
//!
//! [thirdparty]: https://spec.matrix.org/latest/client-server-api/#third-party-networks
use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
hash::{Hash, Hasher},
};
use serde::{Deserialize, Serialize};
@ -229,7 +232,6 @@ pub enum Medium {
/// this type using `ThirdPartyIdentifier::Init` / `.into()`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ThirdPartyIdentifier {
/// The third party identifier address.
pub address: String,
@ -244,6 +246,20 @@ pub struct ThirdPartyIdentifier {
pub added_at: MilliSecondsSinceUnixEpoch,
}
impl Eq for ThirdPartyIdentifier {}
impl Hash for ThirdPartyIdentifier {
fn hash<H: Hasher>(&self, hasher: &mut H) {
(self.medium.as_str(), &self.address).hash(hasher);
}
}
impl PartialEq for ThirdPartyIdentifier {
fn eq(&self, other: &ThirdPartyIdentifier) -> bool {
self.address == other.address && self.medium == other.medium
}
}
/// Initial set of fields of `ThirdPartyIdentifier`.
///
/// This struct will not be updated even if additional fields are added to `ThirdPartyIdentifier`