Skip to main content

topcoat_runtime/surrogate/
string.rs

1use ref_cast::RefCast;
2use serde::{Deserialize, Serialize};
3
4use crate::{StrSurrogate, impl_surrogate, impl_surrogate_mut, impl_surrogate_ref};
5
6#[derive(Debug, Clone, RefCast, Serialize, Deserialize)]
7#[repr(transparent)]
8#[serde(transparent)]
9pub struct StringSurrogate(String);
10
11impl StringSurrogate {
12    #[inline]
13    pub(crate) const fn new(v: String) -> Self {
14        Self(v)
15    }
16}
17
18impl_surrogate!(String, StringSurrogate);
19impl_surrogate_ref!(String, StringSurrogate);
20impl_surrogate_mut!(String, StringSurrogate);
21
22impl std::fmt::Display for StringSurrogate {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        self.0.fmt(f)
25    }
26}
27
28impl std::ops::Deref for StringSurrogate {
29    type Target = StrSurrogate;
30
31    #[inline]
32    fn deref(&self) -> &StrSurrogate {
33        StrSurrogate::ref_cast(self.0.as_str())
34    }
35}