Struct auxtools::WeakValue

source ·
pub struct WeakValue { /* private fields */ }
Expand description

A weak reference to some datum or atom in the game.

Normal Values are not safe to move between threads. Using methods like Value::set or Value::call can at best cause undefined behavior, at worst crash the server.

A way to bypass that limitation is to store a raw value and use Value::from_raw on the main thread to actually work with it. However, if that Value is deleted, your stored value will point to another datum or to simply nothing.

This struct serves to solve the latter problem. You can use Value::as_weak to create a weak reference to it. The reference can be stored in global structures or passed to other threads. You can then return it to the main thread as needed, and call WeakValue::upgrade to turn it back into a real Value. If the datum pointed to was deleted in the meantime, upgrade will return None, otherwise you get your datum back.

However, this struct is not entirely thread safe, since you can WeakValue::upgrade on another thread and invoke undefined behavior with the resulting Value. So, don’t do that.

Using this struct requires all datums to have a __auxtools_weakref_id variable.

Example

let weakref = thing.as_weak()?;
callbacks.set(some_id, weakref);

... some proc calls later ...

let weakref = callbacks.get(some_id);
if let Some(thing) = weakref.upgrade() {
	thing.call("callback", &[])?;
}

Implementations§

source§

impl WeakValue

source

pub fn upgrade(&self) -> Option<Value>

Converts the stored raw value to a full fledged Value and checks if it has been deleted in the meantime.

source

pub fn upgrade_or_null(&self) -> Value

Same as WeakValue::upgrade but returns a null if the datum was deleted, so you can pass it straight into DM.

Trait Implementations§

source§

impl Clone for WeakValue

source§

fn clone(&self) -> WeakValue

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Copy for WeakValue

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.