forked from Orchid/orchid
Cut down on macro nonsense
- InertAtomic replaced atomic_inert! for improved tooling support - atomic_defaults! is easier to type out than to explain in a docstring - Changed rustfmt config to better support tiny functions such as as_any
This commit is contained in:
29
src/utils/ddispatch.rs
Normal file
29
src/utils/ddispatch.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
//! A variant of [std::any::Provider]
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
/// A request for a value of an unknown type
|
||||
pub struct Request<'a>(&'a mut dyn Any);
|
||||
impl<'a> Request<'a> {
|
||||
pub fn can_serve<T: 'static>(&self) -> bool { self.0.is::<Option<T>>() }
|
||||
|
||||
pub fn serve<T: 'static>(&mut self, value: T) { self.serve_with(|| value) }
|
||||
|
||||
pub fn serve_with<T: 'static>(&mut self, provider: impl FnOnce() -> T) {
|
||||
if let Some(slot) = self.0.downcast_mut() {
|
||||
*slot = provider();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for objects that can respond to type-erased commands. This trait is
|
||||
/// a dependency of `Atomic` but the implementation can be left empty.
|
||||
pub trait Responder {
|
||||
fn respond(&self, _request: Request) {}
|
||||
}
|
||||
|
||||
pub fn request<T: 'static>(responder: &(impl Responder + ?Sized)) -> Option<T> {
|
||||
let mut slot = None;
|
||||
responder.respond(Request(&mut slot));
|
||||
slot
|
||||
}
|
||||
Reference in New Issue
Block a user