Skip to content

Commit e31d65a

Browse files
committed
rustfmt
1 parent 246c074 commit e31d65a

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

src/decode.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

3-
use std::os::raw::c_char;
3+
use crate::typeref;
44
use pyo3::prelude::*;
55
use pyo3::types::*;
66
use pyo3::IntoPyPointer;
@@ -9,7 +9,7 @@ use smallvec::SmallVec;
99
use std::borrow::Cow;
1010
use std::fmt;
1111
use std::marker::PhantomData;
12-
use crate::typeref;
12+
use std::os::raw::c_char;
1313

1414
import_exception!(json, JSONDecodeError);
1515

@@ -67,18 +67,14 @@ impl<'de, 'a> Visitor<'de> for JsonValue<'a> {
6767
E: de::Error,
6868
{
6969
match value {
70-
true => {
71-
unsafe {
72-
pyo3::ffi::Py_INCREF(typeref::TRUE);
73-
Ok(typeref::TRUE)
74-
}
70+
true => unsafe {
71+
pyo3::ffi::Py_INCREF(typeref::TRUE);
72+
Ok(typeref::TRUE)
73+
},
74+
false => unsafe {
75+
pyo3::ffi::Py_INCREF(typeref::FALSE);
76+
Ok(typeref::FALSE)
7577
},
76-
false => {
77-
unsafe {
78-
pyo3::ffi::Py_INCREF(typeref::FALSE);
79-
Ok(typeref::FALSE)
80-
}
81-
}
8278
}
8379
}
8480

@@ -107,15 +103,24 @@ impl<'de, 'a> Visitor<'de> for JsonValue<'a> {
107103
where
108104
E: de::Error,
109105
{
110-
111-
Ok(unsafe { pyo3::ffi::PyUnicode_FromStringAndSize(value.as_ptr() as *const c_char, value.len() as pyo3::ffi::Py_ssize_t) })
106+
Ok(unsafe {
107+
pyo3::ffi::PyUnicode_FromStringAndSize(
108+
value.as_ptr() as *const c_char,
109+
value.len() as pyo3::ffi::Py_ssize_t,
110+
)
111+
})
112112
}
113113

114114
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
115115
where
116116
E: de::Error,
117117
{
118-
Ok(unsafe { pyo3::ffi::PyUnicode_FromStringAndSize(value.as_ptr() as *const c_char, value.len() as pyo3::ffi::Py_ssize_t) })
118+
Ok(unsafe {
119+
pyo3::ffi::PyUnicode_FromStringAndSize(
120+
value.as_ptr() as *const c_char,
121+
value.len() as pyo3::ffi::Py_ssize_t,
122+
)
123+
})
119124
}
120125

121126
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
@@ -139,11 +144,16 @@ impl<'de, 'a> Visitor<'de> for JsonValue<'a> {
139144
{
140145
let dict_ptr = PyDict::new(self.py).into_ptr();
141146
while let Some((key, value)) = map.next_entry_seed(PhantomData::<Cow<str>>, self)? {
142-
let _ = unsafe { pyo3::ffi::PyDict_SetItem(
143-
dict_ptr,
144-
pyo3::ffi::PyUnicode_FromStringAndSize(key.as_ptr() as *const c_char, key.len() as pyo3::ffi::Py_ssize_t),
145-
value,
146-
) };
147+
let _ = unsafe {
148+
pyo3::ffi::PyDict_SetItem(
149+
dict_ptr,
150+
pyo3::ffi::PyUnicode_FromStringAndSize(
151+
key.as_ptr() as *const c_char,
152+
key.len() as pyo3::ffi::Py_ssize_t,
153+
),
154+
value,
155+
)
156+
};
147157
}
148158
Ok(dict_ptr)
149159
}

src/encode.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ impl<'p, 'a> Serialize for SerializePyObject<'p, 'a> {
3636
} else if unsafe { obj_ptr == INT_PTR } {
3737
let val = unsafe { pyo3::ffi::PyLong_AsLong(self.obj.as_ptr()) };
3838
if unsafe { std::intrinsics::unlikely(val == -1 && PyErr::occurred(self.py)) } {
39-
return Err(ser::Error::custom(
40-
"Integer exceeds 64-bit max"
41-
))
39+
return Err(ser::Error::custom("Integer exceeds 64-bit max"));
4240
}
4341
serializer.serialize_i64(val)
4442
} else if unsafe { obj_ptr == BOOL_PTR } {
@@ -53,9 +51,7 @@ impl<'p, 'a> Serialize for SerializePyObject<'p, 'a> {
5351
let mut map = serializer.serialize_map(Some(len))?;
5452
for (key, value) in val.iter() {
5553
if unsafe { std::intrinsics::unlikely(key.get_type_ptr() != STR_PTR) } {
56-
return Err(ser::Error::custom(
57-
"Dict key must be str"
58-
));
54+
return Err(ser::Error::custom("Dict key must be str"));
5955
}
6056
map.serialize_entry(
6157
unsafe {

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ pub fn loads(py: Python, obj: PyObject) -> PyResult<PyObject> {
3838
let obj_ptr = obj_ref.get_type_ptr();
3939
let val: Cow<str>;
4040
if unsafe { obj_ptr == typeref::STR_PTR } {
41-
val = unsafe { Cow::Borrowed(std::str::from_utf8_unchecked(
42-
<PyUnicode as PyTryFrom>::try_from_unchecked(obj_ref).as_bytes()
43-
)) };
41+
val = unsafe {
42+
Cow::Borrowed(std::str::from_utf8_unchecked(
43+
<PyUnicode as PyTryFrom>::try_from_unchecked(obj_ref).as_bytes(),
44+
))
45+
};
4446
} else if unsafe { obj_ptr == typeref::BYTES_PTR } {
45-
val = String::from_utf8_lossy(
46-
unsafe { <PyBytes as PyTryFrom>::try_from_unchecked(obj_ref).as_bytes() }
47-
);
47+
val = String::from_utf8_lossy(unsafe {
48+
<PyBytes as PyTryFrom>::try_from_unchecked(obj_ref).as_bytes()
49+
});
4850
} else {
4951
return Err(pyo3::exceptions::TypeError::py_err(format!(
5052
"Input must be str or bytes, not: {}",

0 commit comments

Comments
 (0)