Skip to content

Commit 90a2ec3

Browse files
authored
Merge pull request #971 from davidhewitt/pytuple-slice-pyany
Change return type of `PyTuple::slice` to `&[&PyAny]`
2 parents 7a72713 + 8a85fec commit 90a2ec3

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- Call `Py_Finalize` at exit to flush buffers, etc. [#943](https://github.com/PyO3/pyo3/pull/943)
1414
- Add type parameter to PyBuffer. #[951](https://github.com/PyO3/pyo3/pull/951)
1515
- Require `Send` bound for `#[pyclass]`. [#966](https://github.com/PyO3/pyo3/pull/966)
16+
- Change return type of `PyTuple::as_slice` to `&[&PyAny]`. [#971](https://github.com/PyO3/pyo3/pull/971)
1617

1718
### Removed
1819
- Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930)

src/types/tuple.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use crate::ffi::{self, Py_ssize_t};
44
use crate::{
5-
exceptions, AsPyPointer, AsPyRef, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny,
6-
PyErr, PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject,
5+
exceptions, AsPyPointer, FromPy, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr,
6+
PyNativeType, PyObject, PyResult, PyTryFrom, Python, ToPyObject,
77
};
88
use std::slice;
99

@@ -77,20 +77,19 @@ impl PyTuple {
7777
}
7878

7979
/// Returns `self` as a slice of objects.
80-
pub fn as_slice(&self) -> &[PyObject] {
81-
// This is safe because PyObject has the same memory layout as *mut ffi::PyObject,
80+
pub fn as_slice(&self) -> &[&PyAny] {
81+
// This is safe because &PyAny has the same memory layout as *mut ffi::PyObject,
8282
// and because tuples are immutable.
8383
unsafe {
8484
let ptr = self.as_ptr() as *mut ffi::PyTupleObject;
8585
let slice = slice::from_raw_parts((*ptr).ob_item.as_ptr(), self.len());
86-
&*(slice as *const [*mut ffi::PyObject] as *const [PyObject])
86+
&*(slice as *const [*mut ffi::PyObject] as *const [&PyAny])
8787
}
8888
}
8989

9090
/// Returns an iterator over the tuple items.
9191
pub fn iter(&self) -> PyTupleIterator {
9292
PyTupleIterator {
93-
py: self.py(),
9493
slice: self.as_slice(),
9594
index: 0,
9695
}
@@ -99,8 +98,7 @@ impl PyTuple {
9998

10099
/// Used by `PyTuple::iter()`.
101100
pub struct PyTupleIterator<'a> {
102-
py: Python<'a>,
103-
slice: &'a [PyObject],
101+
slice: &'a [&'a PyAny],
104102
index: usize,
105103
}
106104

@@ -110,7 +108,7 @@ impl<'a> Iterator for PyTupleIterator<'a> {
110108
#[inline]
111109
fn next(&mut self) -> Option<&'a PyAny> {
112110
if self.index < self.slice.len() {
113-
let item = self.slice[self.index].as_ref(self.py);
111+
let item = self.slice[self.index];
114112
self.index += 1;
115113
Some(item)
116114
} else {
@@ -180,7 +178,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
180178
let slice = t.as_slice();
181179
if t.len() == $length {
182180
Ok((
183-
$(slice[$n].extract::<$T>(obj.py())?,)+
181+
$(slice[$n].extract::<$T>()?,)+
184182
))
185183
} else {
186184
Err(wrong_tuple_length(t, $length))

0 commit comments

Comments
 (0)