topcoat_runtime/surrogate/
_str.rs1use ref_cast::RefCast;
2
3use crate::{
4 BoolSurrogate, StringSurrogate, UsizeSurrogate, 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 pub fn len(&self) -> UsizeSurrogate {
64 UsizeSurrogate::new(self.0.len())
65 }
66
67 #[inline]
68 #[must_use]
69 pub fn trim(&self) -> &StrSurrogate {
70 StrSurrogate::ref_cast(self.0.trim())
71 }
72
73 #[inline]
74 #[must_use]
75 pub fn trim_start(&self) -> &StrSurrogate {
76 StrSurrogate::ref_cast(self.0.trim_start())
77 }
78
79 #[inline]
80 #[must_use]
81 pub fn trim_end(&self) -> &StrSurrogate {
82 StrSurrogate::ref_cast(self.0.trim_end())
83 }
84
85 #[inline]
86 #[must_use]
87 pub fn starts_with(&self, other: &StrSurrogate) -> BoolSurrogate {
88 BoolSurrogate::new(self.0.starts_with(&other.0))
89 }
90
91 #[inline]
92 #[must_use]
93 pub fn ends_with(&self, other: &StrSurrogate) -> BoolSurrogate {
94 BoolSurrogate::new(self.0.ends_with(&other.0))
95 }
96
97 #[inline]
98 #[must_use]
99 pub fn contains(&self, other: &StrSurrogate) -> BoolSurrogate {
100 BoolSurrogate::new(self.0.contains(&other.0))
101 }
102}