topcoat_runtime/surrogate/
_str.rs1use ref_cast::RefCast;
2
3use crate::{
4 BoolSurrogate, F64Surrogate, StringSurrogate, impl_surrogate_mut, impl_surrogate_ref,
5 serialize_tagged,
6};
7
8#[derive(Debug, RefCast)]
9#[repr(transparent)]
10pub struct StrSurrogate(pub(super) str);
11
12impl_surrogate_ref!(str, StrSurrogate);
13impl_surrogate_mut!(str, StrSurrogate);
14
15impl serde::Serialize for StrSurrogate {
16 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
17 where
18 S: serde::Serializer,
19 {
20 serialize_tagged(serializer, "str", &self.0)
21 }
22}
23
24impl std::fmt::Display for StrSurrogate {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 self.0.fmt(f)
27 }
28}
29
30macro_rules! impl_cmp_op {
31 ($method:ident, $op:tt) => {
32 impl StrSurrogate {
33 #[inline]
34 pub fn $method(&self, rhs: &StrSurrogate) -> BoolSurrogate {
35 BoolSurrogate::new(self.0 $op rhs.0)
36 }
37 }
38 };
39}
40
41impl_cmp_op!(eq, ==);
42impl_cmp_op!(ne, !=);
43impl_cmp_op!(gt, >);
44impl_cmp_op!(lt, <);
45impl_cmp_op!(ge, >=);
46impl_cmp_op!(le, <=);
47
48impl StrSurrogate {
49 #[inline]
50 #[must_use]
51 pub fn to_owned(&self) -> StringSurrogate {
52 StringSurrogate::new(self.0.to_owned())
53 }
54
55 #[inline]
56 #[must_use]
57 pub fn is_empty(&self) -> BoolSurrogate {
58 BoolSurrogate::new(self.0.is_empty())
59 }
60
61 #[inline]
62 #[must_use]
63 #[allow(clippy::cast_precision_loss)]
64 pub fn len(&self) -> F64Surrogate {
65 F64Surrogate::new(self.0.len() as f64)
66 }
67
68 #[inline]
69 #[must_use]
70 pub fn trim(&self) -> &StrSurrogate {
71 StrSurrogate::ref_cast(self.0.trim())
72 }
73
74 #[inline]
75 #[must_use]
76 pub fn trim_start(&self) -> &StrSurrogate {
77 StrSurrogate::ref_cast(self.0.trim_start())
78 }
79
80 #[inline]
81 #[must_use]
82 pub fn trim_end(&self) -> &StrSurrogate {
83 StrSurrogate::ref_cast(self.0.trim_end())
84 }
85
86 #[inline]
87 #[must_use]
88 pub fn starts_with(&self, other: &StrSurrogate) -> BoolSurrogate {
89 BoolSurrogate::new(self.0.starts_with(&other.0))
90 }
91
92 #[inline]
93 #[must_use]
94 pub fn ends_with(&self, other: &StrSurrogate) -> BoolSurrogate {
95 BoolSurrogate::new(self.0.ends_with(&other.0))
96 }
97
98 #[inline]
99 #[must_use]
100 pub fn contains(&self, other: &StrSurrogate) -> BoolSurrogate {
101 BoolSurrogate::new(self.0.contains(&other.0))
102 }
103}