taktora_executor/
task_id.rs1use core::fmt;
4use std::sync::Arc;
5
6#[derive(Clone, Debug, Eq, PartialEq, Hash)]
11pub struct TaskId(Arc<str>);
12
13impl TaskId {
14 pub fn new(s: impl Into<String>) -> Self {
16 Self(Arc::from(s.into()))
17 }
18
19 #[must_use]
21 pub fn as_str(&self) -> &str {
22 &self.0
23 }
24}
25
26impl fmt::Display for TaskId {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 f.write_str(&self.0)
29 }
30}
31
32impl From<String> for TaskId {
33 fn from(s: String) -> Self {
34 Self::new(s)
35 }
36}
37
38impl From<&str> for TaskId {
39 fn from(s: &str) -> Self {
40 Self(Arc::from(s))
41 }
42}
43
44impl From<&String> for TaskId {
45 fn from(s: &String) -> Self {
46 Self(Arc::from(s.as_str()))
47 }
48}
49
50impl AsRef<str> for TaskId {
51 fn as_ref(&self) -> &str {
52 self.as_str()
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn round_trip_display() {
62 let id = TaskId::new("hello");
63 assert_eq!(id.to_string(), "hello");
64 assert_eq!(id.as_str(), "hello");
65 }
66
67 #[test]
68 fn clone_yields_equal_id() {
69 let a = TaskId::new("x");
70 let b = a.clone();
71 assert_eq!(a, b);
72 }
73
74 #[test]
75 fn from_various_string_kinds() {
76 let lit: TaskId = "lit".into();
77 assert_eq!(lit.as_str(), "lit");
78
79 let owned: TaskId = String::from("owned").into();
80 assert_eq!(owned.as_str(), "owned");
81
82 let referenced_owned = String::from("ref");
83 let from_ref: TaskId = (&referenced_owned).into();
84 assert_eq!(from_ref.as_str(), "ref");
85 }
86}