Improved impl_cast and made all traits Sync + Send + 'static
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-10 23:51:22 +02:00
parent 65f76904dd
commit b54c9512b9
9 changed files with 77 additions and 66 deletions

View File

@@ -1,38 +1,47 @@
pub extern crate paste;
#[macro_export]
macro_rules! impl_setup {
() => {
pub trait As<T: ?Sized> {
fn consume(self: Box<Self>) -> Option<Box<T>>;
fn cast(&self) -> Option<&T>;
fn cast_mut(&mut self) -> Option<&mut T>;
}
};
}
#[macro_export]
macro_rules! impl_cast {
($base:ident, $trait:ident) => {
$crate::paste::paste! {
pub trait [< As $trait>] {
fn consume(self: Box<Self>) -> Option<Box<dyn $trait + Sync + Send>>;
fn cast(&self) -> Option<&dyn $trait>;
fn cast_mut(&mut self) -> Option<&mut dyn $trait>;
}
impl<T: $base> [< As $trait>] for T {
default fn consume(self: Box<Self>) -> Option<Box<dyn $trait + Sync + Send>> {
None
}
default fn cast(&self) -> Option<&dyn $trait> {
None
}
default fn cast_mut(&mut self) -> Option<&mut dyn $trait> {
None
}
}
impl<T: $base + $trait + Sync + Send + 'static> [< As $trait>] for T {
fn consume(self: Box<Self>) -> Option<Box<dyn $trait + Sync + Send>> {
impl<T: $base + $trait> As<dyn $trait> for T {
fn consume(self: Box<Self>) -> Option<Box<dyn $trait>> {
Some(self)
}
fn cast(&self) -> Option<&dyn $trait> {
Some(self)
}
fn cast_mut(&mut self) -> Option<&mut dyn $trait> {
fn cast_mut(&mut self) -> Option<&mut dyn $trait> {
Some(self)
}
}
impl<T: $base> As<dyn $trait> for T {
default fn consume(self: Box<Self>) -> Option<Box<dyn $trait>> {
None
}
default fn cast(&self) -> Option<&dyn $trait> {
None
}
default fn cast_mut(&mut self) -> Option<&mut dyn $trait> {
None
}
}
}
};
}