Trait Opaque

Source
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:

§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§

Source

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

Source

pub fn is<T: Opaque>(&self) -> bool

Checks if this opaque value is of a specific type.

§Type Parameters
  • T: The type to check for
§Returns

true if this value is of type T, false otherwise

Source

pub fn downcast<T: Opaque>(self: Box<Self>) -> Result<T, Box<Self>>

Attempts to downcast this boxed opaque value to a specific type.

§Type Parameters
  • T: The target type to downcast to
§Returns
  • Ok(T): Downcast successful
  • Err(Box<Self>): Downcast failed, returns original boxed value
Source

pub fn downcast_ref<T: Opaque>(&self) -> Option<&T>

Returns a reference to the contained value if it’s of the specified type.

Source

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.

Trait Implementations§

Source§

impl PartialEq for dyn Opaque

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for dyn Opaque

Implementors§