Skip to content

Commit

Permalink
Trim Result type path
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuele Giaquinta <[email protected]>
  • Loading branch information
exg committed Nov 26, 2024
1 parent 7746bc6 commit b57df8e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 53 deletions.
5 changes: 1 addition & 4 deletions src/deserialize/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ impl<const C: usize> KeyMap<C> {
KeyMap { entries: entries }
}

pub fn get(
&mut self,
key: &[u8],
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Utf8Error> {
pub fn get(&mut self, key: &[u8]) -> Result<NonNull<pyo3::ffi::PyObject>, Utf8Error> {
let mut hasher = unsafe { HASH_BUILDER.get().unwrap().build_hasher() };
let hash = {
hasher.write(key);
Expand Down
63 changes: 18 additions & 45 deletions src/deserialize/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn deserialize(
ptr: *mut pyo3::ffi::PyObject,
ext_hook: Option<NonNull<pyo3::ffi::PyObject>>,
opts: Opt,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, DeserializeError<'static>> {
) -> Result<NonNull<pyo3::ffi::PyObject>, DeserializeError<'static>> {
let obj_type_ptr = ob_type!(ptr);
let buffer: *const u8;
let length: usize;
Expand Down Expand Up @@ -187,10 +187,7 @@ impl<'de> Deserializer<'de> {
Ok(Marker::from_u8(n))
}

fn deserialize_ext(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_ext(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let tag = self.read_i8()?;
let data = self.read_slice(len as usize)?;

Expand Down Expand Up @@ -219,65 +216,47 @@ impl<'de> Deserializer<'de> {
}
}

fn deserialize_null(&self) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_null(&self) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
ffi!(Py_INCREF(NONE));
Ok(nonnull!(NONE))
}

fn deserialize_true(&self) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_true(&self) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
ffi!(Py_INCREF(TRUE));
Ok(nonnull!(TRUE))
}

fn deserialize_false(&self) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_false(&self) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
ffi!(Py_INCREF(FALSE));
Ok(nonnull!(FALSE))
}

fn deserialize_i64(
&self,
value: i64,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_i64(&self, value: i64) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
Ok(nonnull!(ffi!(PyLong_FromLongLong(value))))
}

fn deserialize_u64(
&self,
value: u64,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_u64(&self, value: u64) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
Ok(nonnull!(ffi!(PyLong_FromUnsignedLongLong(value))))
}

fn deserialize_f64(
&self,
value: f64,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_f64(&self, value: f64) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
Ok(nonnull!(ffi!(PyFloat_FromDouble(value))))
}

fn deserialize_str(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_str(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let data = self.read_slice(len as usize)?;
let value = from_utf8(data)?;
Ok(nonnull!(unicode_from_str(value)))
}

fn deserialize_bin(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_bin(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let v = self.read_slice(len as usize)?;
let ptr = v.as_ptr() as *const c_char;
let len = v.len() as pyo3::ffi::Py_ssize_t;
Ok(nonnull!(ffi!(PyBytes_FromStringAndSize(ptr, len))))
}

fn deserialize_array(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_array(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let ptr = ffi!(PyList_New(len as pyo3::ffi::Py_ssize_t));
for i in 0..len {
let elem = self.deserialize()?;
Expand All @@ -293,7 +272,7 @@ impl<'de> Deserializer<'de> {
fn deserialize_map_with_str_keys(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let dict_ptr = ffi!(_PyDict_NewPresized(len as pyo3::ffi::Py_ssize_t));
for _ in 0..len {
let marker = self.read_marker()?;
Expand Down Expand Up @@ -331,7 +310,7 @@ impl<'de> Deserializer<'de> {
fn deserialize_map_with_non_str_keys(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let dict_ptr = ffi!(_PyDict_NewPresized(len as pyo3::ffi::Py_ssize_t));
for _ in 0..len {
let key = self.deserialize_map_key()?;
Expand All @@ -346,18 +325,15 @@ impl<'de> Deserializer<'de> {
Ok(nonnull!(dict_ptr))
}

fn deserialize_map(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_map(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
if self.opts & NON_STR_KEYS != 0 {
self.deserialize_map_with_non_str_keys(len)
} else {
self.deserialize_map_with_str_keys(len)
}
}

fn deserialize(&mut self) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize(&mut self) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
self.recursion += 1;
if unlikely!(self.recursion == RECURSION_LIMIT) {
return Err(Error::RecursionLimitReached);
Expand Down Expand Up @@ -477,10 +453,7 @@ impl<'de> Deserializer<'de> {
value
}

fn deserialize_map_str_key(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_map_str_key(&mut self, len: u32) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
if unlikely!(len > 64) {
let value = self.deserialize_str(len)?;
hash_str(value.as_ptr());
Expand All @@ -495,7 +468,7 @@ impl<'de> Deserializer<'de> {
fn deserialize_map_array_key(
&mut self,
len: u32,
) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
let ptr = ffi!(PyTuple_New(len as pyo3::ffi::Py_ssize_t));
for i in 0..len {
let elem = self.deserialize_map_key()?;
Expand All @@ -508,7 +481,7 @@ impl<'de> Deserializer<'de> {
Ok(nonnull!(ptr))
}

fn deserialize_map_key(&mut self) -> std::result::Result<NonNull<pyo3::ffi::PyObject>, Error> {
fn deserialize_map_key(&mut self) -> Result<NonNull<pyo3::ffi::PyObject>, Error> {
self.recursion += 1;
if unlikely!(self.recursion == RECURSION_LIMIT) {
return Err(Error::RecursionLimitReached);
Expand Down
4 changes: 2 additions & 2 deletions src/serialize/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum TimeError {
}

impl std::fmt::Display for TimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
match self {
Self::HasTimezone => write!(f, "datetime.time must not have tzinfo set"),
}
Expand Down Expand Up @@ -106,7 +106,7 @@ pub enum DateTimeError {
}

impl std::fmt::Display for DateTimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
match self {
Self::LibraryUnsupported => write!(f, "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"),
}
Expand Down
4 changes: 2 additions & 2 deletions src/serialize/numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ pub enum NumpyDatetimeUnit {
}

impl std::fmt::Display for NumpyDatetimeUnit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
let unit = match self {
Self::NaT => "NaT",
Self::Years => "years",
Expand Down Expand Up @@ -804,7 +804,7 @@ enum NumpyDateTimeError {
}

impl std::fmt::Display for NumpyDateTimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
match self {
Self::UnsupportedUnit(unit) => write!(f, "unsupported numpy.datetime64 unit: {}", unit),
Self::Unrepresentable { unit, val } => {
Expand Down

0 comments on commit b57df8e

Please sign in to comment.