Skip to content

Commit

Permalink
AttrValue's PartialEq takes the enum variant into account (#2162)
Browse files Browse the repository at this point in the history
* Attempt to fix recursion on display

* Include into_string() for AttrValue

* PartialEq takes the enum variant into account #2161

Co-authored-by: mibes <mibes@avaya.com>
  • Loading branch information
mibes and mibes404 committed Nov 20, 2021
1 parent 916dc2e commit 068a20a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/yew/src/virtual_dom/mod.rs
Expand Up @@ -43,7 +43,7 @@ use std::ops::Deref;
use std::rc::Rc;

/// Attribute value
#[derive(Eq, PartialEq, Debug)]
#[derive(Debug)]
pub enum AttrValue {
/// String living for `'static`
Static(&'static str),
Expand Down Expand Up @@ -109,6 +109,14 @@ impl fmt::Display for AttrValue {
}
}

impl PartialEq for AttrValue {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}

impl Eq for AttrValue {}

impl AttrValue {
/// Consumes the AttrValue and returns the owned String from the AttrValue whenever possible.
/// For AttrValue::Rc the <str> is cloned to String in case there are other Rc or Weak pointers to the
Expand Down Expand Up @@ -143,6 +151,22 @@ mod tests_attr_value {
let av = AttrValue::Rc("Rc<str>".into());
assert_eq!(av.into_string(), "Rc<str>");
}

#[test]
fn test_equality() {
// construct 3 AttrValue with same embedded value; expectation is that all are equal
let a = AttrValue::Owned("same".to_string());
let b = AttrValue::Static("same");
let c = AttrValue::Rc("same".into());

assert_eq!(a, b);
assert_eq!(b, c);
assert_eq!(a, c);

assert!(a == b);
assert!(b == c);
assert!(a == c);
}
}

/// Applies contained changes to DOM [Element]
Expand Down

0 comments on commit 068a20a

Please sign in to comment.