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>>
impl<T> PyWrapper<PyWrapperT0<T>>
pub fn new0(inner: T) -> Self
pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>
pub fn inner_mut(&mut self) -> impl MappableDerefMut<'_, Target = T>
pub fn into_inner(self) -> T
Source§impl<T> PyWrapper<PyWrapperT1<T>>
impl<T> PyWrapper<PyWrapperT1<T>>
pub fn new1(inner: T) -> Self
pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>>
pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>>
pub fn into_inner(self) -> T
Sourcepub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>
👎Deprecated: use lock_inner_ref
instead
pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>
lock_inner_ref
instead§Panics
Panics if the internal value has already been mutably borrowed.
Sourcepub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>
👎Deprecated: use lock_inner_mut
instead
pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>
lock_inner_mut
instead§Panics
Panics if the internal value has already been mutably borrowed.
Source§impl<T> PyWrapper<PyWrapperT2<T>>
impl<T> PyWrapper<PyWrapperT2<T>>
pub fn new2(inner: T) -> Self
pub fn try_lock_inner_ref( &self, ) -> LockResult<ConsumedResult<MappedRwLockReadGuard<'_, T>>>
pub fn try_lock_inner_mut( &self, ) -> LockResult<ConsumedResult<MappedRwLockWriteGuard<'_, T>>>
pub fn try_take_inner(&self) -> LockResult<ConsumedResult<T>>
Sourcepub fn try_replace_inner(
&self,
inner: ConsumedResult<T>,
) -> LockResult<ConsumedResult<T>>
pub fn try_replace_inner( &self, inner: ConsumedResult<T>, ) -> LockResult<ConsumedResult<T>>
similar to std::mem::replace
Sourcepub fn try_read(&self) -> LockResult<RwLockReadGuard<'_, ConsumedResult<T>>>
pub fn try_read(&self) -> LockResult<RwLockReadGuard<'_, ConsumedResult<T>>>
similar to parking_lot::RwLock::try_read
Sourcepub fn try_write(&self) -> LockResult<RwLockWriteGuard<'_, ConsumedResult<T>>>
pub fn try_write(&self) -> LockResult<RwLockWriteGuard<'_, ConsumedResult<T>>>
similar to parking_lot::RwLock::try_write
pub fn try_into_inner(self) -> ConsumedResult<T>
Sourcepub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>>
👎Deprecated: use try_lock_inner_ref
instead
pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>>
try_lock_inner_ref
instead§Panics
Panics if the internal value has already been consumed, i.e., its ownership has been moved out.
Sourcepub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>>
👎Deprecated: use try_lock_inner_mut
instead
pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>>
try_lock_inner_mut
instead§Panics
Panics if the internal value has already been consumed, i.e., its ownership has been moved out.
Sourcepub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>
👎Deprecated: use try_lock_inner_ref
instead
pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T>
try_lock_inner_ref
instead§Panics
Panics if the internal value has already been mutably borrowed or consumed.
Sourcepub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>
👎Deprecated: use try_lock_inner_mut
instead
pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T>
try_lock_inner_mut
instead§Panics
Panics if the internal value has already been mutably borrowed or consumed.
Sourcepub fn into_inner(self) -> T
👎Deprecated: use try_into_inner
instead
pub fn into_inner(self) -> T
try_into_inner
instead§Panics
Panics if the internal value has already been consumed, i.e., its ownership has been moved out.