|
| 1 | +pub enum StringRef { |
| 2 | + Owned(String), |
| 3 | + Static(&'static str), |
| 4 | +} |
| 5 | + |
| 6 | +impl StringRef { |
| 7 | + pub fn as_str(&self) -> &str { |
| 8 | + match self { |
| 9 | + StringRef::Owned(s) => s.as_str(), |
| 10 | + StringRef::Static(s) => s, |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +impl From<String> for StringRef { |
| 16 | + fn from(s: String) -> Self { |
| 17 | + StringRef::Owned(s) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl From<&'static str> for StringRef { |
| 22 | + fn from(s: &'static str) -> Self { |
| 23 | + StringRef::Static(s) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl From<bluer::Error> for StringRef { |
| 28 | + fn from(err: bluer::Error) -> Self { |
| 29 | + StringRef::Owned(err.to_string()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl From<anyhow::Error> for StringRef { |
| 34 | + fn from(err: anyhow::Error) -> Self { |
| 35 | + StringRef::Owned(err.to_string()) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl AsRef<str> for StringRef { |
| 40 | + fn as_ref(&self) -> &str { |
| 41 | + self.as_str() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl std::fmt::Display for StringRef { |
| 46 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 47 | + write!(f, "{}", self.as_str()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl std::fmt::Debug for StringRef { |
| 52 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 53 | + match self { |
| 54 | + StringRef::Owned(s) => write!(f, "StringRef::Owned({:?})", s), |
| 55 | + StringRef::Static(s) => write!(f, "StringRef::Static({:?})", s), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl Clone for StringRef { |
| 61 | + fn clone(&self) -> Self { |
| 62 | + match self { |
| 63 | + StringRef::Owned(s) => StringRef::Owned(s.clone()), |
| 64 | + StringRef::Static(s) => StringRef::Static(s), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments