sunflower/src/value/custom.rs

90 lines
1.8 KiB
Rust

use crate::function::FunctionMap;
use super::Value;
fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
std::any::type_name::<T>()
}
pub trait CustomValue: std::fmt::Debug + BoxClone {
/// Cursed workaround. Since every type has to implement this
/// method, we can get a handle back to the type.
fn as_any(&self) -> &dyn std::any::Any;
fn value_kind(&self) -> &'static str {
type_name_of_val(self)
}
fn format(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
fn eq(&self, _: &Value) -> bool {
false
}
fn partial_cmp(&self, _: &Value) -> Option<std::cmp::Ordering> {
None
}
fn functions(&self) -> Option<FunctionMap> {
None
}
fn to_bool(&self) -> bool {
true
}
fn add(&self, _: &Value) -> Value {
Value::default()
}
fn sub(&self, _: &Value) -> Value {
Value::default()
}
fn mul(&self, _: &Value) -> Value {
Value::default()
}
fn div(&self, _: &Value) -> Value {
Value::default()
}
fn rem(&self, _: &Value) -> Value {
Value::default()
}
}
impl PartialEq<Value> for dyn CustomValue {
fn eq(&self, other: &Value) -> bool {
self.eq(other)
}
}
impl PartialOrd<Value> for dyn CustomValue {
fn partial_cmp(&self, other: &Value) -> Option<std::cmp::Ordering> {
self.partial_cmp(other)
}
}
pub trait BoxClone {
fn clone_box(&self) -> Box<dyn CustomValue>;
}
impl<T> BoxClone for T
where
T: 'static + CustomValue + Clone,
{
fn clone_box(&self) -> Box<dyn CustomValue> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn CustomValue> {
fn clone(&self) -> Self {
self.clone_box()
}
}