mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add AnyView::as_boxed_any for easier downcasting
This commit is contained in:
parent
06305c89a9
commit
dea226a095
@ -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<AnyView> = Box::new(TextView::new("text"));
|
||||
/// let text: Box<TextView> = boxed.as_boxed_any().downcast().unwrap();
|
||||
/// # }
|
||||
/// ```
|
||||
fn as_boxed_any(self: Box<Self>) -> Box<Any>;
|
||||
}
|
||||
|
||||
impl<T: View> AnyView for T {
|
||||
@ -83,6 +99,10 @@ impl<T: View> AnyView for T {
|
||||
fn as_any_mut(&mut self) -> &mut Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_boxed_any(self: Box<Self>) -> Box<Any> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Main trait defining a view behaviour.
|
||||
|
@ -570,9 +570,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let layer = stack.pop_layer().unwrap();
|
||||
let box_view = layer.as_any().downcast_ref::<Box<AnyView>>().unwrap();
|
||||
let text_view =
|
||||
(**box_view).as_any().downcast_ref::<TextView>().unwrap();
|
||||
assert_eq!(text_view.get_content().source(), "2");
|
||||
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
|
||||
assert_eq!(text.get_content().source(), "2");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user