PyWrapper

Struct PyWrapper 

Source
pub struct PyWrapper<T>
where T: PyWrapperT,
{ /* private fields */ }
Expand description

You can wrap the desired internal value in this structure to implement a pyclass that can obtain ownership of the internal value.

§Example

use pyo3::prelude::*;
use pyo3_utils::py_wrapper::{PyWrapper, PyWrapperT2};

struct Foo;

impl Foo {
    fn foo(self) {}
}

#[pyclass(frozen)]
#[non_exhaustive]
pub struct Bar(PyWrapper<PyWrapperT2<Foo>>);

#[pymethods]
impl Bar {
    // Normally you can directly use `&self` to get a reference
    // instead of using `Py<Self>::get` in a pymethod.
    // Here just for demonstration purposes.
    fn py_foo(slf: Py<Self>) -> PyResult<()> {
        slf.get().0.try_take_inner()??.foo();
        Ok(())
    }
}

NOTE: For PyWrapper<T>, changes from T = [PyWrapperT0] -> [PyWrapperT1] -> [PyWrapperT2] will not be considered breaking changes.

  • When the type is PyWrapperT0, all methods are zero-cost abstractions.
  • When the type changes to PyWrapperT1, compatibility with PyWrapperT0 is achieved by implicitly calling other methods that acquire locks. These compatible methods will emit deprecation warnings.
  • When the type changes to PyWrapperT2, compatibility with PyWrapperT1 is achieved by implicitly calling Result::unwrap() on other methods that return Result. These compatible methods will emit deprecation warnings.

Implementations§

Source§

impl<T> PyWrapper<PyWrapperT0<T>>

Source

pub fn new0(inner: T) -> Self

Source

pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>

Source

pub fn inner_mut(&mut self) -> impl MappableDerefMut<'_, Target = T>

Source

pub fn into_inner(self) -> T

Source§

impl<T> PyWrapper<PyWrapperT1<T>>

Source

pub fn new1(inner: T) -> Self

Source

pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>>

Source

pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>>

Source

pub fn into_inner(self) -> T

Source

pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>

👎Deprecated: use lock_inner_ref instead
§Panics

Panics if the internal value has already been mutably borrowed.

Source

pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>

👎Deprecated: use lock_inner_mut instead
§Panics

Panics if the internal value has already been mutably borrowed.

Source§

impl<T> PyWrapper<PyWrapperT2<T>>

Source

pub fn new2(inner: T) -> Self

Source

pub fn try_lock_inner_ref( &self, ) -> LockResult<ConsumedResult<MappedRwLockReadGuard<'_, T>>>

Source

pub fn try_lock_inner_mut( &self, ) -> LockResult<ConsumedResult<MappedRwLockWriteGuard<'_, T>>>

Source

pub fn try_take_inner(&self) -> LockResult<ConsumedResult<T>>

Source

pub fn try_replace_inner( &self, inner: ConsumedResult<T>, ) -> LockResult<ConsumedResult<T>>

similar to std::mem::replace

Source

pub fn try_read(&self) -> LockResult<RwLockReadGuard<'_, ConsumedResult<T>>>

Source

pub fn try_write(&self) -> LockResult<RwLockWriteGuard<'_, ConsumedResult<T>>>

Source

pub fn try_into_inner(self) -> ConsumedResult<T>

Source

pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>>

👎Deprecated: use try_lock_inner_ref instead
§Panics

Panics if the internal value has already been consumed, i.e., its ownership has been moved out.

Source

pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>>

👎Deprecated: use try_lock_inner_mut instead
§Panics

Panics if the internal value has already been consumed, i.e., its ownership has been moved out.

Source

pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>

👎Deprecated: use try_lock_inner_ref instead
§Panics

Panics if the internal value has already been mutably borrowed or consumed.

Source

pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>

👎Deprecated: use try_lock_inner_mut instead
§Panics

Panics if the internal value has already been mutably borrowed or consumed.

Source

pub fn into_inner(self) -> T

👎Deprecated: use try_into_inner instead
§Panics

Panics if the internal value has already been consumed, i.e., its ownership has been moved out.

Trait Implementations§

Source§

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT0<T>>

Source§

type Wrapped = T

Source§

fn inner_ref_semver( &self, ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn inner_mut_semver( &mut self, ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped>

Source§

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT2<T>>

Source§

type Wrapped = T

Source§

fn inner_ref_semver( &self, ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn inner_mut_semver( &mut self, ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped>

Source§

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT1<T>>

Source§

type Wrapped = T

Source§

fn inner_ref_semver( &self, ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn inner_mut_semver( &mut self, ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>>

For implementations of PyWrapper::<PyWrapperT1> and ::<PyWrapperT2>, locks will be acquired
Source§

fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped>

Auto Trait Implementations§

§

impl<T> Freeze for PyWrapper<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for PyWrapper<T>
where T: RefUnwindSafe,

§

impl<T> Send for PyWrapper<T>
where T: Send,

§

impl<T> Sync for PyWrapper<T>
where T: Sync,

§

impl<T> Unpin for PyWrapper<T>
where T: Unpin,

§

impl<T> UnwindSafe for PyWrapper<T>
where T: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.
Source§

impl<T> Ungil for T
where T: Send,