use std::borrow::Cow;
#[derive(Debug)]
pub struct Message(Cow<'static, str>);
impl From<&'static str> for Message {
fn from(s: &'static str) -> Self {
Message(Cow::Borrowed(s))
}
}
impl From<String> for Message {
fn from(s: String) -> Self {
Message(Cow::Owned(s))
}
}
impl Message {
pub fn as_str(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_str_and_string_preserve_text() {
let test_cases: Vec<(Message, &str)> = vec![
("hello".into(), "hello"),
("world".to_string().into(), "world"),
];
for (message, expected) in test_cases {
assert_eq!(message.as_str(), expected);
}
}
}