[−][src]Crate object_provider
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); } }
Structs
Request | A dynamic request for an object based on its type. |
Traits
ObjectProvider | Trait to provide other objects based on a requested type at runtime. |
ObjectProviderExt | Methods supported by all |