From 84ea73617f92b2c55aace33cec0faf03d9277512 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Fri, 26 Jan 2018 11:39:10 +0100 Subject: [PATCH] Add & use inner_getter! in BoxView --- src/view/view_wrapper.rs | 48 ++++++++++++++++++++++++++++++++-------- src/views/box_view.rs | 28 ++++++++++++++++------- src/views/id_view.rs | 6 ----- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/view/view_wrapper.rs b/src/view/view_wrapper.rs index 5c49da6..6e3d8a9 100644 --- a/src/view/view_wrapper.rs +++ b/src/view/view_wrapper.rs @@ -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 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 { +/// view: T, +/// } +/// +/// impl FooView { +/// inner_getters!(T); +/// } +/// +/// impl ViewWrapper for FooView { +/// 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 + } + } +} diff --git a/src/views/box_view.rs b/src/views/box_view.rs index 02d481c..0ae1e06 100644 --- a/src/views/box_view.rs +++ b/src/views/box_view.rs @@ -162,6 +162,8 @@ impl BoxView { view, ) } + + inner_getters!(T); } impl ViewWrapper for BoxView { @@ -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::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); } } diff --git a/src/views/id_view.rs b/src/views/id_view.rs index a639422..a925ff8 100644 --- a/src/views/id_view.rs +++ b/src/views/id_view.rs @@ -100,10 +100,4 @@ impl ViewWrapper for IdView { } } - fn get_view(&self) -> &Self::V { - use std::ops::Deref; - let view = self.view.try_borrow().unwrap() - ; - view.deref() - } }