loro_common/
internal_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::{fmt::Display, ops::Deref};

use serde::{Deserialize, Serialize};

#[repr(transparent)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct InternalString(string_cache::DefaultAtom);
impl InternalString {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<T: Into<string_cache::DefaultAtom>> From<T> for InternalString {
    #[inline(always)]
    fn from(value: T) -> Self {
        Self(value.into())
    }
}

impl From<&InternalString> for String {
    #[inline(always)]
    fn from(value: &InternalString) -> Self {
        value.0.to_string()
    }
}

impl From<&InternalString> for InternalString {
    #[inline(always)]
    fn from(value: &InternalString) -> Self {
        value.clone()
    }
}

impl Display for InternalString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Deref for InternalString {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}