Enum hexchat_api::UserData
source · pub enum UserData {
BoxedData(Box<RefCell<dyn Any>>),
SharedData(Rc<RefCell<dyn Any>>),
SyncData(Arc<RwLock<dyn Any>>),
NoData,
}
Expand description
Represents the user data that is provided to callbacks when they’re invoked.
A UserData
object is registered with the callback using one of the
hook commands. The user data can be virtually any type that implements the
Any
trait capable of being downcast to its original type. There are
four variants for the user data. Which one to use depends on how the
callback user data is shared. If the data is unique to one callback, then
BoxedData
should be enough. For single threaded sharing among more than
one callback, SharedData
will do the trick. If, for any odd sort of reason
threading somehow becomes relevant to the user data object, SyncData
would
be the variant to use.
The class has 4 creation functions - one for each variant. And a convenience
function that takes a closure that accepts the UserData
’s wrapped value
as a parameter. A good way to avoid all the dereferencing it takes to
access the interior objects nested inside the sharing and mutability host
objects.
Variants
BoxedData
- Can hold user data that only one callback uses.SharedData
- Can allow more than one callback or other code to hold a cloned copy that references the same user data.SyncData
- LikeSharedData
, but uses the sync objects internally to allow the user data to be shared among threads.NoData
- Represents the absence of data.
Variants§
Implementations§
source§impl UserData
impl UserData
sourcepub fn apply<D: 'static, F, R>(&self, f: F) -> Rwhere
F: FnOnce(&D) -> R,
pub fn apply<D: 'static, F, R>(&self, f: F) -> Rwhere F: FnOnce(&D) -> R,
Applies the given function to the wrapped object inside a UserData
object. The type of the wrapped data has to be compatible with the
type of the function’s single parameter, or the downcast won’t work
and apply()
will panic.
Arguments
f
- A function, or closure, to invoke with the user data, free of any wrappers. The format of the function needs to beFn(&T) -> R
, whereD
is the type of the user data; andR
is the return type that gets wrapped in anOption
and returned byapply()
.
Returns
- Returns the return value of function
f
if the downcast is successful.
Trait Implementations§
source§impl Clone for UserData
impl Clone for UserData
source§fn clone(&self) -> Self
fn clone(&self) -> Self
The clone operation for UserData
allows each variant to be cloned,
except for BoxedData
. The reason BoxedData
is prohibited is to
deter sharing a box between callbacks, as that’s not what
a box is meant to be used for. One of the shared variants is more
appropriate to share access to user data.
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more