pub struct CloneBounds;Expand description
CloneBounds is used to bound keys or values stored in hashmap to cloneable types.
use std::sync::Arc;
use typedmap::{TypedMap, TypedMapKey};
use typedmap::clone::{clone_box, CloneBounds};
let mut map: TypedMap::<(), CloneBounds, CloneBounds, _> = TypedMap::default();
#[derive(PartialEq, Eq, Hash, Debug)]
struct Key;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct CloneKey;
impl TypedMapKey for Key {
type Value = u32;
}
impl TypedMapKey for CloneKey {
type Value = u32;
}
map.insert(CloneKey, 32);
// Won't compile, because Key is not Clone
// map.insert(Key, 32);
for elem in map.iter() {
let cloned = clone_box(elem.key_container_ref());
let key = *cloned.as_any_box().downcast::<CloneKey>().unwrap();
assert_eq!(key, CloneKey);
let cloned = clone_box(elem.value_container_ref());
let value = *cloned.as_any_box().downcast::<u32>().unwrap();
assert_eq!(value, 32);
}Trait Implementations§
Source§impl Bounds for CloneBounds
impl Bounds for CloneBounds
Source§type KeyContainer = dyn ContainerWithHashAndClone<CloneBounds>
type KeyContainer = dyn ContainerWithHashAndClone<CloneBounds>
Type used to store keys with those bounds. It should be
dyn ContainerWithHash<Self> + Marker traits Read moreSource§type Container = dyn CloneAny
type Container = dyn CloneAny
Type used to store values that fulfill specified bounds (e.g.
dyn Any + Send + Sync or
dyn CloneAny)fn as_any(this: &Self::Container) -> &dyn Any
fn as_any_mut(this: &mut Self::Container) -> &mut dyn Any
fn as_any_box(this: Box<Self::Container>) -> Box<dyn Any>
Source§impl<T: CloneAny + Any> HasBounds<T> for CloneBounds
impl<T: CloneAny + Any> HasBounds<T> for CloneBounds
Source§fn cast_key_box(this: Box<T>) -> Box<Self::KeyContainer>
fn cast_key_box(this: Box<T>) -> Box<Self::KeyContainer>
Converts from
Box<T> to Box<KeyContainer>Source§fn box_key(this: T) -> Box<Self::KeyContainer>
fn box_key(this: T) -> Box<Self::KeyContainer>
Boxes key of type
T as Box<KeyContainer>Source§fn box_value(this: T) -> Box<Self::Container>where
Self: Sized,
fn box_value(this: T) -> Box<Self::Container>where
Self: Sized,
Boxes value of type
T as Box<Container>Source§fn downcast_ref(this: &Self::Container) -> Option<&T>where
Self: 'static + Sized,
fn downcast_ref(this: &Self::Container) -> Option<&T>where
Self: 'static + Sized,
Attempts to downcast
&Container to &TAuto Trait Implementations§
impl Freeze for CloneBounds
impl RefUnwindSafe for CloneBounds
impl Send for CloneBounds
impl Sync for CloneBounds
impl Unpin for CloneBounds
impl UnwindSafe for CloneBounds
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more