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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use alloc::{borrow::ToOwned, string::String};
use core::{fmt, marker::PhantomData};

use serde::de::{self, Deserialize, Deserializer, Visitor};
use serde::ser::{Serialize, Serializer};

use crate::generic::Cow;
use crate::traits::internal::{Beef, Capacity};

impl<T, U> Serialize for Cow<'_, T, U>
where
    T: Beef + Serialize + ?Sized,
    U: Capacity,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        T::serialize(self.as_ref(), serializer)
    }
}

struct CowVisitor<'de, 'a, T: Beef + ?Sized, U: Capacity>(
    PhantomData<fn() -> (&'de T, Cow<'a, T, U>)>,
);

impl<'de, 'a, U> Visitor<'de> for CowVisitor<'de, 'a, str, U>
where
    'de: 'a,
    U: Capacity,
{
    type Value = Cow<'a, str, U>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string")
    }

    fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Cow::borrowed(value))
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Cow::owned(value.to_owned()))
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Cow::owned(value))
    }
}

impl<'de, 'a, U> Deserialize<'de> for Cow<'a, str, U>
where
    'de: 'a,
    U: Capacity,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(CowVisitor::<'de, 'a, str, U>(PhantomData))
    }
}

impl<'de, 'a, T, U> Deserialize<'de> for Cow<'a, [T], U>
where
    [T]: Beef,
    U: Capacity,
    <[T] as ToOwned>::Owned: Deserialize<'de>,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        <[T] as ToOwned>::Owned::deserialize(deserializer).map(Cow::owned)
    }
}

#[cfg(test)]
mod tests {
    use serde_derive::{Deserialize, Serialize};

    #[test]
    fn wide_cow_de() {
        use crate::Cow;

        #[derive(Serialize, Deserialize)]
        struct Test<'a> {
            #[serde(borrow)]
            foo: Cow<'a, str>,
            bar: Cow<'a, str>,
        }

        let json = r#"{"foo":"Hello","bar":"\tWorld!"}"#;
        let test: Test = serde_json::from_str(json).unwrap();

        assert_eq!(test.foo, "Hello");
        assert_eq!(test.bar, "\tWorld!");

        assert!(test.foo.is_borrowed());
        assert!(test.bar.is_owned());

        let out = serde_json::to_string(&test).unwrap();

        assert_eq!(json, out);
    }

    #[test]
    fn wide_cow_direct() {
        use crate::Cow;

        let json = r#""foo""#;
        let cow: Cow<str> = serde_json::from_str(json).unwrap();

        assert_eq!(cow, "foo");

        assert!(cow.is_borrowed());

        let json = r#""\tfoo""#;
        let cow: Cow<str> = serde_json::from_str(json).unwrap();

        assert_eq!(cow, "\tfoo");

        assert!(cow.is_owned());
    }

    #[test]
    fn wide_cow_direct_bytes() {
        use crate::Cow;

        let json = r#"[102, 111, 111]"#;
        let cow: Cow<[u8]> = serde_json::from_str(json).unwrap();

        assert_eq!(cow, &b"foo"[..]);

        // We need to stay generic over `[T]`, so no specialization for byte slices
        assert!(cow.is_owned());
    }
}