Skip to content

Commit

Permalink
Merge pull request #971 from davidhewitt/pytuple-slice-pyany
Browse files Browse the repository at this point in the history
Change return type of `PyTuple::slice` to `&[&PyAny]`
  • Loading branch information
kngwyu authored Jun 13, 2020
2 parents 7a72713 + 8a85fec commit 90a2ec3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 90a2ec3

Please sign in to comment.