Skip to content

Commit

Permalink
PyList/PyTuple: add as_sequence()
Browse files Browse the repository at this point in the history
Fixes #1845
  • Loading branch information
birkenfeld committed Sep 3, 2021
1 parent afd4d46 commit 7cb4faf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add range indexing implementations of `std::ops::Index` for `PyList`, `PyTuple` and `PySequence`. [#1829](https://github.com/PyO3/pyo3/pull/1829)
- Add commonly-used sequence methods to `PyList` and `PyTuple`. [#1849](https://github.com/PyO3/pyo3/pull/1849)
- The `pyo3-build-config` crate now has a `resolve-config` feature to control whether its build script does anything. [#1856](https://github.com/PyO3/pyo3/pull/1856)
- Add `as_sequence` methods to `PyList` and `PyTuple`. [#1860](https://github.com/PyO3/pyo3/pull/1860)

### Changed

Expand Down
13 changes: 9 additions & 4 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ impl PyList {
self.len() == 0
}

/// Returns `self` cast as a `PySequence`.
pub fn as_sequence(&self) -> &PySequence {
unsafe { PySequence::try_from_unchecked(self) }
}

/// Gets the list item at the specified index.
/// # Example
/// ```
Expand Down Expand Up @@ -139,7 +144,7 @@ impl PyList {
/// This is equivalent to the Python statement `del self[i]`.
#[inline]
pub fn del_item(&self, index: usize) -> PyResult<()> {
unsafe { PySequence::try_from_unchecked(self).del_item(index) }
self.as_sequence().del_item(index)
}

/// Assigns the sequence `seq` to the slice of `self` from `low` to `high`.
Expand All @@ -165,7 +170,7 @@ impl PyList {
/// This is equivalent to the Python statement `del self[low:high]`.
#[inline]
pub fn del_slice(&self, low: usize, high: usize) -> PyResult<()> {
unsafe { PySequence::try_from_unchecked(self).del_slice(low, high) }
self.as_sequence().del_slice(low, high)
}

/// Appends an item to the list.
Expand Down Expand Up @@ -201,7 +206,7 @@ impl PyList {
where
V: ToBorrowedObject,
{
unsafe { PySequence::try_from_unchecked(self).contains(value) }
self.as_sequence().contains(value)
}

/// Returns the first index `i` for which `self[i] == value`.
Expand All @@ -212,7 +217,7 @@ impl PyList {
where
V: ToBorrowedObject,
{
unsafe { PySequence::try_from_unchecked(self).index(value) }
self.as_sequence().index(value)
}

/// Returns an iterator over this list's items.
Expand Down
9 changes: 7 additions & 2 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ impl PyTuple {
self.len() == 0
}

/// Returns `self` cast as a `PySequence`.
pub fn as_sequence(&self) -> &PySequence {
unsafe { PySequence::try_from_unchecked(self) }
}

/// Takes the slice `self[low:high]` and returns it as a new tuple.
///
/// Indices must be nonnegative, and out-of-range indices are clipped to
Expand Down Expand Up @@ -153,7 +158,7 @@ impl PyTuple {
where
V: ToBorrowedObject,
{
unsafe { PySequence::try_from_unchecked(self).contains(value) }
self.as_sequence().contains(value)
}

/// Returns the first index `i` for which `self[i] == value`.
Expand All @@ -164,7 +169,7 @@ impl PyTuple {
where
V: ToBorrowedObject,
{
unsafe { PySequence::try_from_unchecked(self).index(value) }
self.as_sequence().index(value)
}

/// Returns an iterator over the tuple items.
Expand Down

0 comments on commit 7cb4faf

Please sign in to comment.