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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use core::cmp;
use core::fmt;
use core::hash;
use core::ops;

use serde::{Deserialize, Serialize};

use crate::alloc::prelude::*;
use crate::alloc::{self, String};
use crate::hash::{Hash, IntoHash};

/// Struct representing a static string.
#[derive(Serialize, Deserialize)]
pub struct StaticString {
    inner: String,
    hash: Hash,
}

impl cmp::PartialEq for StaticString {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash && self.inner == other.inner
    }
}

impl cmp::Eq for StaticString {}

impl cmp::PartialOrd for StaticString {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl cmp::Ord for StaticString {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl hash::Hash for StaticString {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.hash.hash(state)
    }
}

impl StaticString {
    /// Construct a new static string.
    pub fn new<S>(s: S) -> alloc::Result<Self>
    where
        S: AsRef<str>,
    {
        let inner = s.as_ref().try_to_owned()?;
        let hash = s.as_ref().into_hash();
        Ok(Self { inner, hash })
    }

    /// Get the hash of the string.
    #[inline]
    pub fn hash(&self) -> Hash {
        self.hash
    }
}

impl AsRef<String> for StaticString {
    #[inline]
    fn as_ref(&self) -> &String {
        &self.inner
    }
}

impl fmt::Debug for StaticString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", &self.inner)?;
        Ok(())
    }
}

impl ops::Deref for StaticString {
    type Target = String;

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

impl From<String> for StaticString {
    fn from(inner: String) -> Self {
        let hash = inner.as_str().into_hash();
        Self { inner, hash }
    }
}