Skip to main content

SharedBoxRef

Struct SharedBoxRef 

Source
pub struct SharedBoxRef<T: ?Sized> { /* private fields */ }
Expand description

A Reference to SharedBox that can be sent to other threads.

Dropping a SharedBoxRef leaks the heap memory it points to. When panic-on-drop feature is enabled, dropping it will panic:

let mut b = manual_share::SharedBox::new(1);
b.borrow();

// forget SharedBox to make sure the panic is not caused by dropping it first.
std::mem::forget(b);

// panic here due to dropping SharedBoxRef

Use SharedBox::try_return to consume it without causing panic.

let mut b = manual_share::SharedBox::new(1);
let r = b.borrow();
b.try_return(r).unwrap();

Implementations§

Source§

impl<T: ?Sized> SharedBoxRef<T>

Source

pub fn get(&self) -> &T

Example usage:

let mut b = manual_share::SharedBox::new(42);
let r = b.borrow();
let value = *r.get();
assert_eq!(value, 42);

b.try_return(r).unwrap();

The reference got from this method has the same lifetime of the SharedBoxRef, which means it will be invalidated after SharedBoxRef is given back to SharedBox:

let mut b = manual_share::SharedBox::new(42);
let br = b.borrow();
let r = br.get();

b.try_return(br).unwrap();

// r is no longer valid here.
println!("{}", r);

Trait Implementations§

Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Drop for SharedBoxRef<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Sync> Send for SharedBoxRef<T>

SharedBoxRef is like &T, which only requires T: Sync to implement Send.

Source§

impl<T: Sync> Sync for SharedBoxRef<T>

Auto Trait Implementations§

§

impl<T> Freeze for SharedBoxRef<T>
where T: ?Sized,

§

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

§

impl<T> Unpin for SharedBoxRef<T>
where T: ?Sized,

§

impl<T> UnsafeUnpin for SharedBoxRef<T>
where T: ?Sized,

§

impl<T> UnwindSafe for SharedBoxRef<T>
where T: RefUnwindSafe + ?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>,

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.