Struct RefOnce

Source
pub struct RefOnce<'a, T: ?Sized> { /* private fields */ }

Implementations§

Source§

impl<'a, T> RefOnce<'a, T>

Source

pub fn new(value: T, slot: &'a mut MaybeUninit<T>) -> Self

Examples found in repository?
examples/ref_once.rs (lines 13-20)
5fn main() {
6    let mut a = vec![1, 2, 3];
7    let mut x = 0;
8
9    let mut slots = (MaybeUninit::uninit(), MaybeUninit::uninit());
10
11    scope_lock::lock_scope(|e| {
12        thread::spawn({
13            let f = e.fn_(RefOnce::new(
14                |()| {
15                    println!("hello from the first scoped thread");
16                    // We can borrow `a` here.
17                    dbg!(&a);
18                },
19                &mut slots.0,
20            ));
21            move || f(())
22        });
23        thread::spawn({
24            let mut f = e.fn_mut(RefOnce::new(
25                |()| {
26                    println!("hello from the second scoped thread");
27                    // We can even mutably borrow `x` here,
28                    // because no other threads are using it.
29                    x += a[0] + a[2];
30                },
31                &mut slots.1,
32            ));
33            move || f(())
34        });
35        println!("hello from the main thread");
36    });
37
38    // After the scope, we can modify and access our variables again:
39    a.push(4);
40    assert_eq!(x, a.len());
41}
Source

pub fn into_inner(this: Self) -> T

Source§

impl<'a, T: ?Sized> RefOnce<'a, T>

Source

pub fn into_raw(this: Self) -> *mut T

Essentially leaks object as a pointer until the original RefOnce is restored via Self::from_raw.

Source

pub unsafe fn from_raw(ptr: *mut T) -> Self

Convert pointer returned from Self::into_raw back into RefOnce.

§Safety

ptr must have been returned from Self::into_raw. New lifetime argument 'a of RefOnce should not outlive old lifetime not to cause any undefined behaviour.

Trait Implementations§

Source§

impl<T: ?Sized> Borrow<T> for RefOnce<'_, T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: ?Sized> BorrowMut<T> for RefOnce<'_, T>

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T: ?Sized> Deref for RefOnce<'_, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: ?Sized> DerefMut for RefOnce<'_, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized> Drop for RefOnce<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> PointerIntoInner for RefOnce<'_, T>

Source§

fn into_inner(self) -> Self::Pointee

Source§

impl<'a, T> PointerLike for RefOnce<'a, T>

Source§

type Pointee = T

The type our pointer points at.
Source§

fn into_ptr(self) -> *mut Self::Pointee

Convert smart pointer into a raw pointer, possibly leaking it. Read more
Source§

unsafe fn from_ptr(ptr: *mut Self::Pointee) -> Self

Convert a raw pointer back into a smart pointer. Read more
Source§

impl<T> PointerDeref for RefOnce<'_, T>

Source§

impl<T> PointerDerefMut for RefOnce<'_, T>

Source§

impl<T> PointerPinUnforgotten for RefOnce<'_, T>

Note that RefOnce::into_pin implementation, mimicking the Box::into_pin would be unsound because RefOnce stores object within the std::mem::MaybeUninit slot allocated somewhere (including on a stack) which would allow us to deallocate pinned object without first calling drop on it, thus violating the pin’s drop guarantee. However we can safely assume that this pointer won’t be forgotten and drop will “eventually” run, so we are safe here.

Auto Trait Implementations§

§

impl<'a, T> Freeze for RefOnce<'a, T>
where T: ?Sized,

§

impl<'a, T> RefUnwindSafe for RefOnce<'a, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'a, T> Send for RefOnce<'a, T>
where T: Send + ?Sized,

§

impl<'a, T> Sync for RefOnce<'a, T>
where T: Sync + ?Sized,

§

impl<'a, T> Unpin for RefOnce<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for RefOnce<'a, T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.