somatize_core/any.rs
1//! Downcasting, without every implementor writing the same three lines.
2
3use std::any::Any;
4
5/// Erase to `dyn Any`, so a `dyn Filter` or `dyn Step` can be downcast to
6/// the concrete type behind it.
7///
8/// A supertrait with a blanket impl rather than a required method.
9/// `Filter` and `Step` both demanded `fn as_any(&self) -> &dyn Any { self }`
10/// from every implementor — sixty-odd identical bodies across the
11/// workspace, and one more required of anyone writing a filter of their
12/// own — to serve two downcasts in the whole codebase.
13pub trait AsAny {
14 /// The receiver as `&dyn Any`, ready for `downcast_ref`.
15 fn as_any(&self) -> &dyn Any;
16}
17
18impl<T: Any> AsAny for T {
19 fn as_any(&self) -> &dyn Any {
20 self
21 }
22}