pub struct Borrowed<'a, 'py, T>(/* private fields */);Expand description
A borrowed equivalent to Bound.
The advantage of this over &Bound is that it avoids the need to have a pointer-to-pointer, as Bound
is already a pointer to an `ffi::PyObject``.
Similarly, this type is Copy and Clone, like a shared reference (&T).
Implementations§
Source§impl<'py, T> Borrowed<'_, 'py, T>
 
impl<'py, T> Borrowed<'_, 'py, T>
Sourcepub fn to_owned(self) -> Bound<'py, T>
 
pub fn to_owned(self) -> Bound<'py, T>
Creates a new owned Bound<T> from this borrowed reference by
increasing the reference count.
§Example
use pyo3::{prelude::*, types::PyTuple};
Python::with_gil(|py| -> PyResult<()> {
    let tuple = PyTuple::new_bound(py, [1, 2, 3]);
    // borrows from `tuple`, so can only be
    // used while `tuple` stays alive
    let borrowed = tuple.get_borrowed_item(0)?;
    // creates a new owned reference, which
    // can be used indendently of `tuple`
    let bound = borrowed.to_owned();
    drop(tuple);
    assert_eq!(bound.extract::<i32>().unwrap(), 1);
    Ok(())
})Source§impl<'a, 'py> Borrowed<'a, 'py, PyAny>
 
impl<'a, 'py> Borrowed<'a, 'py, PyAny>
Sourcepub unsafe fn from_ptr(py: Python<'py>, ptr: *mut PyObject) -> Self
 
pub unsafe fn from_ptr(py: Python<'py>, ptr: *mut PyObject) -> Self
Constructs a new Borrowed<'a, 'py, PyAny> from a pointer. Panics if ptr is null.
Prefer to use Bound::from_borrowed_ptr, as that avoids the major safety risk
of needing to precisely define the lifetime 'a for which the borrow is valid.
§Safety
ptrmust be a valid pointer to a Python object- similar to 
std::slice::from_raw_parts, the lifetime'ais completely defined by the caller and it is the caller’s responsibility to ensure that the reference this is derived from is valid for the lifetime'a. 
Sourcepub unsafe fn from_ptr_or_opt(
    py: Python<'py>,
    ptr: *mut PyObject,
) -> Option<Self>
 
pub unsafe fn from_ptr_or_opt( py: Python<'py>, ptr: *mut PyObject, ) -> Option<Self>
Constructs a new Borrowed<'a, 'py, PyAny> from a pointer. Returns None if ptr is null.
Prefer to use Bound::from_borrowed_ptr_or_opt, as that avoids the major safety risk
of needing to precisely define the lifetime 'a for which the borrow is valid.
§Safety
ptrmust be a valid pointer to a Python object, or null- similar to 
std::slice::from_raw_parts, the lifetime'ais completely defined by the caller and it is the caller’s responsibility to ensure that the reference this is derived from is valid for the lifetime'a. 
Sourcepub unsafe fn from_ptr_or_err(
    py: Python<'py>,
    ptr: *mut PyObject,
) -> PyResult<Self>
 
pub unsafe fn from_ptr_or_err( py: Python<'py>, ptr: *mut PyObject, ) -> PyResult<Self>
Constructs a new Borrowed<'a, 'py, PyAny> from a pointer. Returns an Err by calling PyErr::fetch
if ptr is null.
Prefer to use Bound::from_borrowed_ptr_or_err, as that avoids the major safety risk
of needing to precisely define the lifetime 'a for which the borrow is valid.
§Safety
ptrmust be a valid pointer to a Python object, or null- similar to 
std::slice::from_raw_parts, the lifetime'ais completely defined by the caller and it is the caller’s responsibility to ensure that the reference this is derived from is valid for the lifetime'a. 
Methods from Deref<Target = Bound<'py, T>>§
Sourcepub fn borrow(&self) -> PyRef<'py, T>
 
pub fn borrow(&self) -> PyRef<'py, T>
Immutably borrows the value T.
This borrow lasts while the returned PyRef exists.
Multiple immutable borrows can be taken out at the same time.
For frozen classes, the simpler get is available.
§Examples
#[pyclass]
struct Foo {
    inner: u8,
}
Python::with_gil(|py| -> PyResult<()> {
    let foo: Bound<'_, Foo> = Bound::new(py, Foo { inner: 73 })?;
    let inner: &u8 = &foo.borrow().inner;
    assert_eq!(*inner, 73);
    Ok(())
})?;§Panics
Panics if the value is currently mutably borrowed. For a non-panicking variant, use
try_borrow.
Sourcepub fn borrow_mut(&self) -> PyRefMut<'py, T>where
    T: PyClass<Frozen = False>,
 
pub fn borrow_mut(&self) -> PyRefMut<'py, T>where
    T: PyClass<Frozen = False>,
Mutably borrows the value T.
This borrow lasts while the returned PyRefMut exists.
§Examples
#[pyclass]
struct Foo {
    inner: u8,
}
Python::with_gil(|py| -> PyResult<()> {
    let foo: Bound<'_, Foo> = Bound::new(py, Foo { inner: 73 })?;
    foo.borrow_mut().inner = 35;
    assert_eq!(foo.borrow().inner, 35);
    Ok(())
})?;§Panics
Panics if the value is currently borrowed. For a non-panicking variant, use
try_borrow_mut.
Sourcepub fn try_borrow(&self) -> Result<PyRef<'py, T>, PyBorrowError>
 
pub fn try_borrow(&self) -> Result<PyRef<'py, T>, PyBorrowError>
Sourcepub fn try_borrow_mut(&self) -> Result<PyRefMut<'py, T>, PyBorrowMutError>where
    T: PyClass<Frozen = False>,
 
pub fn try_borrow_mut(&self) -> Result<PyRefMut<'py, T>, PyBorrowMutError>where
    T: PyClass<Frozen = False>,
Attempts to mutably borrow the value T, returning an error if the value is currently borrowed.
The borrow lasts while the returned PyRefMut exists.
This is the non-panicking variant of borrow_mut.
Sourcepub fn get(&self) -> &T
 
pub fn get(&self) -> &T
Provide an immutable borrow of the value T without acquiring the GIL.
This is available if the class is frozen and Sync.
§Examples
use std::sync::atomic::{AtomicUsize, Ordering};
#[pyclass(frozen)]
struct FrozenCounter {
    value: AtomicUsize,
}
Python::with_gil(|py| {
    let counter = FrozenCounter { value: AtomicUsize::new(0) };
    let py_counter = Bound::new(py, counter).unwrap();
    py_counter.get().value.fetch_add(1, Ordering::Relaxed);
});Sourcepub fn as_ptr(&self) -> *mut PyObject
 
pub fn as_ptr(&self) -> *mut PyObject
Returns the raw FFI pointer represented by self.
§Safety
Callers are responsible for ensuring that the pointer does not outlive self.
The reference is borrowed; callers should not decrease the reference count when they are finished with the pointer.
Sourcepub fn as_borrowed<'a>(&'a self) -> Borrowed<'a, 'py, T>
 
pub fn as_borrowed<'a>(&'a self) -> Borrowed<'a, 'py, T>
Casts this Bound<T> to a Borrowed<T> smart pointer.
Sourcepub fn as_unbound(&self) -> &Py<T>
 
pub fn as_unbound(&self) -> &Py<T>
Removes the connection for this Bound<T> from the GIL, allowing
it to cross thread boundaries, without transferring ownership.
Trait Implementations§
Source§impl PartialEq<&[u8]> for Borrowed<'_, '_, PyBytes>
Compares whether the Python bytes object is equal to the u8.
 
impl PartialEq<&[u8]> for Borrowed<'_, '_, PyBytes>
Compares whether the Python bytes object is equal to the u8.
In some cases Python equality might be more appropriate; see the note on PyBytes.
Source§impl PartialEq<&str> for Borrowed<'_, '_, PyString>
Compares whether the data in the Python string is equal to the given UTF8.
 
impl PartialEq<&str> for Borrowed<'_, '_, PyString>
Compares whether the data in the Python string is equal to the given UTF8.
In some cases Python equality might be more appropriate; see the note on PyString.
Source§impl PartialEq<[u8]> for Borrowed<'_, '_, PyBytes>
Compares whether the Python bytes object is equal to the u8.
 
impl PartialEq<[u8]> for Borrowed<'_, '_, PyBytes>
Compares whether the Python bytes object is equal to the u8.
In some cases Python equality might be more appropriate; see the note on PyBytes.
Source§impl PartialEq<Borrowed<'_, '_, PyBytes>> for &[u8]
Compares whether the Python bytes object is equal to the u8.
 
impl PartialEq<Borrowed<'_, '_, PyBytes>> for &[u8]
Compares whether the Python bytes object is equal to the u8.
In some cases Python equality might be more appropriate; see the note on PyBytes.
Source§impl PartialEq<Borrowed<'_, '_, PyBytes>> for [u8]
Compares whether the Python bytes object is equal to the u8.
 
impl PartialEq<Borrowed<'_, '_, PyBytes>> for [u8]
Compares whether the Python bytes object is equal to the u8.
In some cases Python equality might be more appropriate; see the note on PyBytes.
Source§impl PartialEq<Borrowed<'_, '_, PyString>> for &str
Compares whether the data in the Python string is equal to the given UTF8.
 
impl PartialEq<Borrowed<'_, '_, PyString>> for &str
Compares whether the data in the Python string is equal to the given UTF8.
In some cases Python equality might be more appropriate; see the note on PyString.
Source§impl PartialEq<Borrowed<'_, '_, PyString>> for str
Compares whether the data in the Python string is equal to the given UTF8.
 
impl PartialEq<Borrowed<'_, '_, PyString>> for str
Compares whether the data in the Python string is equal to the given UTF8.
In some cases Python equality might be more appropriate; see the note on PyString.
Source§impl PartialEq<str> for Borrowed<'_, '_, PyString>
Compares whether the data in the Python string is equal to the given UTF8.
 
impl PartialEq<str> for Borrowed<'_, '_, PyString>
Compares whether the data in the Python string is equal to the given UTF8.
In some cases Python equality might be more appropriate; see the note on PyString.