Skip to content

Commit

Permalink
impl IntoPyObject for tuples (#4468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Aug 23, 2024
1 parent 03659ae commit ea6a0aa
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::iter::FusedIterator;

use crate::conversion::IntoPyObject;
use crate::ffi::{self, Py_ssize_t};
use crate::ffi_ptr_ext::FfiPtrExt;
#[cfg(feature = "experimental-inspect")]
Expand All @@ -8,8 +9,8 @@ use crate::instance::Borrowed;
use crate::internal_tricks::get_ssize_index;
use crate::types::{any::PyAnyMethods, sequence::PySequenceMethods, PyList, PySequence};
use crate::{
exceptions, Bound, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python,
ToPyObject,
exceptions, Bound, BoundObject, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult,
Python, ToPyObject,
};

#[inline]
Expand Down Expand Up @@ -526,6 +527,20 @@ fn type_output() -> TypeInfo {
}
}

impl <'py, $($T),+> IntoPyObject<'py> for ($($T,)+)
where
$($T: IntoPyObject<'py>,)+
PyErr: $(From<$T::Error> + )+
{
type Target = PyTuple;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(array_into_tuple(py, [$(self.$n.into_pyobject(py)?.into_any().unbind()),+]).into_bound(py))
}
}

impl <$($T: IntoPy<PyObject>),+> IntoPy<Py<PyTuple>> for ($($T,)+) {
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
array_into_tuple(py, [$(self.$n.into_py(py)),+])
Expand Down

0 comments on commit ea6a0aa

Please sign in to comment.