Skip to main content

NativeObject

Trait NativeObject 

Source
pub trait NativeObject:
    Send
    + Sync
    + Debug
    + Trace
    + 'static {
    // Required methods
    fn type_tag(&self) -> &str;
    fn as_any(&self) -> &dyn Any;
}
Expand description

An opaque Rust value that can be wrapped as a Clojure Value::NativeObject.

Implementors must be Send + Sync (shared across threads), Debug (for display), and Trace (so the GC can walk any GcPtr/Value references held inside).

§Example

struct Counter { n: AtomicI64 }

impl NativeObject for Counter {
    fn type_tag(&self) -> &str { "Counter" }
    fn as_any(&self) -> &dyn Any { self }
}

// Trace is a no-op if the struct holds no GcPtr/Value fields.
impl Trace for Counter {
    fn trace(&self, _: &mut MarkVisitor) {}
}

Required Methods§

Source

fn type_tag(&self) -> &str

Short type name used for type_tag_of and protocol dispatch.

Convention: use the Rust struct name (e.g. "TcpStream", "Counter"). This string is what you pass to extend-type on the Clojure side.

Source

fn as_any(&self) -> &dyn Any

Downcast support — native functions that know the concrete type can use obj.as_any().downcast_ref::<MyType>() to access it.

Implementors§