Change return type of `PyTuple::slice` to `&[&PyAny]`

This commit is contained in:
David Hewitt 2020-06-12 05:21:27 +01:00
parent 7a7271319c
commit 8a85feca97
2 changed files with 9 additions and 10 deletions

View File

@ -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) - 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) - 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) - 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 ### Removed
- Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930) - Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930)

View File

@ -2,8 +2,8 @@
use crate::ffi::{self, Py_ssize_t}; use crate::ffi::{self, Py_ssize_t};
use crate::{ use crate::{
exceptions, AsPyPointer, AsPyRef, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, exceptions, AsPyPointer, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr,
PyErr, PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject, PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject,
}; };
use std::slice; use std::slice;
@ -77,20 +77,19 @@ impl PyTuple {
} }
/// Returns `self` as a slice of objects. /// Returns `self` as a slice of objects.
pub fn as_slice(&self) -> &[PyObject] { pub fn as_slice(&self) -> &[&PyAny] {
// This is safe because PyObject has the same memory layout as *mut ffi::PyObject, // This is safe because &PyAny has the same memory layout as *mut ffi::PyObject,
// and because tuples are immutable. // and because tuples are immutable.
unsafe { unsafe {
let ptr = self.as_ptr() as *mut ffi::PyTupleObject; let ptr = self.as_ptr() as *mut ffi::PyTupleObject;
let slice = slice::from_raw_parts((*ptr).ob_item.as_ptr(), self.len()); 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. /// Returns an iterator over the tuple items.
pub fn iter(&self) -> PyTupleIterator { pub fn iter(&self) -> PyTupleIterator {
PyTupleIterator { PyTupleIterator {
py: self.py(),
slice: self.as_slice(), slice: self.as_slice(),
index: 0, index: 0,
} }
@ -99,8 +98,7 @@ impl PyTuple {
/// Used by `PyTuple::iter()`. /// Used by `PyTuple::iter()`.
pub struct PyTupleIterator<'a> { pub struct PyTupleIterator<'a> {
py: Python<'a>, slice: &'a [&'a PyAny],
slice: &'a [PyObject],
index: usize, index: usize,
} }
@ -110,7 +108,7 @@ impl<'a> Iterator for PyTupleIterator<'a> {
#[inline] #[inline]
fn next(&mut self) -> Option<&'a PyAny> { fn next(&mut self) -> Option<&'a PyAny> {
if self.index < self.slice.len() { 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; self.index += 1;
Some(item) Some(item)
} else { } else {
@ -180,7 +178,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
let slice = t.as_slice(); let slice = t.as_slice();
if t.len() == $length { if t.len() == $length {
Ok(( Ok((
$(slice[$n].extract::<$T>(obj.py())?,)+ $(slice[$n].extract::<$T>()?,)+
)) ))
} else { } else {
Err(wrong_tuple_length(t, $length)) Err(wrong_tuple_length(t, $length))