simd_json_derive/impls/
string.rs

1use crate::{de, BaseGenerator, Deserialize, DummyGenerator, Result, Serialize, Tape, Write};
2
3impl Serialize for String {
4    #[inline]
5    fn json_write<W>(&self, writer: &mut W) -> Result
6    where
7        W: Write,
8    {
9        DummyGenerator(writer).write_string(self)
10    }
11}
12
13impl<'input> Deserialize<'input> for String {
14    #[inline]
15    fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
16    where
17        Self: Sized + 'input,
18    {
19        match tape.next() {
20            Some(simd_json::Node::String(s)) => Ok(String::from(s)),
21            _ => Err(de::Error::expected_string()),
22        }
23    }
24}
25
26impl<'input> Deserialize<'input> for &'input str {
27    #[inline]
28    fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
29    where
30        Self: Sized + 'input,
31    {
32        match tape.next() {
33            Some(simd_json::Node::String(s)) => Ok(s),
34            _ => Err(de::Error::expected_string()),
35        }
36    }
37}
38
39impl Serialize for str {
40    #[inline]
41    fn json_write<W>(&self, writer: &mut W) -> Result
42    where
43        W: Write,
44    {
45        DummyGenerator(writer).write_string(self)
46    }
47}
48
49// "Figure this out". <-- PS. you cant as no one manages str's memory,
50// and its also dynamically sized so you cant really allocate it on the stack risking overflow
51// even if you should dynamically allocate on the stack which rust can't
52// you could do a Box<str> though
53//
54// impl<'input> Deserialize<'input> for str {
55//     #[inline]
56//     fn from_tape(
57//         tape: &mut Tape<'input>,
58//     ) -> simd_json::Result<Self>
59//     where
60//         Self: std::marker::Sized + 'input,
61//     {
62//         match tape.next() {
63//             Some(simd_json::Node::String(s)) => Ok(s),
64//             _ => Err(de::Error::simd(
65//                 de::Error::expected_string(),
66//             )),
67//         }
68//     }
69// }
70
71impl<'input> Deserialize<'input> for Box<str> {
72    fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
73    where
74        Self: Sized + 'input,
75    {
76        match tape.next() {
77            Some(simd_json::Node::String(s)) => Ok(Box::from(s)),
78            _ => Err(de::Error::expected_string()),
79        }
80    }
81}