2 unstable releases
0.2.0 | Aug 6, 2020 |
---|---|
0.1.0 | Mar 29, 2020 |
#58 in #lookup
14KB
166 lines
object-provider
This crate provides the ObjectProvider
trait and related types to support
dynamic type-directed object lookups. See the documentation for more details.
lib.rs
:
Trait for requesting values by type from a given object.
Examples
Using a Provider
let provider: &dyn ObjectProvider;
// It's possible to request concrete types like `PathBuf`
let path_buf = provider.request_ref::<PathBuf>().unwrap();
assert_eq!(path_buf, my_path);
// Requesting `!Sized` types, like slices and trait objects, is also supported.
let path = provider.request_ref::<Path>().unwrap();
assert_eq!(path, my_path);
let debug = provider.request_ref::<dyn Debug>().unwrap();
assert_eq!(
format!("{:?}", debug),
format!("{:?}", my_path),
);
// Types or interfaces not explicitly provided return `None`.
assert!(provider.request_ref::<i32>().is_none());
assert!(provider.request_ref::<dyn AsRef<Path>>().is_none());
Implementing a Provider
struct MyProvider {
path: PathBuf,
}
impl ObjectProvider for MyProvider {
fn provide<'a>(&'a self, request: Pin<&mut Request<'a>>) {
request
.provide_ref::<PathBuf>(&self.path)
.provide_ref::<Path>(&self.path)
.provide_ref::<dyn Debug>(&self.path);
}
}