From 17ccda2d40d61778c5b498767720e2b472b84452 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 14 Mar 2018 13:39:30 -0700 Subject: [PATCH] Use BoxableView trait bound --- src/cursive.rs | 9 ++++++--- src/views/any_box.rs | 6 +++--- src/views/stack_view.rs | 15 ++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/cursive.rs b/src/cursive.rs index 4a0fede..4045424 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -9,7 +9,7 @@ use std::path::Path; use std::sync::mpsc; use theme; use vec::Vec2; -use view::{self, AnyView, Finder, Position, View}; +use view::{self, AnyView, BoxableView, Finder, Position, View}; use views::{self, LayerPosition}; /// Identifies a screen in the cursive root. @@ -454,7 +454,10 @@ impl Cursive { /// siv.add_layer(views::TextView::new("Hello world!")); /// # } /// ``` - pub fn add_layer(&mut self, view: T) { + pub fn add_layer(&mut self, view: T) + where + T: BoxableView, + { self.screen_mut().add_layer(view); } @@ -463,7 +466,7 @@ impl Cursive { /// Fullscreen layers have no shadow. pub fn add_fullscreen_layer(&mut self, view: T) where - T: 'static + View, + T: BoxableView, { self.screen_mut().add_fullscreen_layer(view); } diff --git a/src/views/any_box.rs b/src/views/any_box.rs index 87621a9..5901104 100644 --- a/src/views/any_box.rs +++ b/src/views/any_box.rs @@ -1,5 +1,5 @@ use std::ops::{Deref, DerefMut}; -use view::{AnyView, View, ViewWrapper}; +use view::{AnyView, BoxableView, ViewWrapper}; /// A boxed `AnyView`. pub struct AnyBox { @@ -13,8 +13,8 @@ impl AnyBox { } /// Box the given view - pub fn boxed(view: T) -> Self { - AnyBox::new(Box::new(view)) + pub fn boxed(view: T) -> Self { + AnyBox::new(view.as_boxed_view()) } /// Returns the inner boxed view. diff --git a/src/views/stack_view.rs b/src/views/stack_view.rs index dda4095..cbc5309 100644 --- a/src/views/stack_view.rs +++ b/src/views/stack_view.rs @@ -7,7 +7,8 @@ use std::cell; use std::ops::Deref; use theme::ColorStyle; use vec::Vec2; -use view::{AnyView, Offset, Position, Selector, View, ViewWrapper}; +use view::{AnyView, BoxableView, Offset, Position, Selector, View, + ViewWrapper}; use views::{AnyBox, Layer, ShadowView}; /// Simple stack of views. @@ -180,7 +181,7 @@ impl StackView { /// Fullscreen layers have no shadow. pub fn add_fullscreen_layer(&mut self, view: T) where - T: 'static + View, + T: BoxableView, { let boxed = AnyBox::boxed(view); self.layers.push(Child { @@ -194,7 +195,7 @@ impl StackView { /// Adds new view on top of the stack in the center of the screen. pub fn add_layer(&mut self, view: T) where - T: 'static + View, + T: BoxableView, { self.add_layer_at(Position::center(), view); } @@ -204,7 +205,7 @@ impl StackView { /// Chainable variant. pub fn layer(self, view: T) -> Self where - T: 'static + View, + T: BoxableView, { self.with(|s| s.add_layer(view)) } @@ -265,7 +266,7 @@ impl StackView { /// Chainable variant. pub fn fullscreen_layer(self, view: T) -> Self where - T: 'static + View, + T: BoxableView, { self.with(|s| s.add_fullscreen_layer(view)) } @@ -273,7 +274,7 @@ impl StackView { /// Adds a view on top of the stack. pub fn add_layer_at(&mut self, position: Position, view: T) where - T: 'static + View, + T: BoxableView, { let boxed = AnyBox::boxed(view); self.layers.push(Child { @@ -294,7 +295,7 @@ impl StackView { /// Chainable variant. pub fn layer_at(self, position: Position, view: T) -> Self where - T: 'static + View, + T: BoxableView, { self.with(|s| s.add_layer_at(position, view)) }