Skip to content

Commit

Permalink
use PyTuple_Pack in fixed-size tuple conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jul 4, 2023
1 parent 40437c5 commit 867a12b
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,24 @@ fn wrong_tuple_length(t: &PyTuple, expected_length: usize) -> PyErr {
macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => {
impl <$($T: ToPyObject),+> ToPyObject for ($($T,)+) {
fn to_object(&self, py: Python<'_>) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = PyObject::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.to_object(py).into_ptr());)+
ret
#[inline]
fn inner(py: Python<'_>, $($refN: PyObject),+) -> PyObject {
unsafe {
PyObject::from_owned_ptr(py, ffi::PyTuple_Pack($length, $($refN.as_ptr()),+))
}
}
inner(py, $(self.$n.to_object(py)),+)
}
}
impl <$($T: IntoPy<PyObject>),+> IntoPy<PyObject> for ($($T,)+) {
fn into_py(self, py: Python<'_>) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = PyObject::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_py(py).into_ptr());)+
ret
#[inline]
fn inner(py: Python<'_>, $($refN: PyObject),+) -> PyObject {
unsafe {
Py::from_owned_ptr(py, ffi::PyTuple_Pack($length, $($refN.as_ptr()),+))
}
}
inner(py, $(self.$n.into_py(py)),+)
}

#[cfg(feature = "experimental-inspect")]
Expand All @@ -310,12 +312,13 @@ fn type_output() -> TypeInfo {

impl <$($T: IntoPy<PyObject>),+> IntoPy<Py<PyTuple>> for ($($T,)+) {
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = Py::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_py(py).into_ptr());)+
ret
#[inline]
fn inner(py: Python<'_>, $($refN: PyObject),+) -> Py<PyTuple> {
unsafe {
Py::from_owned_ptr(py, ffi::PyTuple_Pack($length, $($refN.as_ptr()),+))
}
}
inner(py, $(self.$n.into_py(py)),+)
}

#[cfg(feature = "experimental-inspect")]
Expand Down

0 comments on commit 867a12b

Please sign in to comment.