pub trait IntoConstant: IntoValue + TypedValue {
// Provided method
fn into_constant(self) -> Constant { ... }
}
Expand description
Trait for types that can be converted into compile-time CEL constants.
This trait extends IntoValue
and TypedValue
to provide conversion
to Constant
values, which can be used for compile-time optimization
and type inference in CEL expressions.
§Compile-Time vs Runtime Values
- Constants: Known at compile-time, can be folded and optimized
- Values: Computed at runtime, require full evaluation
Constants enable the CEL compiler to:
- Perform constant folding optimizations
- Detect type errors earlier
- Generate more efficient evaluation code
- Support constant expression evaluation
§Automatic Implementation
This trait is automatically implemented for all types that implement
both IntoValue
and TypedValue
, providing a default conversion
from value to constant.
§Examples
§Basic Constant Creation
use cel_cxx::{IntoConstant, Constant};
// Primitive constants
let null_const = ().into_constant(); // Constant::Null
let bool_const = true.into_constant(); // Constant::Bool(true)
let int_const = 42i64.into_constant(); // Constant::Int(42)
let string_const = "hello".into_constant(); // Constant::String("hello".to_string())
§Usage in Environment Building
use cel_cxx::{Env, IntoConstant};
let env = Env::builder()
.define_constant("PI", 3.14159)?
.define_constant("APP_NAME", "MyApp")?
.define_constant("MAX_RETRIES", 5i64)?
.build()?;
// These constants can be used in expressions:
// "PI * radius * radius"
// "APP_NAME + ' v1.0'"
// "retries < MAX_RETRIES"
Provided Methods§
Sourcefn into_constant(self) -> Constant
fn into_constant(self) -> Constant
Convert this value into a compile-time CEL constant.
This method creates a Constant
from the value, which can be used
for compile-time optimizations and constant expression evaluation.
§Returns
A Constant
representing this value that can be used at compile-time.
§Implementation Note
The default implementation converts the value using IntoValue::into_value
and then wraps it as a constant. Custom implementations can provide
more efficient constant creation if needed.
§Examples
use cel_cxx::{IntoConstant, Constant};
let const_val = 42i64.into_constant();
let const_str = "hello".into_constant();
// Constants created successfully
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.