use std::mem; use std::any::{TypeId, Any}; use math::{Point, Isometry}; use shape::{Compound, TriMesh, Polyline, CompositeShape}; use inspection::{Repr, ReprDesc}; /// Gets the id associated with the `CompositeShape` trait. pub fn composite_shape_repr_id() -> TypeId { TypeId::of::<&CompositeShape>() } /// Converts a shape to a composite shape if possible. #[inline] pub fn maybe_repr_desc_as_composite_shape<'a, P: Any, M: Any>(desc: ReprDesc<'a, P, M>) -> Option<&'a (CompositeShape + 'a)> { if desc.repr_id() == composite_shape_repr_id::() { Some(unsafe { mem::transmute(desc.repr()) }) } else { None } } /// Converts a shape to a composite shape if possible. #[inline] pub fn maybe_as_composite_shape(g: &G) -> Option<&CompositeShape> where P: Any, M: Any, G: Repr { maybe_repr_desc_as_composite_shape(g.repr()) } macro_rules! impl_composite_shape_repr( ($t: ty) => { impl Repr for $t where P: Point, M: Isometry { #[inline(always)] fn repr(&self) -> ReprDesc { unsafe { ReprDesc::new( TypeId::of::<$t>(), TypeId::of::<&CompositeShape>(), mem::transmute(self as &CompositeShape) ) } } } } ); impl_composite_shape_repr!(Compound); impl_composite_shape_repr!(TriMesh

); impl_composite_shape_repr!(Polyline

);