macro_rules! impl_custom_bounds {
($bounds:ident, $dyn:ident, $trait_name:ident) => { ... };
($bounds:ident, $dyn:ident, $trait_name:ident, $(+ $marker_traits:ident)*) => { ... };
}Expand description
Implements Bounds & HasBounds traits for $bounds using $dyn trait object and wrapping $trait_name.
Together with impl_dyn_trait_wrapper macro, it serves as simple way to implement custom Bounds.
For example you may define requirement for custom trait Component. Firstly, you need to define trait object
that will be both Any and Component. Easiest way to do it is to use [impl_dyn_trait_wrapper].
Then you need to define bounds struct and use impl_custom_bounds macro.
use std::any::Any;
use typedmap::{impl_custom_bounds, impl_dyn_trait_wrapper};
// Trait that you require
trait Component {}
// Requirement bounds used in TypedMap or TypedDashMap
struct CustomBounds;
impl_dyn_trait_wrapper!(DynComponent, Component);
impl_custom_bounds!(CustomBounds, DynComponent, Component);impl_custom_bounds expands to:
impl ::typedmap::bounds::Bounds for CustomBounds {
type KeyContainer = dyn ::typedmap::bounds::ContainerWithHash<CustomBounds>;
type Container = dyn DynComponent;
fn as_any(this: &Self::Container) -> &dyn ::std::any::Any {
this.as_any()
}
fn as_any_mut(this: &mut Self::Container) -> &mut dyn ::std::any::Any {
this.as_mut_any()
}
fn as_any_box(this: Box<Self::Container>) -> Box<dyn ::std::any::Any> {
this.as_any_box()
}
}
impl<T: Component + ::std::any::Any> ::typedmap::bounds::HasBounds<T> for CustomBounds {
fn cast_box(this: Box<T>) -> Box<Self::Container> {
this
}
fn as_ref(this: &T) -> &Self::Container {
this
}
fn as_mut(this: &mut T) -> &mut Self::Container {
this
}
fn cast_key_box(this: Box<T>) -> Box<Self::KeyContainer> where T: 'static + Sized + ::std::hash::Hash + ::std::cmp::Eq {
this
}
}You can also add marker traits as requrement, for example:
impl_custom_bounds!(CustomBounds, DynComponent, Component, +Send+Sync);which expands to:
impl ::typedmap::bounds::Bounds for CustomBounds {
type KeyContainer = dyn ::typedmap::bounds::ContainerWithHash<CustomBounds> + Send + Sync;
type Container = dyn DynComponent + Send + Sync;
fn as_any(this: &Self::Container) -> &dyn ::std::any::Any {
this.as_any()
}
fn as_any_mut(this: &mut Self::Container) -> &mut dyn ::std::any::Any {
this.as_mut_any()
}
fn as_any_box(this: Box<Self::Container>) -> Box<dyn ::std::any::Any> {
this.as_any_box()
}
}
impl<T: Component + ::std::any::Any + Send + Sync> ::typedmap::bounds::HasBounds<T> for CustomBounds {
fn cast_box(this: Box<T>) -> Box<Self::Container> {
this
}
fn as_ref(this: &T) -> &Self::Container {
this
}
fn as_mut(this: &mut T) -> &mut Self::Container {
this
}
fn cast_key_box(this: Box<T>) -> Box<Self::KeyContainer> where T: 'static + Sized + ::std::hash::Hash + ::std::cmp::Eq {
this
}
}