Compare commits

...

2 Commits

Author SHA1 Message Date
Valeryi Savich 66b2325e8a Implemented Eq and Hash traits for definitions 2021-07-08 00:39:23 +02:00
Valeryi Savich d365ce0f7e Added custom name for a definition 2021-07-07 23:59:41 +02:00
2 changed files with 37 additions and 1 deletions

View File

@ -1,6 +1,9 @@
use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use uuid::Uuid;
use crate::actor::traits::Actor;
use crate::routing::path::{ActorPath, Scope};
@ -8,6 +11,8 @@ type CustomActorNameFn = dyn Fn() -> String + Send + 'static;
/// A structure that holds configuration of the Bastion actor.
pub struct Definition {
/// A unique definition name;
name: String,
/// A struct that implements actor's behaviour
actor: Option<Arc<dyn Actor>>,
/// Defines a used scope for instantiating actors.
@ -21,12 +26,14 @@ pub struct Definition {
impl Definition {
/// Returns a new instance of the actor's definition.
pub fn new() -> Self {
let name = Uuid::new_v4().to_string();
let scope = Scope::User;
let actor_name_fn = None;
let redundancy = 1;
let actor = None;
Definition {
name,
actor,
scope,
actor_name_fn,
@ -34,6 +41,12 @@ impl Definition {
}
}
/// Overrides the definition's name. The passed value must be unique.
pub fn name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
/// Sets the actor to schedule.
pub fn actor<T: 'static>(mut self, actor: T) -> Self
where
@ -89,6 +102,19 @@ impl Debug for Definition {
}
}
impl Eq for Definition {}
impl PartialEq for Definition {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Hash for Definition {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
#[cfg(test)]
mod tests {
use crate::actor::definition::Definition;
@ -108,6 +134,16 @@ mod tests {
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_name() {
let instance = Definition::new().name("test_name");
assert_eq!(instance.name, "test_name");
assert_eq!(instance.scope, Scope::User);
assert_eq!(instance.actor_name_fn.is_some(), false);
assert_eq!(instance.redundancy, 1);
}
#[test]
fn test_definition_with_custom_actor_name() {
let instance = Definition::new().custom_actor_name(fake_actor_name);

View File

@ -72,7 +72,7 @@ where
}
#[cfg(test)]
mod message_target_tests {
mod envelope_tests {
use crate::mailbox::envelope::Envelope;
use crate::mailbox::message::{Message, MessageType};