-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SerializeState for fast call convention, repr(transparent)
- Loading branch information
Showing
11 changed files
with
198 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,62 @@ | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
use crate::opt::*; | ||
use crate::serialize::error::*; | ||
use crate::serialize::serializer::*; | ||
|
||
use serde::ser::{Serialize, Serializer}; | ||
|
||
use std::ptr::NonNull; | ||
|
||
pub struct DefaultSerializer { | ||
ptr: *mut pyo3_ffi::PyObject, | ||
opts: Opt, | ||
default_calls: u8, | ||
recursion: u8, | ||
default: Option<NonNull<pyo3_ffi::PyObject>>, | ||
#[repr(transparent)] | ||
pub struct DefaultSerializer<'a> { | ||
previous: &'a PyObjectSerializer, | ||
} | ||
|
||
impl DefaultSerializer { | ||
pub fn new( | ||
ptr: *mut pyo3_ffi::PyObject, | ||
opts: Opt, | ||
default_calls: u8, | ||
recursion: u8, | ||
default: Option<NonNull<pyo3_ffi::PyObject>>, | ||
) -> Self { | ||
DefaultSerializer { | ||
ptr: ptr, | ||
opts: opts, | ||
default_calls: default_calls, | ||
recursion: recursion, | ||
default: default, | ||
} | ||
impl<'a> DefaultSerializer<'a> { | ||
pub fn new(previous: &'a PyObjectSerializer) -> Self { | ||
Self { previous: previous } | ||
} | ||
} | ||
|
||
impl Serialize for DefaultSerializer { | ||
impl<'a> Serialize for DefaultSerializer<'a> { | ||
#[inline(never)] | ||
#[cfg_attr(feature = "optimize", optimize(size))] | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match self.default { | ||
match self.previous.default { | ||
Some(callable) => { | ||
if unlikely!(self.default_calls == RECURSION_LIMIT) { | ||
if unlikely!(self.previous.state.default_calls_limit()) { | ||
err!(SerializeError::DefaultRecursionLimit) | ||
} | ||
#[cfg(not(Py_3_10))] | ||
let default_obj = ffi!(PyObject_CallFunctionObjArgs( | ||
callable.as_ptr(), | ||
self.ptr, | ||
self.previous.ptr, | ||
std::ptr::null_mut() as *mut pyo3_ffi::PyObject | ||
)); | ||
#[cfg(Py_3_10)] | ||
let default_obj = unsafe { | ||
pyo3_ffi::PyObject_Vectorcall( | ||
callable.as_ptr(), | ||
std::ptr::addr_of!(self.ptr), | ||
std::ptr::addr_of!(self.previous.ptr), | ||
pyo3_ffi::PyVectorcall_NARGS(1) as usize, | ||
std::ptr::null_mut(), | ||
) | ||
}; | ||
if unlikely!(default_obj.is_null()) { | ||
err!(SerializeError::UnsupportedType(nonnull!(self.ptr))) | ||
err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))) | ||
} else { | ||
let res = PyObjectSerializer::new( | ||
default_obj, | ||
self.opts, | ||
self.default_calls + 1, | ||
self.recursion, | ||
self.default, | ||
self.previous.state.copy_for_default_call(), | ||
self.previous.default, | ||
) | ||
.serialize(serializer); | ||
ffi!(Py_DECREF(default_obj)); | ||
res | ||
} | ||
} | ||
None => err!(SerializeError::UnsupportedType(nonnull!(self.ptr))), | ||
None => err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))), | ||
} | ||
} | ||
} |
Oops, something went wrong.