Merge pull request #319 from NickHackman/modernize-enum-discriminant-comparison

Simplified Logic when comparing discriminants
This commit is contained in:
Alex Crichton 2019-08-13 14:36:26 -05:00 committed by GitHub
commit 64d1385946
2 changed files with 4 additions and 23 deletions

View file

@ -8,6 +8,7 @@ use std::borrow::Cow;
use std::error; use std::error;
use std::f64; use std::f64;
use std::fmt; use std::fmt;
use std::mem::discriminant;
use std::str; use std::str;
use std::vec; use std::vec;
@ -2057,17 +2058,6 @@ impl<'a> E<'a> {
impl<'a> Value<'a> { impl<'a> Value<'a> {
fn same_type(&self, other: &Value<'a>) -> bool { fn same_type(&self, other: &Value<'a>) -> bool {
match (&self.e, &other.e) { discriminant(&self.e) == discriminant(&other.e)
(&E::String(..), &E::String(..))
| (&E::Integer(..), &E::Integer(..))
| (&E::Float(..), &E::Float(..))
| (&E::Boolean(..), &E::Boolean(..))
| (&E::Datetime(..), &E::Datetime(..))
| (&E::Array(..), &E::Array(..))
| (&E::InlineTable(..), &E::InlineTable(..)) => true,
(&E::DottedTable(..), &E::DottedTable(..)) => true,
_ => false,
}
} }
} }

View file

@ -3,6 +3,7 @@
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::fmt; use std::fmt;
use std::hash::Hash; use std::hash::Hash;
use std::mem::discriminant;
use std::ops; use std::ops;
use std::str::FromStr; use std::str::FromStr;
use std::vec; use std::vec;
@ -212,17 +213,7 @@ impl Value {
/// Tests whether this and another value have the same type. /// Tests whether this and another value have the same type.
pub fn same_type(&self, other: &Value) -> bool { pub fn same_type(&self, other: &Value) -> bool {
match (self, other) { discriminant(self) == discriminant(other)
(&Value::String(..), &Value::String(..))
| (&Value::Integer(..), &Value::Integer(..))
| (&Value::Float(..), &Value::Float(..))
| (&Value::Boolean(..), &Value::Boolean(..))
| (&Value::Datetime(..), &Value::Datetime(..))
| (&Value::Array(..), &Value::Array(..))
| (&Value::Table(..), &Value::Table(..)) => true,
_ => false,
}
} }
/// Returns a human-readable representation of the type of this value. /// Returns a human-readable representation of the type of this value.