Crate query_interface

Source
Expand description

query-interface - dynamically query a type-erased object for any trait implementation

#[macro_use]
extern crate query_interface;
use query_interface::{Object, ObjectClone};
use std::fmt::Debug;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Foo;

interfaces!(Foo: ObjectClone, Debug, Bar);

trait Bar {
    fn do_something(&self);
}
impl Bar for Foo {
    fn do_something(&self) {
        println!("I'm a Foo!");
    }
}

fn main() {
    let obj = Box::new(Foo) as Box<Object>;
    let obj2 = obj.clone();
    println!("{:?}", obj2);

    obj2.query_ref::<Bar>().unwrap().do_something();  // Prints: "I'm a Foo!"
}

Macros§

interfaces
Allow a set of traits to be dynamically queried from a type when it is stored as an Object trait object.
mopo
Define a custom Object-like trait. The query, query_ref and query_mut methods will be automatically implemented on this trait object.

Traits§

HasInterface
You can use this trait to ensure that a type implements a trait as an interface. This means the type declared the trait in its interfaces!(...) list, and guarantees that querying an Object of that type for the trait will always succeed.
Object
This trait is the primary function of the library. Object trait objects can be freely queried for any other trait, allowing conversion between trait objects.
ObjectClone
This is an object-safe version of Clone, which is automatically implemented for all Clone + Object types. This is a support trait used to allow Object trait objects to be clonable.
ObjectEq
This is an object-safe version of Eq, which is automatically implemented for all Eq + Object types. This is a support trait used to allow Object trait objects to be comparable in this way.
ObjectHash
This is an object-safe version of Hash, which is automatically implemented for all Hash + Object types. This is a support trait used to allow Object trait objects to be comparable in this way.
ObjectOrd
This is an object-safe version of Ord, which is automatically implemented for all Ord + Object types. This is a support trait used to allow Object trait objects to be comparable in this way.
ObjectPartialEq
This is an object-safe version of PartialEq, which is automatically implemented for all PartialEq + Object types. This is a support trait used to allow Object trait objects to be comparable in this way.
ObjectPartialOrd
This is an object-safe version of PartialOrd, which is automatically implemented for all PartialOrd + Object types. This is a support trait used to allow Object trait objects to be comparable in this way.