Skip to main content

Opaque

Trait Opaque 

Source
pub trait Opaque:
    Any
    + OpaqueEq
    + AsDebug
    + Send
    + Sync {
    // Required method
    fn runtime_type_name(&self) -> &str;
}
Expand description

Trait for user-defined opaque values stored inside Value::Opaque.

Implement this trait for types that should participate in CEL evaluation as opaque/user-defined values. An opaque value:

  • must report a stable runtime type name via Opaque::runtime_type_name;
  • participates in equality via the blanket OpaqueEq implementation;
  • can be formatted via AsDebug;
  • must be thread-safe (Send + Sync).

When the json feature is enabled you may optionally provide a JSON representation for diagnostics, logging or interop. Returning None keeps the value non-serializable for JSON.

Example

use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::sync::Arc;
use cel::objects::{Opaque, Value};

#[derive(Eq, PartialEq)]
struct MyId(u64);

impl Debug for MyId {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "MyId({})", self.0) }
}

impl Opaque for MyId {
    fn runtime_type_name(&self) -> &str { "example.MyId" }
}

// Values of `MyId` can now be wrapped in `Value::Opaque` and compared.
let a = Value::Opaque(Arc::new(MyId(7)));
let b = Value::Opaque(Arc::new(MyId(7)));
assert_eq!(a, b);

Required Methods§

Source

fn runtime_type_name(&self) -> &str

Returns a stable, fully-qualified type name for this value’s runtime type.

This name is used to check type compatibility before attempting downcasts during equality checks and other operations. It should be stable across versions and unique within your application or library (e.g., a package qualified name like my.pkg.Type).

Implementations§

Source§

impl dyn Opaque

Source

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§