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§
Required Methods§
Sourcefn from_value<'a>(value: &'a Value) -> Result<Self::Output<'a>, FromValueError>
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.