mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-24 01:46:31 +00:00
Add & use inner_getter! in BoxView
This commit is contained in:
parent
d0956b40b0
commit
84ea73617f
@ -39,9 +39,6 @@ pub trait ViewWrapper: 'static {
|
|||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::V) -> R;
|
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.
|
/// Attempts to retrieve the inner view.
|
||||||
fn into_inner(self) -> Result<Self::V, Self>
|
fn into_inner(self) -> Result<Self::V, Self>
|
||||||
where
|
where
|
||||||
@ -123,9 +120,6 @@ where
|
|||||||
Some(f(self.deref_mut()))
|
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.
|
// 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))
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -162,6 +162,8 @@ impl<T: View> BoxView<T> {
|
|||||||
view,
|
view,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inner_getters!(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> ViewWrapper for BoxView<T> {
|
impl<T: View> ViewWrapper for BoxView<T> {
|
||||||
@ -265,13 +267,23 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
//TODO: rm this or use it
|
fn test_get_inner() {
|
||||||
fn xxx() {
|
use views::TextView;
|
||||||
use views::{BoxView,TextView};
|
|
||||||
use view::{ViewWrapper};
|
let parent = TextView::new("abc").full_screen();
|
||||||
let parent: BoxView<TextView> = TextView::new("abc").full_screen();
|
let child = parent.get_inner();
|
||||||
//let child: TextView = parent.view;
|
assert_eq!(child.get_content().source(), "abc");
|
||||||
let child: &TextView = parent.get_view();
|
}
|
||||||
1/0;
|
#[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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user