pub trait PyTupleMethods<'py>: Sealed {
Show 15 methods
// Required methods
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn as_sequence(&self) -> &Bound<'py, PySequence>;
fn into_sequence(self) -> Bound<'py, PySequence>;
fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyTuple>;
fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>;
fn get_borrowed_item<'a>(
&'a self,
index: usize,
) -> PyResult<Borrowed<'a, 'py, PyAny>>;
unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>;
unsafe fn get_borrowed_item_unchecked<'a>(
&'a self,
index: usize,
) -> Borrowed<'a, 'py, PyAny>;
fn as_slice(&self) -> &[Bound<'py, PyAny>];
fn contains<V>(&self, value: V) -> PyResult<bool>
where V: ToPyObject;
fn index<V>(&self, value: V) -> PyResult<usize>
where V: ToPyObject;
fn iter(&self) -> BoundTupleIterator<'py> ⓘ;
fn iter_borrowed<'a>(&'a self) -> BorrowedTupleIterator<'a, 'py> ⓘ;
fn to_list(&self) -> Bound<'py, PyList>;
}
Expand description
Implementation of functionality for PyTuple
.
These methods are defined for the Bound<'py, PyTuple>
smart pointer, so to use method call
syntax these methods are separated into a trait, because stable Rust does not yet support
arbitrary_self_types
.
Required Methods§
Sourcefn as_sequence(&self) -> &Bound<'py, PySequence>
fn as_sequence(&self) -> &Bound<'py, PySequence>
Returns self
cast as a PySequence
.
Sourcefn into_sequence(self) -> Bound<'py, PySequence>
fn into_sequence(self) -> Bound<'py, PySequence>
Returns self
cast as a PySequence
.
Sourcefn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyTuple>
fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyTuple>
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
self.len()
.
Sourcefn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>
fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>>
Gets the tuple item at the specified index.
§Example
use pyo3::{prelude::*, types::PyTuple};
Python::with_gil(|py| -> PyResult<()> {
let ob = (1, 2, 3).to_object(py);
let tuple = ob.downcast_bound::<PyTuple>(py).unwrap();
let obj = tuple.get_item(0);
assert_eq!(obj.unwrap().extract::<i32>().unwrap(), 1);
Ok(())
})
Sourcefn get_borrowed_item<'a>(
&'a self,
index: usize,
) -> PyResult<Borrowed<'a, 'py, PyAny>>
fn get_borrowed_item<'a>( &'a self, index: usize, ) -> PyResult<Borrowed<'a, 'py, PyAny>>
Like get_item
, but returns a borrowed object, which is a slight performance optimization
by avoiding a reference count change.
Sourceunsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>
unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>
Gets the tuple item at the specified index. Undefined behavior on bad index. Use with caution.
§Safety
Caller must verify that the index is within the bounds of the tuple.
Sourceunsafe fn get_borrowed_item_unchecked<'a>(
&'a self,
index: usize,
) -> Borrowed<'a, 'py, PyAny>
unsafe fn get_borrowed_item_unchecked<'a>( &'a self, index: usize, ) -> Borrowed<'a, 'py, PyAny>
Like get_item_unchecked
, but returns a borrowed object,
which is a slight performance optimization by avoiding a reference count change.
§Safety
Caller must verify that the index is within the bounds of the tuple.
Sourcefn contains<V>(&self, value: V) -> PyResult<bool>where
V: ToPyObject,
fn contains<V>(&self, value: V) -> PyResult<bool>where
V: ToPyObject,
Determines if self contains value
.
This is equivalent to the Python expression value in self
.
Sourcefn index<V>(&self, value: V) -> PyResult<usize>where
V: ToPyObject,
fn index<V>(&self, value: V) -> PyResult<usize>where
V: ToPyObject,
Returns the first index i
for which self[i] == value
.
This is equivalent to the Python expression self.index(value)
.
Sourcefn iter(&self) -> BoundTupleIterator<'py> ⓘ
fn iter(&self) -> BoundTupleIterator<'py> ⓘ
Returns an iterator over the tuple items.
Sourcefn iter_borrowed<'a>(&'a self) -> BorrowedTupleIterator<'a, 'py> ⓘ
fn iter_borrowed<'a>(&'a self) -> BorrowedTupleIterator<'a, 'py> ⓘ
Like iter
, but produces an iterator which returns borrowed objects,
which is a slight performance optimization by avoiding a reference count change.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.