Add View: Any

This commit is contained in:
Alexandre Bury 2017-12-11 18:54:40 -08:00
parent bbee77f1b7
commit 9b9619aa53
5 changed files with 10 additions and 13 deletions

View File

@ -66,7 +66,7 @@ use vec::Vec2;
use views::IdView;
/// Main trait defining a view behaviour.
pub trait View {
pub trait View: Any {
/// Called when a key was pressed.
///
/// Default implementation just ignores it.

View File

@ -14,7 +14,7 @@ use view::{Selector, View};
/// `with_view_mut` for you.
///
/// [`wrap_impl!`]: ../macro.wrap_impl.html
pub trait ViewWrapper {
pub trait ViewWrapper: 'static {
/// Type that this view wraps.
type V: View + ?Sized;
@ -96,7 +96,7 @@ pub trait ViewWrapper {
// Some types easily implement ViewWrapper.
// This includes Box<T: View>
use std::ops::{Deref, DerefMut};
impl<U: View + ?Sized, T: Deref<Target = U> + DerefMut> ViewWrapper for T {
impl<U: View + ?Sized, T: Deref<Target = U> + DerefMut + 'static> ViewWrapper for T {
type V = U;
fn with_view<F, R>(&self, f: F) -> Option<R>

View File

@ -173,7 +173,7 @@ impl<T> Canvas<T> {
}
}
impl<T> View for Canvas<T> {
impl<T: 'static> View for Canvas<T> {
fn draw(&self, printer: &Printer) {
(self.draw)(&self.state, printer);
}

View File

@ -1,6 +1,7 @@
use owning_ref::{OwningHandle, RcRef};
use std::any::Any;
use std::cell::{RefCell, RefMut};
use std::ops::DerefMut;
use std::rc::Rc;
use view::{Selector, View, ViewWrapper};
@ -69,18 +70,14 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
// Whoops! Abort! Undo!
self.view = rc;
Err(self)
},
Ok(cell) => {
Ok(cell.into_inner())
}
Ok(cell) => Ok(cell.into_inner()),
}
}
// Some for<'b> weirdness here to please the borrow checker gods...
fn wrap_call_on_any<'a>(
&mut self,
selector: &Selector,
mut callback: BoxedCallback<'a>,
&mut self, selector: &Selector, mut callback: BoxedCallback<'a>
) {
match selector {
&Selector::Id(id) if id == self.id => callback(self),
@ -88,7 +85,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
self.view
.try_borrow_mut()
.ok()
.map(|mut v| v.call_on_any(s, callback));
.map(|mut v| v.deref_mut().call_on_any(s, callback));
}
}
}
@ -99,7 +96,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
s => self.view
.try_borrow_mut()
.map_err(|_| ())
.and_then(|mut v| v.focus_view(s)),
.and_then(|mut v| v.deref_mut().focus_view(s)),
}
}
}

View File

@ -154,7 +154,7 @@ impl<T> RadioButton<T> {
}
}
impl<T> View for RadioButton<T> {
impl<T: 'static> View for RadioButton<T> {
fn required_size(&mut self, _: Vec2) -> Vec2 {
self.req_size()
}