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
91
92
93
94
95
96
use core::cmp::Ordering;

use rancor::{Fallible, Source};

use crate::{
    alloc::string::{String, ToString},
    string::{ArchivedString, StringResolver},
    Archive, Deserialize, DeserializeUnsized, Place, Serialize,
    SerializeUnsized,
};

impl Archive for String {
    type Archived = ArchivedString;
    type Resolver = StringResolver;

    #[inline]
    fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) {
        ArchivedString::resolve_from_str(self.as_str(), resolver, out);
    }
}

impl<S: Fallible + ?Sized> Serialize<S> for String
where
    S::Error: Source,
    str: SerializeUnsized<S>,
{
    fn serialize(
        &self,
        serializer: &mut S,
    ) -> Result<Self::Resolver, S::Error> {
        ArchivedString::serialize_from_str(self.as_str(), serializer)
    }
}

impl<D: Fallible + ?Sized> Deserialize<String, D> for ArchivedString
where
    str: DeserializeUnsized<str, D>,
{
    fn deserialize(&self, _: &mut D) -> Result<String, D::Error> {
        Ok(self.as_str().to_string())
    }
}

impl PartialEq<String> for ArchivedString {
    #[inline]
    fn eq(&self, other: &String) -> bool {
        PartialEq::eq(self.as_str(), other.as_str())
    }
}

impl PartialEq<ArchivedString> for String {
    #[inline]
    fn eq(&self, other: &ArchivedString) -> bool {
        PartialEq::eq(other.as_str(), self.as_str())
    }
}

impl PartialOrd<String> for ArchivedString {
    #[inline]
    fn partial_cmp(&self, other: &String) -> Option<Ordering> {
        self.as_str().partial_cmp(other.as_str())
    }
}

impl PartialOrd<ArchivedString> for String {
    #[inline]
    fn partial_cmp(&self, other: &ArchivedString) -> Option<Ordering> {
        self.as_str().partial_cmp(other.as_str())
    }
}

#[cfg(test)]
mod tests {
    use crate::{alloc::string::ToString, api::test::roundtrip};

    #[test]
    fn roundtrip_string() {
        roundtrip(&"".to_string());
        roundtrip(&"hello world".to_string());
    }

    #[test]
    fn roundtrip_option_string() {
        roundtrip(&Some("".to_string()));
        roundtrip(&Some("hello world".to_string()));
    }

    #[test]
    fn roundtrip_result_string() {
        roundtrip(&Ok::<_, ()>("".to_string()));
        roundtrip(&Ok::<_, ()>("hello world".to_string()));

        roundtrip(&Err::<(), _>("".to_string()));
        roundtrip(&Err::<(), _>("hello world".to_string()));
    }
}