X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/18518214c4609e182be5277b14c7c510c8fa9505..fdba2f45b9b9d6fa05be6a4d0849add42d94516e:/src/raw/any.rs diff --git a/src/raw/any.rs b/src/raw/any.rs new file mode 100644 index 0000000..d908040 --- /dev/null +++ b/src/raw/any.rs @@ -0,0 +1,105 @@ +use std::fmt; +use std::any::Any as StdAny; + +#[cfg(feature = "clone")] +#[doc(hidden)] +pub trait CloneToAny { + /// Clone `self` into a new `Box` object. + fn clone_to_any(&self) -> Box; +} + +#[cfg(feature = "clone")] +impl CloneToAny for T { + fn clone_to_any(&self) -> Box { + Box::new(self.clone()) + } +} + +macro_rules! define_any { + (#[$m:meta] $t:item $i:item) => { + /// A type to emulate dynamic typing. + /// + /// Every suitable type with no non-`'static` references implements `Any`. See the + /// [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for more + /// details on `Any` in general. + /// + /// This trait is not `std::any::Any` but rather a type extending that for this library’s + /// purposes; most specifically, there are a couple of Cargo features that can be enabled + /// which will alter the constraints of what comprises a suitable type: + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + #[cfg_attr(feature = "clone", doc = " ")] + #[cfg_attr(not(feature = "clone"), doc = " ")] + /// + /// + /// + /// + #[cfg_attr(feature = "concurrent", doc = " ")] + #[cfg_attr(not(feature = "concurrent"), doc = " ")] + /// + /// + ///
Feature nameAdditional boundsEnabled in these docs?
cloneCloneYesNo
concurrentSend + SyncYesNo
+ #[$m] $t + #[$m] $i + } +} + +define_any! { + #[cfg(all(not(feature = "clone"), not(feature = "concurrent")))] + pub trait Any: StdAny { } + impl Any for T { } +} + +define_any! { + #[cfg(all(feature = "clone", not(feature = "concurrent")))] + pub trait Any: StdAny + CloneToAny { } + impl Any for T { } +} + +define_any! { + #[cfg(all(not(feature = "clone"), feature = "concurrent"))] + pub trait Any: StdAny + Send + Sync { } + impl Any for T { } +} + +define_any! { + #[cfg(all(feature = "clone", feature = "concurrent"))] + pub trait Any: StdAny + CloneToAny + Send + Sync { } + impl Any for T { } +} + +#[cfg(feature = "clone")] +impl Clone for Box { + fn clone(&self) -> Box { + (**self).clone_to_any() + } +} + +impl<'a> fmt::Debug for &'a Any { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("&Any") + } +} + +impl<'a> fmt::Debug for Box { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("Box") + } +}