Dynamically query a type-erased object for any trait implementation
Example: ```rust
extern crate query_interface;
use query_interface::{Object, ObjectClone}; use std::fmt::Debug;
struct Foo;
interfaces!(Foo: ObjectClone, Debug, Bar);
trait Bar { fn dosomething(&self); } impl Bar for Foo { fn dosomething(&self) { println!("I'm a Foo!"); } }
fn main() { let obj = Box::new(Foo) as Box
obj2.query_ref::<Bar>().unwrap().do_something(); // Prints: "I'm a Foo!"
} ```
In short, this allows you to pass around Object
s and still have access to any of the (object-safe) traits
implemented for the underlying type. The library also provides some useful object-safe equivalents to standard
traits, including ObjectClone
, ObjectPartialEq
, ObjectPartialOrd
, ObjectHash
.
To improve usability, the non-object-safe versions of the traits are implemented directly on the Object
trait
object, allowing you to easily clone Object
s and store them in collections.
You can have your own Object
-like traits to enforce some additional static requirements by using the mopo!
macro:
```rust
trait CustomObject : Object {
...
}
mopo!(CustomObject);
struct Foo; interfaces!(Foo: CustomObject);
impl CustomObject for Foo { ... } ```
Now you can use CustomObject
in all the ways you could use Object
.