pub trait Set: PartialReflect {
// Required methods
fn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect>;
fn len(&self) -> usize;
fn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_>;
fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>;
fn clone_dynamic(&self) -> DynamicSet;
fn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool;
fn remove(&mut self, value: &dyn PartialReflect) -> bool;
fn contains(&self, value: &dyn PartialReflect) -> bool;
// Provided method
fn is_empty(&self) -> bool { ... }
}
Expand description
A trait used to power set-like operations via reflection.
Sets contain zero or more entries of a fixed type, and correspond to types
like HashSet
and BTreeSet
.
The order of these entries is not guaranteed by this trait.
§Hashing and equality
All values are expected to return a valid hash value from PartialReflect::reflect_hash
and be
comparable using PartialReflect::reflect_partial_eq
.
If using the #[derive(Reflect)]
macro, this can be done by adding
#[reflect(Hash, PartialEq)]
to the entire struct or enum.
The ordering is expected to be total, that is as if the reflected type implements the Eq
trait.
This is true even for manual implementors who do not hash or compare values,
as it is still relied on by DynamicSet
.
§Example
use bevy_reflect::{PartialReflect, Set};
use bevy_utils::HashSet;
let foo: &mut dyn Set = &mut HashSet::<u32>::new();
foo.insert_boxed(Box::new(123_u32));
assert_eq!(foo.len(), 1);
let field: &dyn PartialReflect = foo.get(&123_u32).unwrap();
assert_eq!(field.try_downcast_ref::<u32>(), Some(&123_u32));
Required Methods§
Sourcefn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect>
fn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect>
Returns a reference to the value.
If no value is contained, returns None
.
Sourcefn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_>
fn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_>
Returns an iterator over the values of the set.
Sourcefn drain(&mut self) -> Vec<Box<dyn PartialReflect>>
fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>
Drain the values of this set to get a vector of owned values.
After calling this function, self
will be empty.
Sourcefn clone_dynamic(&self) -> DynamicSet
fn clone_dynamic(&self) -> DynamicSet
Clones the set, producing a DynamicSet
.
Sourcefn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool
fn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool
Inserts a value into the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
Sourcefn remove(&mut self, value: &dyn PartialReflect) -> bool
fn remove(&mut self, value: &dyn PartialReflect) -> bool
Removes a value from the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
Sourcefn contains(&self, value: &dyn PartialReflect) -> bool
fn contains(&self, value: &dyn PartialReflect) -> bool
Checks if the given value is contained in the set