Skip to content

Commit 59f9b22

Browse files
committed
Improve Boolean/Number/JsString consistency
* 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
1 parent df6e15e commit 59f9b22

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

crates/js-sys/src/lib.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,14 @@ extern "C" {
467467
#[wasm_bindgen]
468468
extern "C" {
469469
#[wasm_bindgen(extends = Object)]
470-
#[derive(Clone, Debug)]
470+
#[derive(Clone)]
471471
pub type Boolean;
472472

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

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

487+
impl From<bool> for Boolean {
488+
fn from(b: bool) -> Boolean {
489+
Boolean { obj: JsValue::from(b) }
490+
}
491+
}
492+
493+
impl From<Boolean> for bool {
494+
fn from(b: Boolean) -> bool {
495+
b.value_of()
496+
}
497+
}
498+
499+
impl PartialEq<bool> for Boolean {
500+
fn eq(&self, other: &bool) -> bool {
501+
self.value_of() == *other
502+
}
503+
}
504+
505+
impl fmt::Debug for Boolean {
506+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
507+
self.value_of().fmt(f)
508+
}
509+
}
510+
486511
// DataView
487512
#[wasm_bindgen]
488513
extern "C" {
@@ -1406,7 +1431,7 @@ extern "C" {
14061431
#[wasm_bindgen]
14071432
extern "C" {
14081433
#[wasm_bindgen(extends = Object)]
1409-
#[derive(Clone, Debug)]
1434+
#[derive(Clone)]
14101435
pub type Number;
14111436

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

14461472
/// The Number.parseInt() method parses a string argument and returns an
@@ -1500,6 +1526,35 @@ extern "C" {
15001526
pub fn value_of(this: &Number) -> f64;
15011527
}
15021528

1529+
macro_rules! number_from {
1530+
($($x:ident)*) => ($(
1531+
impl From<$x> for Number {
1532+
fn from(x: $x) -> Number {
1533+
Number { obj: x.into() }
1534+
}
1535+
}
1536+
1537+
impl PartialEq<$x> for Number {
1538+
fn eq(&self, other: &$x) -> bool {
1539+
self.value_of() == f64::from(*other)
1540+
}
1541+
}
1542+
)*)
1543+
}
1544+
number_from!(i8 u8 i16 u16 i32 u32 f32 f64);
1545+
1546+
impl From<Number> for f64 {
1547+
fn from(n: Number) -> f64 {
1548+
n.value_of()
1549+
}
1550+
}
1551+
1552+
impl fmt::Debug for Number {
1553+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1554+
self.value_of().fmt(f)
1555+
}
1556+
}
1557+
15031558
// Date.
15041559
#[wasm_bindgen]
15051560
extern "C" {

0 commit comments

Comments
 (0)