Skip to content

Commit

Permalink
Feat/eq remote addr representation (#27)
Browse files Browse the repository at this point in the history
* feat: Eq for RemoteAddr and AddrRepresentation

* feat: Hash for AnyAddr

Co-authored-by: wenig <info@pwenig.de>
  • Loading branch information
wenig and wenig authored Apr 13, 2021
1 parent dbf3750 commit 37635ce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/remote/addr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::hash::Hash;
use std::hash::{Hash, Hasher};
use std::net::SocketAddr;
use std::str::FromStr;

Expand Down Expand Up @@ -69,7 +69,7 @@ impl Clone for RemoteAddr {

impl PartialEq for RemoteAddr {
fn eq(&self, other: &Self) -> bool {
self.socket_addr.eq(&other.socket_addr)
self.socket_addr.eq(&other.socket_addr) && self.id.eq(&other.id)
}
}

Expand Down Expand Up @@ -106,3 +106,30 @@ impl<T: Actor> Clone for AnyAddr<T> {
}
}
}


impl<T: Actor> PartialEq for AnyAddr<T> {
fn eq(&self, other: &Self) -> bool {
match self {
AnyAddr::Local(addr) => match other {
AnyAddr::Local(other_addr) => addr.eq(other_addr),
AnyAddr::Remote(_) => false
},
AnyAddr::Remote(addr) => match other {
AnyAddr::Local(_) => false,
AnyAddr::Remote(other_addr) => addr.eq(other_addr)
}
}
}
}

impl<T: Actor> Eq for AnyAddr<T> {}

impl<T: Actor> Hash for AnyAddr<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
AnyAddr::Local(addr) => addr.hash(state),
AnyAddr::Remote(addr) => addr.hash(state)
}
}
}
16 changes: 16 additions & 0 deletions src/remote/addr/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ impl Clone for AddrRepresentation {
}
}

impl PartialEq for AddrRepresentation {
fn eq(&self, other: &Self) -> bool {
let self_key = self.to_string();
(self_key == other.to_string() && self_key != "Key")
|| (self_key == "Key" && match self {
AddrRepresentation::Key(key) => match other {
AddrRepresentation::Key(other_key) => key == other_key,
_ => false
} ,
_ => false
})
}
}

impl Eq for AddrRepresentation {}


#[derive(Message)]
#[rtype(result = "Result<AddrResponse, ()>")]
Expand Down
24 changes: 23 additions & 1 deletion src/remote/addr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use actix::prelude::*;
use serde::{Serialize, Deserialize};
use crate::prelude::*;
use actix_telepathy_derive::{RemoteActor, RemoteMessage};
use crate::{AddrResolver, AddrRequest, AddrResponse};
use crate::{AddrResolver, AddrRequest, AddrResponse, AddrRepresentation};
use tokio::time::delay_for;
use std::time::Duration;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -56,3 +56,25 @@ async fn addr_resolver_registers_and_resolves_addr() {
delay_for(Duration::from_secs(1)).await;
assert_eq!((*(identifiers.lock().unwrap())).get(0).unwrap(), &identifier);
}

#[test]
fn addr_representation_eq_not_key() {
let own = AddrRepresentation::NetworkInterface;
let other1 = AddrRepresentation::NetworkInterface;
let other2 = AddrRepresentation::Gossip;
let other3 = AddrRepresentation::Key("test".to_string());

assert!(own.eq(&other1));
assert!(own.ne(&other2));
assert!(own.ne(&other3));
}

#[test]
fn addr_representation_eq_key() {
let own = AddrRepresentation::Key("own".to_string());
let other1 = AddrRepresentation::Key("own".to_string());
let other2 = AddrRepresentation::Key("other2".to_string());

assert!(own.eq(&other1));
assert!(own.ne(&other2));
}

0 comments on commit 37635ce

Please sign in to comment.