use alloc::boxed::Box;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use crate::{FromType, Reflect};
#[derive(Clone)]
pub struct ReflectDefault {
default: fn() -> Box<dyn Reflect>,
}
impl ReflectDefault {
pub fn default(&self) -> Box<dyn Reflect> {
(self.default)()
}
}
impl<T: Reflect + Default> FromType<T> for ReflectDefault {
fn from_type() -> Self {
ReflectDefault {
default: || Box::<T>::default(),
}
}
}
#[derive(Clone)]
pub struct ReflectAdd {
pub add: fn(
Box<dyn Reflect>,
Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)>,
}
impl ReflectAdd {
pub fn add(
&self,
a: Box<dyn Reflect>,
b: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)> {
(self.add)(a, b)
}
}
impl<T: Reflect + Add<Output: Reflect>> FromType<T> for ReflectAdd {
fn from_type() -> Self {
ReflectAdd {
add: |a: Box<dyn Reflect>, b: Box<dyn Reflect>| {
let (a, b) = match (a.downcast::<T>(), b.downcast::<T>()) {
(Ok(a), Ok(b)) => (a, b),
(a, b) => {
let a = a.map(|a| a as Box<dyn Reflect>).unwrap_or_else(|e| e);
let b = b.map(|b| b as Box<dyn Reflect>).unwrap_or_else(|e| e);
return Err((a, b));
}
};
Ok(Box::new(*a + *b))
},
}
}
}
#[derive(Clone)]
pub struct ReflectSub {
pub sub: fn(
Box<dyn Reflect>,
Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)>,
}
impl ReflectSub {
pub fn sub(
&self,
a: Box<dyn Reflect>,
b: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)> {
(self.sub)(a, b)
}
}
impl<T: Reflect + Sub<Output: Reflect>> FromType<T> for ReflectSub {
fn from_type() -> Self {
ReflectSub {
sub: |a: Box<dyn Reflect>, b: Box<dyn Reflect>| {
let (a, b) = match (a.downcast::<T>(), b.downcast::<T>()) {
(Ok(a), Ok(b)) => (a, b),
(a, b) => {
let a = a.map(|a| a as Box<dyn Reflect>).unwrap_or_else(|e| e);
let b = b.map(|b| b as Box<dyn Reflect>).unwrap_or_else(|e| e);
return Err((a, b));
}
};
Ok(Box::new(*a - *b))
},
}
}
}
#[derive(Clone)]
pub struct ReflectMul {
pub mul: fn(
Box<dyn Reflect>,
Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)>,
}
impl ReflectMul {
pub fn mul(
&self,
a: Box<dyn Reflect>,
b: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)> {
(self.mul)(a, b)
}
}
impl<T: Reflect + Mul<Output: Reflect>> FromType<T> for ReflectMul {
fn from_type() -> Self {
ReflectMul {
mul: |a: Box<dyn Reflect>, b: Box<dyn Reflect>| {
let (a, b) = match (a.downcast::<T>(), b.downcast::<T>()) {
(Ok(a), Ok(b)) => (a, b),
(a, b) => {
let a = a.map(|a| a as Box<dyn Reflect>).unwrap_or_else(|e| e);
let b = b.map(|b| b as Box<dyn Reflect>).unwrap_or_else(|e| e);
return Err((a, b));
}
};
Ok(Box::new(*a * *b))
},
}
}
}
#[derive(Clone)]
pub struct ReflectDiv {
pub div: fn(
Box<dyn Reflect>,
Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)>,
}
impl ReflectDiv {
pub fn div(
&self,
a: Box<dyn Reflect>,
b: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)> {
(self.div)(a, b)
}
}
impl<T: Reflect + Div<Output: Reflect>> FromType<T> for ReflectDiv {
fn from_type() -> Self {
ReflectDiv {
div: |a: Box<dyn Reflect>, b: Box<dyn Reflect>| {
let (a, b) = match (a.downcast::<T>(), b.downcast::<T>()) {
(Ok(a), Ok(b)) => (a, b),
(a, b) => {
let a = a.map(|a| a as Box<dyn Reflect>).unwrap_or_else(|e| e);
let b = b.map(|b| b as Box<dyn Reflect>).unwrap_or_else(|e| e);
return Err((a, b));
}
};
Ok(Box::new(*a / *b))
},
}
}
}
#[derive(Clone)]
pub struct ReflectRem {
pub rem: fn(
Box<dyn Reflect>,
Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)>,
}
impl ReflectRem {
pub fn rem(
&self,
a: Box<dyn Reflect>,
b: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, (Box<dyn Reflect>, Box<dyn Reflect>)> {
(self.rem)(a, b)
}
}
impl<T: Reflect + Rem<Output: Reflect>> FromType<T> for ReflectRem {
fn from_type() -> Self {
ReflectRem {
rem: |a: Box<dyn Reflect>, b: Box<dyn Reflect>| {
let (a, b) = match (a.downcast::<T>(), b.downcast::<T>()) {
(Ok(a), Ok(b)) => (a, b),
(a, b) => {
let a = a.map(|a| a as Box<dyn Reflect>).unwrap_or_else(|e| e);
let b = b.map(|b| b as Box<dyn Reflect>).unwrap_or_else(|e| e);
return Err((a, b));
}
};
Ok(Box::new(*a % *b))
},
}
}
}
#[derive(Clone)]
pub struct ReflectAddAssign {
pub add_assign: fn(&mut dyn Reflect, Box<dyn Reflect>) -> Result<(), Option<Box<dyn Reflect>>>,
}
impl ReflectAddAssign {
pub fn add_assign(
&self,
a: &mut dyn Reflect,
b: Box<dyn Reflect>,
) -> Result<(), Option<Box<dyn Reflect>>> {
(self.add_assign)(a, b)
}
}
impl<T: Reflect + AddAssign> FromType<T> for ReflectAddAssign {
fn from_type() -> Self {
ReflectAddAssign {
add_assign: |a: &mut dyn Reflect, b: Box<dyn Reflect>| {
let Some(a) = a.downcast_mut::<T>() else {
return Err(None);
};
let b = match b.downcast::<T>() {
Ok(b) => b,
Err(b) => return Err(Some(b)),
};
*a += *b;
Ok(())
},
}
}
}
#[derive(Clone)]
pub struct ReflectSubAssign {
pub sub_assign: fn(&mut dyn Reflect, Box<dyn Reflect>) -> Result<(), Option<Box<dyn Reflect>>>,
}
impl ReflectSubAssign {
pub fn sub_assign(
&self,
a: &mut dyn Reflect,
b: Box<dyn Reflect>,
) -> Result<(), Option<Box<dyn Reflect>>> {
(self.sub_assign)(a, b)
}
}
impl<T: Reflect + SubAssign> FromType<T> for ReflectSubAssign {
fn from_type() -> Self {
ReflectSubAssign {
sub_assign: |a: &mut dyn Reflect, b: Box<dyn Reflect>| {
let Some(a) = a.downcast_mut::<T>() else {
return Err(None);
};
let b = match b.downcast::<T>() {
Ok(b) => b,
Err(b) => return Err(Some(b)),
};
*a -= *b;
Ok(())
},
}
}
}
#[derive(Clone)]
pub struct ReflectMulAssign {
pub mul_assign: fn(&mut dyn Reflect, Box<dyn Reflect>) -> Result<(), Option<Box<dyn Reflect>>>,
}
impl ReflectMulAssign {
pub fn mul_assign(
&self,
a: &mut dyn Reflect,
b: Box<dyn Reflect>,
) -> Result<(), Option<Box<dyn Reflect>>> {
(self.mul_assign)(a, b)
}
}
impl<T: Reflect + MulAssign> FromType<T> for ReflectMulAssign {
fn from_type() -> Self {
ReflectMulAssign {
mul_assign: |a: &mut dyn Reflect, b: Box<dyn Reflect>| {
let Some(a) = a.downcast_mut::<T>() else {
return Err(None);
};
let b = match b.downcast::<T>() {
Ok(b) => b,
Err(b) => return Err(Some(b)),
};
*a *= *b;
Ok(())
},
}
}
}
#[derive(Clone)]
pub struct ReflectDivAssign {
pub div_assign: fn(&mut dyn Reflect, Box<dyn Reflect>) -> Result<(), Option<Box<dyn Reflect>>>,
}
impl ReflectDivAssign {
pub fn div_assign(
&self,
a: &mut dyn Reflect,
b: Box<dyn Reflect>,
) -> Result<(), Option<Box<dyn Reflect>>> {
(self.div_assign)(a, b)
}
}
impl<T: Reflect + DivAssign> FromType<T> for ReflectDivAssign {
fn from_type() -> Self {
ReflectDivAssign {
div_assign: |a: &mut dyn Reflect, b: Box<dyn Reflect>| {
let Some(a) = a.downcast_mut::<T>() else {
return Err(None);
};
let b = match b.downcast::<T>() {
Ok(b) => b,
Err(b) => return Err(Some(b)),
};
*a /= *b;
Ok(())
},
}
}
}
#[derive(Clone)]
pub struct ReflectRemAssign {
pub rem_assign: fn(&mut dyn Reflect, Box<dyn Reflect>) -> Result<(), Option<Box<dyn Reflect>>>,
}
impl ReflectRemAssign {
pub fn rem_assign(
&self,
a: &mut dyn Reflect,
b: Box<dyn Reflect>,
) -> Result<(), Option<Box<dyn Reflect>>> {
(self.rem_assign)(a, b)
}
}
impl<T: Reflect + RemAssign> FromType<T> for ReflectRemAssign {
fn from_type() -> Self {
ReflectRemAssign {
rem_assign: |a: &mut dyn Reflect, b: Box<dyn Reflect>| {
let Some(a) = a.downcast_mut::<T>() else {
return Err(None);
};
let b = match b.downcast::<T>() {
Ok(b) => b,
Err(b) => return Err(Some(b)),
};
*a %= *b;
Ok(())
},
}
}
}