diff --git a/src/view/mod.rs b/src/view/mod.rs index 9856590..f2a7923 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -71,6 +71,22 @@ pub trait AnyView: View { /// Downcast self to a mutable `Any`. fn as_any_mut(&mut self) -> &mut Any; + + /// Returns a boxed any from a boxed self. + /// + /// Can be used before `Box::downcast()`. + /// + /// # Examples + /// + /// ```rust + /// # use cursive::views::TextView; + /// # use cursive::view::AnyView; + /// # fn main() { + /// let boxed: Box = Box::new(TextView::new("text")); + /// let text: Box = boxed.as_boxed_any().downcast().unwrap(); + /// # } + /// ``` + fn as_boxed_any(self: Box) -> Box; } impl AnyView for T { @@ -83,6 +99,10 @@ impl AnyView for T { fn as_any_mut(&mut self) -> &mut Any { self } + + fn as_boxed_any(self: Box) -> Box { + self + } } /// Main trait defining a view behaviour. diff --git a/src/views/stack_view.rs b/src/views/stack_view.rs index a056b9b..29e0769 100644 --- a/src/views/stack_view.rs +++ b/src/views/stack_view.rs @@ -570,9 +570,7 @@ mod tests { ); let layer = stack.pop_layer().unwrap(); - let box_view = layer.as_any().downcast_ref::>().unwrap(); - let text_view = - (**box_view).as_any().downcast_ref::().unwrap(); - assert_eq!(text_view.get_content().source(), "2"); + let text: Box = layer.as_boxed_any().downcast().unwrap(); + assert_eq!(text.get_content().source(), "2"); } }