Add & use inner_getter! in BoxView

This commit is contained in:
Tymoteusz Jankowski 2018-01-26 11:39:10 +01:00
parent d0956b40b0
commit 84ea73617f
3 changed files with 59 additions and 23 deletions

View File

@ -39,9 +39,6 @@ pub trait ViewWrapper: 'static {
where
F: FnOnce(&mut Self::V) -> R;
/// Gets the reference to the wrapped view.
fn get_view(&self) -> &Self::V;
/// Attempts to retrieve the inner view.
fn into_inner(self) -> Result<Self::V, Self>
where
@ -123,9 +120,6 @@ where
Some(f(self.deref_mut()))
}
fn get_view(&self) -> &Self::V {
self.deref()
}
}
// The main point of implementing ViewWrapper is to have View for free.
@ -203,8 +197,44 @@ macro_rules! wrap_impl {
Some(f(&mut self.$v))
}
fn get_view(&self) -> &Self::V {
&self.$v
}
};
}
/// Convenient macro to implement the getters for inner [`View`] in [`ViewWrapper`].
///
/// It defines the `get_inner` and `get_inner_mut` implementations.
///
/// [`ViewWrapper`]: view/trait.ViewWrapper.html
/// [`View`]: view/trait.View.html
///
/// # Examples
///
/// ```no_run
/// # #[macro_use] extern crate cursive;
/// # use cursive::view::{View,ViewWrapper};
/// struct FooView<T: View> {
/// view: T,
/// }
///
/// impl<T: View> FooView<T> {
/// inner_getters!(T);
/// }
///
/// impl <T: View> ViewWrapper for FooView<T> {
/// wrap_impl!(self.view: T);
/// }
/// # fn main() { }
/// ```
#[macro_export]
macro_rules! inner_getters {
($t:ty) => {
/// Gets access to the inner view.
pub fn get_inner(&self) -> &$t {
&self.view
}
/// Gets mutable access to the inner view.
pub fn get_inner_mut(&mut self) -> &mut $t {
&mut self.view
}
}
}

View File

@ -162,6 +162,8 @@ impl<T: View> BoxView<T> {
view,
)
}
inner_getters!(T);
}
impl<T: View> ViewWrapper for BoxView<T> {
@ -265,13 +267,23 @@ mod tests {
}
#[test]
//TODO: rm this or use it
fn xxx() {
use views::{BoxView,TextView};
use view::{ViewWrapper};
let parent: BoxView<TextView> = TextView::new("abc").full_screen();
//let child: TextView = parent.view;
let child: &TextView = parent.get_view();
1/0;
fn test_get_inner() {
use views::TextView;
let parent = TextView::new("abc").full_screen();
let child = parent.get_inner();
assert_eq!(child.get_content().source(), "abc");
}
#[test]
fn test_get_inner_mut() {
use views::TextView;
let mut parent = TextView::new("").full_screen();
let new_value = "new";
let child = parent.get_inner_mut();
child.set_content(new_value);
assert_eq!(child.get_content().source(), new_value);
}
}

View File

@ -100,10 +100,4 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
}
}
fn get_view(&self) -> &Self::V {
use std::ops::Deref;
let view = self.view.try_borrow().unwrap()
;
view.deref()
}
}