Skip to content

Commit

Permalink
Improve Boolean/Number/JsString consistency
Browse files Browse the repository at this point in the history
* Ensure `PartialEq` is implemented from these types to native Rust types
* Implement `From` between these type and native Rust types
* Deprecated `Number::new` and `Boolean::new` to discourage use of the
  object forms, recommending the `from` constructors instead.

Closes #1446
  • Loading branch information
alexcrichton committed Apr 12, 2019
1 parent df6e15e commit ba88ae8
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,14 @@ extern "C" {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub type Boolean;

/// The `Boolean()` constructor creates an object wrapper for a boolean value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
#[wasm_bindgen(constructor)]
#[deprecated(note = "recommended to use `Boolean::from` instead")]
pub fn new(value: &JsValue) -> Boolean;

/// The `valueOf()` method returns the primitive value of a `Boolean` object.
Expand All @@ -483,6 +484,35 @@ extern "C" {
pub fn value_of(this: &Boolean) -> bool;
}

impl From<bool> for Boolean {
#[inline]
fn from(b: bool) -> Boolean {
Boolean::unchecked_from_js(JsValue::from(b))
}
}

impl From<Boolean> for bool {
#[inline]
fn from(b: Boolean) -> bool {
b.value_of()
}
}

impl PartialEq<bool> for Boolean {
#[inline]
fn eq(&self, other: &bool) -> bool {
self.value_of() == *other
}
}

impl Eq for Boolean {}

impl fmt::Debug for Boolean {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value_of().fmt(f)
}
}

// DataView
#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -1406,7 +1436,7 @@ extern "C" {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub type Number;

/// The Number.isFinite() method determines whether the passed value is a finite number.
Expand Down Expand Up @@ -1441,6 +1471,7 @@ extern "C" {
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
#[wasm_bindgen(constructor)]
#[deprecated(note = "recommended to use `Number::from` instead")]
pub fn new(value: &JsValue) -> Number;

/// The Number.parseInt() method parses a string argument and returns an
Expand Down Expand Up @@ -1500,6 +1531,38 @@ extern "C" {
pub fn value_of(this: &Number) -> f64;
}

macro_rules! number_from {
($($x:ident)*) => ($(
impl From<$x> for Number {
#[inline]
fn from(x: $x) -> Number {
Number::unchecked_from_js(JsValue::from(x))
}
}

impl PartialEq<$x> for Number {
#[inline]
fn eq(&self, other: &$x) -> bool {
self.value_of() == f64::from(*other)
}
}
)*)
}
number_from!(i8 u8 i16 u16 i32 u32 f32 f64);

impl From<Number> for f64 {
#[inline]
fn from(n: Number) -> f64 {
n.value_of()
}
}

impl fmt::Debug for Number {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value_of().fmt(f)
}
}

// Date.
#[wasm_bindgen]
extern "C" {
Expand Down

0 comments on commit ba88ae8

Please sign in to comment.