From f8d34b8c520fc7637bffed21e1c9501c73ce9e45 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 23 May 2020 18:51:59 -0700 Subject: [PATCH] Fix EventTrigger::has_tag --- cursive-core/src/event.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cursive-core/src/event.rs b/cursive-core/src/event.rs index 5bd8926..51f22ca 100644 --- a/cursive-core/src/event.rs +++ b/cursive-core/src/event.rs @@ -39,8 +39,18 @@ pub struct EventTrigger { tag: Box, } -trait AnyTag: Any + std::fmt::Debug {} -impl AnyTag for T where T: Any + std::fmt::Debug {} +trait AnyTag: Any + std::fmt::Debug { + fn as_any(&self) -> &dyn Any; +} + +impl AnyTag for T +where + T: Any + std::fmt::Debug, +{ + fn as_any(&self) -> &dyn Any { + &*self + } +} impl EventTrigger { /// Create a new `EventTrigger` using the given function as filter. @@ -57,15 +67,29 @@ impl EventTrigger { F: 'static + Fn(&Event) -> bool, T: Any + std::fmt::Debug, { + let tag = Box::new(tag); EventTrigger { trigger: Box::new(f), - tag: Box::new(tag), + tag: tag, } } /// Check if this trigger has the given tag. + /// + /// # Examples + /// + /// ```rust + /// use cursive_core::event::{Event, EventTrigger}; + /// + /// let event = Event::CtrlChar('c'); + /// let trigger: EventTrigger = event.clone().into(); + /// assert!(trigger.has_tag(&event), "Trigger does not recognize its own tag."); + /// ``` pub fn has_tag(&self, tag: &T) -> bool { - Any::downcast_ref::(&self.tag).map_or(false, |t| tag == t) + (*self.tag) + .as_any() + .downcast_ref::() + .map_or(false, |t| tag == t) } /// Checks if this trigger applies to the given `Event`.