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§

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

Traits§

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.