1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
//! Module for `neq_assign` utility function. use std::borrow::BorrowMut; use yew::html::ShouldRender; /// Blanket trait to provide a convenience method for assigning props in `changed` or updating values in `update`. pub trait NeqAssign<NEW> { /// If `self` and `new` aren't equal, assigns `new` to `self` and returns true, otherwise returns false. /// /// Short for "Not equal assign". /// /// # Example /// ``` /// # use yew::{Component, ShouldRender, ComponentLink}; /// # use yewtil::NeqAssign; /// # use yew::Properties; ///# use yew::virtual_dom::VNode; /// ##[derive(Properties, PartialEq)] /// struct Props { /// field1: String, /// field2: usize /// } /// struct Model { /// props: Props /// } /// impl Component for Model { /// # type Message = (); /// type Properties = Props; /// // ... /// # /// # fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self { /// # unimplemented!() /// # } /// # fn update(&mut self, msg: ()) -> ShouldRender { /// # unimplemented!() /// # } /// # /// fn change(&mut self, props: Self::Properties) -> ShouldRender{ /// self.props.neq_assign(props) /// } ///# ///# fn view(&self) -> VNode<Self> { ///# unimplemented!() ///# } /// } /// ``` fn neq_assign(&mut self, new: NEW) -> ShouldRender; } impl<T: BorrowMut<U>, U: PartialEq> NeqAssign<U> for T { fn neq_assign(&mut self, new: U) -> bool { if self.borrow() != &new { *self.borrow_mut() = new; true } else { false } } }