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
Objecttrait object. - mopo
- Define a custom Object-like trait. The
query,query_refandquery_mutmethods 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 anObjectof that type for the trait will always succeed. - Object
- This trait is the primary function of the library.
Objecttrait objects can be freely queried for any other trait, allowing conversion between trait objects. - Object
Clone - This is an object-safe version of
Clone, which is automatically implemented for allClone + Objecttypes. This is a support trait used to allowObjecttrait objects to be clonable. - Object
Eq - This is an object-safe version of
Eq, which is automatically implemented for allEq + Objecttypes. This is a support trait used to allowObjecttrait objects to be comparable in this way. - Object
Hash - This is an object-safe version of
Hash, which is automatically implemented for allHash + Objecttypes. This is a support trait used to allowObjecttrait objects to be comparable in this way. - Object
Ord - This is an object-safe version of
Ord, which is automatically implemented for allOrd + Objecttypes. This is a support trait used to allowObjecttrait objects to be comparable in this way. - Object
Partial Eq - This is an object-safe version of
PartialEq, which is automatically implemented for allPartialEq + Objecttypes. This is a support trait used to allowObjecttrait objects to be comparable in this way. - Object
Partial Ord - This is an object-safe version of
PartialOrd, which is automatically implemented for allPartialOrd + Objecttypes. This is a support trait used to allowObjecttrait objects to be comparable in this way.