Struct pyo3::impl_::pymethods::BoundRef

source ·
pub struct BoundRef<'a, 'py, T>(pub &'a Bound<'py, T>);
Expand description

Used in #[classmethod] to pass the class object to the method and also in #[pyfunction(pass_module)].

This is a wrapper to avoid implementing From<Bound> for GIL Refs.

Once the GIL Ref API is fully removed, it should be possible to simplify this to just &'a Bound<'py, T> and From implementations.

Tuple Fields§

§0: &'a Bound<'py, T>

Implementations§

source§

impl<'a, 'py> BoundRef<'a, 'py, PyAny>

source

pub unsafe fn ref_from_ptr(py: Python<'py>, ptr: &'a *mut PyObject) -> Self

source

pub unsafe fn ref_from_ptr_or_opt( py: Python<'py>, ptr: &'a *mut PyObject ) -> Option<Self>

source

pub fn downcast<T: PyTypeCheck>( self ) -> Result<BoundRef<'a, 'py, T>, DowncastError<'a, 'py>>

source

pub unsafe fn downcast_unchecked<T>(self) -> BoundRef<'a, 'py, T>

Methods from Deref<Target = Bound<'py, T>>§

source

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.

source

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.

source

pub fn try_borrow(&self) -> Result<PyRef<'py, T>, PyBorrowError>

Attempts to immutably borrow the value T, returning an error if the value is currently mutably borrowed.

The borrow lasts while the returned PyRef exists.

This is the non-panicking variant of borrow.

For frozen classes, the simpler get is available.

source

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.

source

pub fn get(&self) -> &T
where T: PyClass<Frozen = True> + Sync,

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);
});
source

pub(crate) fn get_class_object(&self) -> &PyClassObject<T>

source

pub fn py(&self) -> Python<'py>

Returns the GIL token associated with this object.

source

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.

source

pub fn as_any(&self) -> &Bound<'py, PyAny>

Helper to cast to Bound<'py, PyAny>.

source

pub fn as_borrowed<'a>(&'a self) -> Borrowed<'a, 'py, T>

Casts this Bound<T> to a Borrowed<T> smart pointer.

source

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.

source

pub fn as_gil_ref(&'py self) -> &'py T::AsRefTarget
where T: HasPyGilRef,

Casts this Bound<T> as the corresponding “GIL Ref” type.

This is a helper to be used for migration from the deprecated “GIL Refs” API.

source

pub(crate) fn lookup_special<N>( &self, attr_name: N ) -> PyResult<Option<Bound<'py, PyAny>>>
where N: IntoPy<Py<PyString>>,

Retrieve an attribute value, skipping the instance dictionary during the lookup but still binding the object to the instance.

This is useful when trying to resolve Python’s “magic” methods like __getitem__, which are looked up starting from the type object. This returns an Option as it is not typically a direct error for the special lookup to fail, as magic methods are optional in many situations in which they might be called.

To avoid repeated temporary allocations of Python strings, the intern! macro can be used to intern attr_name.

Trait Implementations§

source§

impl<'py, T> Deref for BoundRef<'_, 'py, T>

§

type Target = Bound<'py, T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> From<BoundRef<'_, '_, T>> for Py<T>

source§

fn from(bound: BoundRef<'_, '_, T>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<BoundRef<'a, 'a, PyModule>> for &'a PyModule

source§

fn from(bound: BoundRef<'a, 'a, PyModule>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<BoundRef<'a, 'a, PyType>> for &'a PyType

source§

fn from(bound: BoundRef<'a, 'a, PyType>) -> Self

Converts to this type from the input type.
source§

impl<'a, 'py, T> From<BoundRef<'a, 'py, T>> for &'a Bound<'py, T>

source§

fn from(bound: BoundRef<'a, 'py, T>) -> Self

Converts to this type from the input type.
source§

impl<'a, 'py, T: PyClass> From<BoundRef<'a, 'py, T>> for &'a PyCell<T>

source§

fn from(bound: BoundRef<'a, 'py, T>) -> Self

Converts to this type from the input type.
source§

impl<'a, 'py, T> From<BoundRef<'a, 'py, T>> for Bound<'py, T>

source§

fn from(bound: BoundRef<'a, 'py, T>) -> Self

Converts to this type from the input type.
source§

impl<'a, 'py, T: PyClass> TryFrom<BoundRef<'a, 'py, T>> for PyRef<'py, T>

§

type Error = PyBorrowError

The type returned in the event of a conversion error.
source§

fn try_from(value: BoundRef<'a, 'py, T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a, 'py, T: PyClass<Frozen = False>> TryFrom<BoundRef<'a, 'py, T>> for PyRefMut<'py, T>

§

type Error = PyBorrowMutError

The type returned in the event of a conversion error.
source§

fn try_from(value: BoundRef<'a, 'py, T>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a, 'py, T> Freeze for BoundRef<'a, 'py, T>

§

impl<'a, 'py, T> RefUnwindSafe for BoundRef<'a, 'py, T>
where T: RefUnwindSafe,

§

impl<'a, 'py, T> !Send for BoundRef<'a, 'py, T>

§

impl<'a, 'py, T> !Sync for BoundRef<'a, 'py, T>

§

impl<'a, 'py, T> Unpin for BoundRef<'a, 'py, T>

§

impl<'a, 'py, T> UnwindSafe for BoundRef<'a, 'py, T>
where T: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AssertNotZeroSized for T

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> SizedTypeProperties for T

source§

#[doc(hidden)] const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
source§

impl<T> SomeWrap<T> for T

source§

fn wrap(self) -> Option<T>

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here