automation_rs/automation_cast/src/lib.rs
Dreaded_X 668f13863a
Replaced impl_cast with a new and improved trait
With this trait the impl_cast macros are no longer needed, simplifying
everything.
This commit also improved how the actual casting itself is handled.
2024-05-05 02:01:13 +02:00

38 lines
606 B
Rust

#![allow(incomplete_features)]
#![feature(specialization)]
#![feature(unsize)]
use std::marker::Unsize;
pub trait Cast<P: ?Sized> {
fn cast(&self) -> Option<&P>;
fn cast_mut(&mut self) -> Option<&mut P>;
}
impl<D, P> Cast<P> for D
where
P: ?Sized,
{
default fn cast(&self) -> Option<&P> {
None
}
default fn cast_mut(&mut self) -> Option<&mut P> {
None
}
}
impl<D, P> Cast<P> for D
where
D: Unsize<P>,
P: ?Sized,
{
fn cast(&self) -> Option<&P> {
Some(self)
}
fn cast_mut(&mut self) -> Option<&mut P> {
Some(self)
}
}