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
andquery_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 anObject
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 allClone + Object
types. This is a support trait used to allowObject
trait objects to be clonable. - This is an object-safe version of
Eq
, which is automatically implemented for allEq + Object
types. This is a support trait used to allowObject
trait objects to be comparable in this way. - This is an object-safe version of
Hash
, which is automatically implemented for allHash + Object
types. This is a support trait used to allowObject
trait objects to be comparable in this way. - This is an object-safe version of
Ord
, which is automatically implemented for allOrd + Object
types. This is a support trait used to allowObject
trait objects to be comparable in this way. - This is an object-safe version of
PartialEq
, which is automatically implemented for allPartialEq + Object
types. This is a support trait used to allowObject
trait objects to be comparable in this way. - This is an object-safe version of
PartialOrd
, which is automatically implemented for allPartialOrd + Object
types. This is a support trait used to allowObject
trait objects to be comparable in this way.