forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.rs
More file actions
55 lines (47 loc) · 1.26 KB
/
Copy pathstr.rs
File metadata and controls
55 lines (47 loc) · 1.26 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
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
use crate::serialize::error::*;
use crate::unicode::*;
use serde::ser::{Serialize, Serializer};
#[repr(transparent)]
pub struct StrSerializer {
ptr: *mut pyo3_ffi::PyObject,
}
impl StrSerializer {
pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self {
StrSerializer { ptr: ptr }
}
}
impl Serialize for StrSerializer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let uni = unicode_to_str(self.ptr);
if unlikely!(uni.is_none()) {
err!(SerializeError::InvalidStr)
}
serializer.serialize_str(uni.unwrap())
}
}
#[repr(transparent)]
pub struct StrSubclassSerializer {
ptr: *mut pyo3_ffi::PyObject,
}
impl StrSubclassSerializer {
pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self {
StrSubclassSerializer { ptr: ptr }
}
}
impl Serialize for StrSubclassSerializer {
#[inline(never)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let uni = unicode_to_str_via_ffi(self.ptr);
if unlikely!(uni.is_none()) {
err!(SerializeError::InvalidStr)
}
serializer.serialize_str(uni.unwrap())
}
}