Trait FromValue

Source
pub trait FromValue: Sized {
    type Output<'a>;

    // Required method
    fn from_value<'a>(
        value: &'a Value,
    ) -> Result<Self::Output<'a>, FromValueError>;
}
Expand description

Trait for converting CEL values into Rust types.

This trait enables conversion from CEL Value instances back to Rust types, with support for both owned and borrowed data through Generic Associated Types (GATs). It is the inverse operation of IntoValue.

§Generic Associated Types (GATs)

The Output<'a> associated type allows the trait to return either owned or borrowed data depending on the source value:

  • For String: Output<'a> = String (always owned)
  • For &str: Output<'a> = &'a str (borrowed from the source Value)
  • For primitives: Output<'a> = Self (copied)

§Implementation

Important: This trait should generally not be implemented manually. For most use cases:

  • Built-in types: Already have implementations with proper lifetime handling
  • Custom opaque types: Use the #[derive(Opaque)] macro
  • Manual implementation: Only for very specific requirements and advanced use cases

§Examples

§Using Built-in Conversions

use cel_cxx::{FromValue, Value};

let cel_string = Value::String("hello".to_string().into());
let rust_string = String::from_value(&cel_string)?;
assert_eq!(rust_string, "hello");

let cel_int = Value::Int(42);
let rust_int = i64::from_value(&cel_int)?;
assert_eq!(rust_int, 42);

§Zero-Copy String Conversion

use cel_cxx::{FromValue, Value};

let cel_string = Value::String("hello world".to_string().into());

// Convert to borrowed string slice (zero-copy)
let borrowed_str = <&str>::from_value(&cel_string)?;
assert_eq!(borrowed_str, "hello world");

Required Associated Types§

Source

type Output<'a>

The output type for the from_value method.

This type is parameterized by the lifetime of the value being converted. The lifetime is used to indicate whether the value is borrowed or owned.

Required Methods§

Source

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Attempts to convert a CEL Value into this type.

This method performs type checking and conversion from a CEL value to the target Rust type. It returns an error if the conversion is not possible.

§Arguments
  • value - The CEL value to convert
§Returns

A Result containing either the converted value or a FromValueError if the conversion failed.

§Errors

Returns FromValueError if:

  • The value type doesn’t match the expected type
  • The value format is invalid for the target type
  • Type constraints are not satisfied

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromValue for &bool

Source§

type Output<'a> = &'a bool

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for &f64

Source§

type Output<'a> = &'a f64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for &i64

Source§

type Output<'a> = &'a i64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for &str

Source§

type Output<'a> = &'a str

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for &u64

Source§

type Output<'a> = &'a u64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for &[u8]

Source§

type Output<'a> = &'a [u8]

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for bool

Source§

type Output<'a> = bool

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for f32

Source§

type Output<'a> = f32

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for f64

Source§

type Output<'a> = f64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for i16

Source§

type Output<'a> = i16

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for i32

Source§

type Output<'a> = i32

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for i64

Source§

type Output<'a> = i64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for isize

Source§

type Output<'a> = isize

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for u16

Source§

type Output<'a> = u16

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for u32

Source§

type Output<'a> = u32

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for u64

Source§

type Output<'a> = u64

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for ()

Source§

type Output<'a> = ()

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for usize

Source§

type Output<'a> = usize

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for Box<str>

Source§

type Output<'a> = Box<str>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for Box<[u8]>

Source§

type Output<'a> = Box<[u8]>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for String

Source§

type Output<'a> = String

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for Vec<u8>

Source§

type Output<'a> = Vec<u8>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl FromValue for SystemTime

Source§

type Output<'a> = SystemTime

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<K, V> FromValue for BTreeMap<K, V>
where K: FromMapKey + TypedMapKey, V: FromValue + TypedValue, for<'a> K::Output<'a>: Eq + Hash + Ord,

Source§

type Output<'a> = BTreeMap<<K as FromValue>::Output<'a>, <V as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<K, V> FromValue for LinkedList<(K, V)>
where K: FromMapKey + TypedMapKey, V: FromValue + TypedValue, for<'a> K::Output<'a>: Eq + Hash + Ord,

Source§

type Output<'a> = LinkedList<(<K as FromValue>::Output<'a>, <V as FromValue>::Output<'a>)>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<K, V> FromValue for VecDeque<(K, V)>
where K: FromMapKey + TypedMapKey, V: FromValue + TypedValue, for<'a> K::Output<'a>: Eq + Hash + Ord,

Source§

type Output<'a> = VecDeque<(<K as FromValue>::Output<'a>, <V as FromValue>::Output<'a>)>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<K, V> FromValue for Vec<(K, V)>
where K: FromMapKey + TypedMapKey, V: FromValue + TypedValue, for<'a> K::Output<'a>: Eq + Hash + Ord,

Source§

type Output<'a> = Vec<(<K as FromValue>::Output<'a>, <V as FromValue>::Output<'a>)>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<K, V> FromValue for HashMap<K, V>
where K: FromMapKey + TypedMapKey, V: FromValue + TypedValue, for<'a> K::Output<'a>: Eq + Hash + Ord,

Source§

type Output<'a> = HashMap<<K as FromValue>::Output<'a>, <V as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<T: FromValue + TypedValue> FromValue for Option<T>

Source§

type Output<'a> = Option<<T as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<T: FromValue + TypedValue> FromValue for LinkedList<T>

Source§

type Output<'a> = LinkedList<<T as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<T: FromValue + TypedValue> FromValue for VecDeque<T>

Source§

type Output<'a> = VecDeque<<T as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Source§

impl<T: FromValue + TypedValue> FromValue for Vec<T>

Source§

type Output<'a> = Vec<<T as FromValue>::Output<'a>>

Source§

fn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>

Implementors§