|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +/// Cheaply clonable TaskIdentifier |
| 4 | +#[derive(Clone)] |
| 5 | +pub enum TaskIdentifier { |
| 6 | + Literal(&'static str), |
| 7 | + Arc(Arc<String>), |
| 8 | +} |
| 9 | + |
| 10 | +impl From<&'static str> for TaskIdentifier { |
| 11 | + fn from(value: &'static str) -> Self { |
| 12 | + Self::Literal(value) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl From<String> for TaskIdentifier { |
| 17 | + fn from(value: String) -> Self { |
| 18 | + Self::Arc(Arc::new(value)) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl From<Arc<String>> for TaskIdentifier { |
| 23 | + fn from(value: Arc<String>) -> Self { |
| 24 | + Self::Arc(value.clone()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl std::fmt::Display for TaskIdentifier { |
| 29 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 30 | + match self { |
| 31 | + TaskIdentifier::Literal(data) => write!(f, "{data}"), |
| 32 | + TaskIdentifier::Arc(data) => write!(f, "{data}"), |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(test)] |
| 38 | +mod tests { |
| 39 | + use super::*; |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn test_display() { |
| 43 | + let identifier = TaskIdentifier::from("Hello World"); |
| 44 | + assert_eq!(identifier.to_string(), "Hello World"); |
| 45 | + |
| 46 | + let identifier = "Hello World".to_owned(); |
| 47 | + let identifier = TaskIdentifier::from(identifier); |
| 48 | + assert_eq!(identifier.to_string(), "Hello World"); |
| 49 | + |
| 50 | + let identifier = Arc::new("Hello World".to_owned()); |
| 51 | + let identifier = TaskIdentifier::from(identifier); |
| 52 | + assert_eq!(identifier.to_string(), "Hello World"); |
| 53 | + } |
| 54 | +} |
0 commit comments