hyperlit_base/
shared_string.rs

1use arcstr::ArcStr;
2use std::fmt::{Debug, Display};
3use std::ops::Deref;
4
5/// A shared, immutable threadsafe string
6#[derive(Clone, Eq, Hash, PartialEq)]
7pub struct SharedString {
8    string: ArcStr,
9}
10
11impl SharedString {
12    pub fn new(string: String) -> Self {
13        Self {
14            string: ArcStr::from(string),
15        }
16    }
17}
18
19impl AsRef<[u8]> for SharedString {
20    fn as_ref(&self) -> &[u8] {
21        self.string.as_bytes()
22    }
23}
24
25impl AsRef<str> for SharedString {
26    fn as_ref(&self) -> &str {
27        self.string.as_str()
28    }
29}
30
31impl Deref for SharedString {
32    type Target = str;
33    fn deref(&self) -> &str {
34        self.string.as_str()
35    }
36}
37
38impl From<String> for SharedString {
39    fn from(string: String) -> Self {
40        Self {
41            string: ArcStr::from(string),
42        }
43    }
44}
45
46impl From<&str> for SharedString {
47    fn from(string: &str) -> Self {
48        Self::from(string.to_string())
49    }
50}
51
52impl From<&SharedString> for SharedString {
53    fn from(string: &SharedString) -> Self {
54        string.clone()
55    }
56}
57
58impl PartialEq<&str> for SharedString {
59    fn eq(&self, other: &&str) -> bool {
60        &self.string.as_str() == other
61    }
62}
63
64impl Debug for SharedString {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        std::fmt::Debug::fmt(&self.string, f)
67    }
68}
69
70impl Display for SharedString {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        std::fmt::Display::fmt(&self.string, f)
73    }
74}