-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathlib.rs
More file actions
93 lines (84 loc) · 2.22 KB
/
lib.rs
File metadata and controls
93 lines (84 loc) · 2.22 KB
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
// Using serde_json as the JSON handler
#[cfg(not(feature = "simd"))]
pub use serde::*;
// Using simd_json as the JSON handler
#[cfg(feature = "simd")]
pub use simd::*;
// Implementations
#[cfg(not(feature = "simd"))]
mod serde {
use bytes::Bytes;
use serde::de::DeserializeOwned;
pub use serde_json::{
self, error::Error as JsonError, from_reader, from_slice, from_str, from_value, json, to_string,
to_string_pretty, to_value, to_writer, value::RawValue, Deserializer as JsonDeserializer, Value,
to_vec,
};
pub fn from_bytes<T>(b: Bytes) -> serde_json::Result<T>
where
T: DeserializeOwned,
{
from_slice(&b)
}
pub fn from_string<T>(s: String) -> serde_json::Result<T>
where
T: DeserializeOwned,
{
from_str(s.as_str())
}
pub fn from_vec<T>(v: Vec<u8>) -> serde_json::Result<T>
where
T: DeserializeOwned,
{
from_slice(&v)
}
}
#[cfg(feature = "simd")]
mod simd {
use bytes::Bytes;
use serde::de::DeserializeOwned;
pub use simd_json::{
self,
json,
owned::Value,
serde::{
from_owned_value as from_value,
from_reader,
from_str, //THIS requires a mutable string slice AND is unsafe
from_slice, //THIS requires a mutable slice!
to_owned_value as to_value,
to_string,
to_string_pretty,
to_writer,
to_vec,
},
tape::Value as RawValue, //THIS is gonna be the fun one!
Deserializer as JsonDeserializer,
Error as JsonError,
};
pub use value_trait::prelude::*;
pub fn from_bytes<T>(b: Bytes) -> simd_json::Result<T>
where
T: DeserializeOwned,
{
match b.try_into_mut() {
Ok(mut b) => from_slice(&mut b),
Err(b) => {
let mut v = b.to_vec();
from_slice(&mut v)
}
}
}
pub fn from_string<T>(mut s: String) -> simd_json::Result<T>
where
T: DeserializeOwned,
{
unsafe{ from_str(s.as_mut_str()) }
}
pub fn from_vec<T>(mut v: Vec<u8>) -> simd_json::Result<T>
where
T: DeserializeOwned,
{
from_slice(&mut v)
}
}