cursive/src/views/view_box.rs

62 lines
1.2 KiB
Rust
Raw Normal View History

use std::ops::{Deref, DerefMut};
use crate::view::{IntoBoxedView, View, ViewWrapper};
/// A boxed `View`.
///
/// It derefs to the wrapped view.
pub struct ViewBox {
2019-02-28 23:55:02 +00:00
view: Box<dyn View>,
}
impl ViewBox {
/// Creates a new `ViewBox` around the given boxed view.
2019-02-28 23:55:02 +00:00
pub fn new(view: Box<dyn View>) -> Self {
ViewBox { view }
}
/// Box the given view
2018-03-14 22:11:27 +00:00
pub fn boxed<T>(view: T) -> Self
where
T: IntoBoxedView,
{
ViewBox::new(view.as_boxed_view())
}
/// Returns the inner boxed view.
2019-02-28 23:55:02 +00:00
pub fn unwrap(self) -> Box<dyn View> {
self.view
}
}
impl Deref for ViewBox {
2019-02-28 23:55:02 +00:00
type Target = dyn View;
2019-02-28 23:55:02 +00:00
fn deref(&self) -> &dyn View {
&*self.view
}
}
impl DerefMut for ViewBox {
2019-02-28 23:55:02 +00:00
fn deref_mut(&mut self) -> &mut dyn View {
&mut *self.view
}
}
impl ViewWrapper for ViewBox {
2019-02-28 23:55:02 +00:00
type V = dyn View;
fn with_view<F, R>(&self, f: F) -> Option<R>
where
F: FnOnce(&Self::V) -> R,
{
Some(f(&*self.view))
}
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
where
F: FnOnce(&mut Self::V) -> R,
{
Some(f(&mut *self.view))
}
}