Skip to main content

librashader_common/
string.rs

1use std::borrow::Borrow;
2use std::cmp::Ordering;
3use std::fmt::{Display, Formatter};
4use std::ops::Deref;
5use strumbra::SharedString;
6
7#[repr(transparent)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash)]
10pub struct ParamString(SharedString);
11
12impl Deref for ParamString {
13    type Target = str;
14
15    #[inline]
16    fn deref(&self) -> &Self::Target {
17        self.0.as_ref()
18    }
19}
20
21impl From<&str> for ParamString {
22    #[inline]
23    fn from(s: &str) -> Self {
24        ParamString(
25            SharedString::try_from(s)
26                .expect("ParamString with more than 4294967295 characters. Parameter too large."),
27        )
28    }
29}
30
31impl From<&String> for ParamString {
32    #[inline]
33    fn from(s: &String) -> Self {
34        ParamString(
35            SharedString::try_from(s)
36                .expect("ParamString with more than 4294967295 characters. Parameter too large."),
37        )
38    }
39}
40
41impl From<String> for ParamString {
42    #[inline]
43    fn from(s: String) -> Self {
44        ParamString(
45            SharedString::try_from(s)
46                .expect("ParamString with more than 4294967295 characters. Parameter too large."),
47        )
48    }
49}
50
51impl Display for ParamString {
52    #[inline]
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        self.0.fmt(f)
55    }
56}
57
58impl AsRef<str> for ParamString {
59    #[inline]
60    fn as_ref(&self) -> &str {
61        self.0.as_ref()
62    }
63}
64
65impl Borrow<str> for ParamString {
66    fn borrow(&self) -> &str {
67        self.0.as_ref()
68    }
69}
70
71impl PartialEq<&str> for ParamString {
72    fn eq(&self, other: &&str) -> bool {
73        self.0.as_str() == *other
74    }
75}
76
77impl PartialEq<str> for ParamString {
78    fn eq(&self, other: &str) -> bool {
79        self.0.as_str() == other
80    }
81}
82
83impl PartialEq<String> for ParamString {
84    fn eq(&self, other: &String) -> bool {
85        self.0.as_str() == other
86    }
87}
88
89impl PartialEq<ParamString> for &str {
90    fn eq(&self, other: &ParamString) -> bool {
91        *self == other.as_ref()
92    }
93}
94
95impl PartialEq<ParamString> for String {
96    fn eq(&self, other: &ParamString) -> bool {
97        *self == other.as_ref()
98    }
99}
100
101impl PartialEq<ParamString> for &String {
102    fn eq(&self, other: &ParamString) -> bool {
103        *self == other.as_ref()
104    }
105}
106
107impl Eq for ParamString {}
108
109impl PartialOrd<String> for ParamString {
110    fn partial_cmp(&self, other: &String) -> Option<Ordering> {
111        self.0.partial_cmp(other)
112    }
113}
114
115impl PartialOrd<str> for ParamString {
116    fn partial_cmp(&self, other: &str) -> Option<Ordering> {
117        self.0.partial_cmp(other)
118    }
119}
120
121impl PartialOrd<&str> for ParamString {
122    fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
123        self.0.partial_cmp(*other)
124    }
125}
126
127impl ParamString {
128    /// Pushes a new string onto the `ParamString`.
129    ///
130    /// This could incur up to two allocations.
131    pub fn push_str(&mut self, s: &str) {
132        let new = format!("{self}{s}");
133        self.0 =
134            SharedString::try_from(new).expect("ParamString with more than 4294967295 characters.");
135    }
136
137    // Extracts a string slice containing the entire `ParamString`.
138    pub fn as_str(&self) -> &str {
139        self.0.as_str()
140    }
141}
142
143#[cfg(test)]
144mod test {
145    use super::*;
146    #[test]
147    pub fn test_push_str() {
148        let mut string = ParamString::from("Hello");
149        string.push_str(" World");
150        assert_eq!("Hello World", string);
151    }
152}