pub trait Opaque:
DynClone
+ Debug
+ Display
+ Send
+ Sync
+ Sealed {
// Required method
fn opaque_type(&self) -> OpaqueType;
}
Expand description
Trait for opaque types that can be stored in CEL values.
This trait allows custom Rust types to be embedded in CEL values as opaque objects. Opaque types are useful for:
- Foreign data structures: Types from other libraries
- Complex business objects: Domain-specific data models
- Native performance: Keep computations in native Rust
- Type safety: Maintain strong typing across CEL boundaries
§Opaque Type Characteristics
Opaque values in CEL:
- Cannot be directly manipulated by CEL expressions
- Can be passed between functions that understand the type
- Support method calls through registered member functions
- Maintain their Rust type identity and behavior
§Required Traits
Implementing Opaque
requires several standard traits:
Clone
: For value duplicationDebug
: For debugging and error messagesstd::fmt::Display
: For string representation- [
Send + Sync
]: For thread safety dyn_clone::DynClone
: For trait object cloning
§Examples
§Basic Opaque Type
use cel_cxx::{Opaque, Value, IntoValue};
#[derive(Opaque, Debug, Clone, PartialEq)]
#[cel_cxx(display)]
struct UserId(u64);
// All necessary traits are automatically implemented by the derive macro
// Usage in CEL values
let user_id = UserId(12345);
let value = user_id.into_value();
§Integration with Functions
use cel_cxx::*;
#[derive(Opaque, Debug, Clone, PartialEq)]
#[cel_cxx(display)]
struct BankAccount {
balance: f64,
account_number: String,
}
impl BankAccount {
fn balance(&self) -> f64 {
self.balance
}
fn can_withdraw(&self, amount: f64) -> bool {
amount <= self.balance
}
fn account_number(&self) -> &str {
&self.account_number
}
}
// Register methods for the opaque type
let env = Env::builder()
.register_member_function("balance", BankAccount::balance)?
.register_member_function("can_withdraw", BankAccount::can_withdraw)?
.register_member_function("account_number", BankAccount::account_number)?
.build()?;
§Type Erasure and Downcasting
Opaque values support safe downcasting:
use cel_cxx::{Value, Opaque, IntoValue};
#[derive(Opaque, Debug, Clone, PartialEq)]
#[cel_cxx(display)]
struct UserId(u64);
let user_id = UserId(12345);
let value = user_id.into_value();
// Safe downcasting
if let Value::Opaque(opaque) = &value {
if opaque.is::<UserId>() {
let user_id_ref = opaque.downcast_ref::<UserId>().unwrap();
println!("User ID: {}", user_id_ref.0);
}
}
Required Methods§
Sourcefn opaque_type(&self) -> OpaqueType
fn opaque_type(&self) -> OpaqueType
Returns the opaque type information for this value.
This method provides type metadata that can be used for type checking and runtime type identification.
Implementations§
Source§impl dyn Opaque
impl dyn Opaque
Sourcepub fn downcast_ref<T: Opaque>(&self) -> Option<&T>
pub fn downcast_ref<T: Opaque>(&self) -> Option<&T>
Returns a reference to the contained value if it’s of the specified type.
Sourcepub fn downcast_mut<T: Opaque>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Opaque>(&mut self) -> Option<&mut T>
Returns a mutable reference to the contained value if it’s of the specified type.