use crate::values::{core_values::native::DatexNative, value::Value};
pub trait TryClone {
fn try_clone(&self) -> Result<Value, ()>;
}
impl<T> TryClone for T {
default fn try_clone(&self) -> Result<Value, ()> {
Err(())
}
}
impl<T> TryClone for T
where
T: Clone + DatexNative,
{
default fn try_clone(&self) -> Result<Value, ()> {
Ok(Value::native(self.clone()))
}
}
#[cfg(test)]
mod tests {
use crate::{
prelude::*,
traits::{convert_value::ConvertValue, try_clone::TryClone},
};
#[test]
fn test_try_string() {
let value = "Hello, world!".to_string();
let cloned_value = value.try_clone().unwrap();
assert_eq!(cloned_value, "Hello, world!".to_string().to_value());
}
#[test]
fn test_try_unclonable_type() {
struct UnclonableType;
let value = UnclonableType;
let cloned_value = value.try_clone();
assert!(cloned_value.is_err());
}
}