From 8a85feca976d2b62f6d15e053c337039fa190526 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Fri, 12 Jun 2020 05:21:27 +0100 Subject: [PATCH] Change return type of `PyTuple::slice` to `&[&PyAny]` --- CHANGELOG.md | 1 + src/types/tuple.rs | 18 ++++++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52b31d41..08908e19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Call `Py_Finalize` at exit to flush buffers, etc. [#943](https://github.com/PyO3/pyo3/pull/943) - Add type parameter to PyBuffer. #[951](https://github.com/PyO3/pyo3/pull/951) - Require `Send` bound for `#[pyclass]`. [#966](https://github.com/PyO3/pyo3/pull/966) +- Change return type of `PyTuple::as_slice` to `&[&PyAny]`. [#971](https://github.com/PyO3/pyo3/pull/971) ### Removed - Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930) diff --git a/src/types/tuple.rs b/src/types/tuple.rs index d8c9a0be..9549ce0f 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -2,8 +2,8 @@ use crate::ffi::{self, Py_ssize_t}; use crate::{ - exceptions, AsPyPointer, AsPyRef, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, - PyErr, PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject, + exceptions, AsPyPointer, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, + PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject, }; use std::slice; @@ -77,20 +77,19 @@ impl PyTuple { } /// Returns `self` as a slice of objects. - pub fn as_slice(&self) -> &[PyObject] { - // This is safe because PyObject has the same memory layout as *mut ffi::PyObject, + pub fn as_slice(&self) -> &[&PyAny] { + // This is safe because &PyAny has the same memory layout as *mut ffi::PyObject, // and because tuples are immutable. unsafe { let ptr = self.as_ptr() as *mut ffi::PyTupleObject; let slice = slice::from_raw_parts((*ptr).ob_item.as_ptr(), self.len()); - &*(slice as *const [*mut ffi::PyObject] as *const [PyObject]) + &*(slice as *const [*mut ffi::PyObject] as *const [&PyAny]) } } /// Returns an iterator over the tuple items. pub fn iter(&self) -> PyTupleIterator { PyTupleIterator { - py: self.py(), slice: self.as_slice(), index: 0, } @@ -99,8 +98,7 @@ impl PyTuple { /// Used by `PyTuple::iter()`. pub struct PyTupleIterator<'a> { - py: Python<'a>, - slice: &'a [PyObject], + slice: &'a [&'a PyAny], index: usize, } @@ -110,7 +108,7 @@ impl<'a> Iterator for PyTupleIterator<'a> { #[inline] fn next(&mut self) -> Option<&'a PyAny> { if self.index < self.slice.len() { - let item = self.slice[self.index].as_ref(self.py); + let item = self.slice[self.index]; self.index += 1; Some(item) } else { @@ -180,7 +178,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+ let slice = t.as_slice(); if t.len() == $length { Ok(( - $(slice[$n].extract::<$T>(obj.py())?,)+ + $(slice[$n].extract::<$T>()?,)+ )) } else { Err(wrong_tuple_length(t, $length))