Struct cpython::UnsafePyLeaked

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

An immutable reference to PySharedRefCell value, not bound to lifetime.

The reference will be invalidated once the original value is mutably borrowed.

§Safety

Even though UnsafePyLeaked tries to enforce the real lifetime of the underlying object, the object having the artificial 'static lifetime may be exposed to your Rust code. You must be careful to not make a bare reference outlive the actual object lifetime.

let outer;
unsafe { leaked.map(py, |o| { outer = o }) };  // Bad
let outer;
let mut leaked_iter = leaked.map(py, |o| o.iter());
{
    let mut iter = unsafe { leaked_iter.try_borrow_mut(py) };
    let inner = iter.next();  // Good, in borrow scope
    outer = inner;            // Bad, &'static T may outlive
}

Implementations§

source§

impl<T: ?Sized> UnsafePyLeaked<T>

source

pub unsafe fn try_borrow<'a>( &'a self, py: Python<'a> ) -> PyResult<PyLeakedRef<'a, T>>

Immutably borrows the wrapped value.

Borrowing fails if the underlying reference has been invalidated.

§Safety

The lifetime of the innermost object is artificial. Do not obtain and copy it out of the borrow scope.

source

pub unsafe fn try_borrow_mut<'a>( &'a mut self, py: Python<'a> ) -> PyResult<PyLeakedRefMut<'a, T>>

Mutably borrows the wrapped value.

Borrowing fails if the underlying reference has been invalidated.

Typically T is an iterator. If T is an immutable reference, get_mut() is useless since the inner value can’t be mutated.

§Safety

The lifetime of the innermost object is artificial. Do not obtain and copy it out of the borrow scope.

source§

impl<T> UnsafePyLeaked<T>

source

pub unsafe fn map<U>( self, py: Python<'_>, f: impl FnOnce(T) -> U ) -> UnsafePyLeaked<U>

Converts the inner value by the given function.

Typically T is a static reference to a collection, and U is an iterator of that collection.

§Panics

Panics if the underlying reference has been invalidated.

This is typically called immediately after the UnsafePyLeaked is obtained. At this time, the reference must be valid and no panic would occur.

§Safety

The lifetime of the object passed in to the function f is artificial. It’s typically a static reference, but is valid only while the corresponding UnsafePyLeaked is alive. Do not copy it out of the function call.

Auto Trait Implementations§

§

impl<T> Freeze for UnsafePyLeaked<T>
where T: Freeze + ?Sized,

§

impl<T> RefUnwindSafe for UnsafePyLeaked<T>
where T: RefUnwindSafe + ?Sized,

§

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

§

impl<T> Sync for UnsafePyLeaked<T>
where T: Sync + ?Sized,

§

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

§

impl<T> UnwindSafe for UnsafePyLeaked<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.