Struct cpython::PySharedRefCell

source ·
pub struct PySharedRefCell<T: ?Sized> { /* private fields */ }
Expand description

A mutable memory location shareable immutably across Python objects.

This is a RefCell that can also be borrowed immutably by another Python object.

The primary use case is to implement a Python iterator over a Rust iterator. Since a Python object cannot hold a lifetime-bound object, Iter<'a, T> cannot be a data field of the Python iterator object. PySharedRef::leak_immutable() provides a way around this issue.

py_class!(pub class List |py| {
    @shared data rust_vec: Vec<i32>;

    def __iter__(&self) -> PyResult<ListIterator> {
        let leaked = self.rust_vec(py).leak_immutable();
        ListIterator::create_instance(
            py,
            RefCell::new(unsafe { leaked.map(py, |o| o.iter()) }),
        )
    }
});

py_class!(pub class ListIterator |py| {
    data rust_iter: RefCell<UnsafePyLeaked<Iter<'static, i32>>>;

    def __next__(&self) -> PyResult<Option<PyInt>> {
        let mut leaked = self.rust_iter(py).borrow_mut();
        let mut iter = unsafe { leaked.try_borrow_mut(py)? };
        Ok(iter.next().map(|v| v.to_py_object(py)))
    }

    def __iter__(&self) -> PyResult<Self> {
        Ok(self.clone_ref(py))
    }
});

The borrow rules are enforced dynamically in a similar manner to the Python iterator.

PySharedRefCell is merely a data struct to be stored in a Python object. Any further operation will be performed through PySharedRef, which is a lifetime-bound reference to the PySharedRefCell.

Implementations§

source§

impl<T> PySharedRefCell<T>

source

pub fn new(value: T) -> PySharedRefCell<T>

Creates a new PySharedRefCell containing value.

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for PySharedRefCell<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for PySharedRefCell<T>

§

impl<T> !RefUnwindSafe for PySharedRefCell<T>

§

impl<T> Send for PySharedRefCell<T>
where T: Send + ?Sized,

§

impl<T> !Sync for PySharedRefCell<T>

§

impl<T> Unpin for PySharedRefCell<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for PySharedRefCell<T>
where T: UnwindSafe + ?Sized,

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

§

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.