sindra/
value.rs

1//! Value and associated traits for run-time in-memory values.
2use std::fmt::{Debug, Display};
3use Type;
4
5/// Value trait (empty, just a collection of other traits). Used to specify the required traits
6/// for any stored value in a programming language.
7///
8/// Typically, the Value trait is implemented by an enum which contains the possible values that
9/// can occur. This is not strictly necessary, though.
10pub trait Value: Clone + Debug + PartialEq + Display {}
11
12/// Trait for type coercions for values.
13pub trait Coerce<T: Type> {
14    /// Coerce a value to target type, depending on the value of the option.
15    fn coerce(self, dest_ty: Option<T>) -> Self;
16}
17
18/// Trait for type casting of values.
19pub trait Cast<T: Type> {
20    /// Cast a value to the target type (should return unchanged value if case is not possible).
21    fn cast(self, dest_ty: T) -> Self;
22}
23
24/// Trait for extracting rust value (of type T) from Value type.
25pub trait Extract<T>: Value {
26    /// Extract the value, returning an `Err` if the Value is of the wrong variant.
27    fn extract(&self) -> Result<T, String>;
28}