Skip to main content

actionqueue_core/ids/
actor_id.rs

1//! Unique identifier for a remote actor registered with the hub.
2
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6use uuid::Uuid;
7
8/// A unique identifier for a remote actor registration.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct ActorId(Uuid);
12
13impl ActorId {
14    /// Creates a new random ActorId.
15    pub fn new() -> Self {
16        ActorId(Uuid::new_v4())
17    }
18
19    /// Creates an ActorId from a UUID.
20    pub fn from_uuid(uuid: Uuid) -> Self {
21        ActorId(uuid)
22    }
23
24    /// Returns the inner UUID.
25    pub fn as_uuid(&self) -> &Uuid {
26        &self.0
27    }
28}
29
30impl Default for ActorId {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36impl FromStr for ActorId {
37    type Err = uuid::Error;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        Uuid::from_str(s).map(ActorId)
41    }
42}
43
44impl Display for ActorId {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}", self.0)
47    }
48}