pub struct UserData {
    inner: OnceCell<UserDataInner>,
}
Expand description

A wrapper for user data, able to store any type, and correctly handling access from a wrong thread

Fields§

§inner: OnceCell<UserDataInner>

Implementations§

source§

impl UserData

source

pub const fn new() -> UserData

Create a new UserData instance

source

pub fn set<T, F>(&self, f: F)where T: Any + 'static, F: FnOnce() -> T,

Sets the UserData to a given value

The provided closure is called to init the UserData, does nothing is the UserData had already been set.

source

pub fn set_threadsafe<T, F>(&self, f: F)where T: Any + Send + Sync + 'static, F: FnOnce() -> T,

Sets the UserData to a given threadsafe value

The provided closure is called to init the UserData, does nothing is the UserData had already been set.

source

pub fn get<T>(&self) -> Option<&T>where T: 'static,

Attempt to access the wrapped user data

Will return None if either:

  • The requested type T does not match the type used for construction
  • This UserData has been created using the non-threadsafe variant and access is attempted from an other thread than the one it was created on

Trait Implementations§

source§

impl Debug for UserData

source§

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

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

impl Drop for UserData

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for UserData

source§

impl Sync for UserData

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.