Skip to content

Commit ba88ae8

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 ba88ae8

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

crates/js-sys/src/lib.rs

Lines changed: 65 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,35 @@ extern "C" {
483484
pub fn value_of(this: &Boolean) -> bool;
484485
}
485486

487+
impl From<bool> for Boolean {
488+
#[inline]
489+
fn from(b: bool) -> Boolean {
490+
Boolean::unchecked_from_js(JsValue::from(b))
491+
}
492+
}
493+
494+
impl From<Boolean> for bool {
495+
#[inline]
496+
fn from(b: Boolean) -> bool {
497+
b.value_of()
498+
}
499+
}
500+
501+
impl PartialEq<bool> for Boolean {
502+
#[inline]
503+
fn eq(&self, other: &bool) -> bool {
504+
self.value_of() == *other
505+
}
506+
}
507+
508+
impl Eq for Boolean {}
509+
510+
impl fmt::Debug for Boolean {
511+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
512+
self.value_of().fmt(f)
513+
}
514+
}
515+
486516
// DataView
487517
#[wasm_bindgen]
488518
extern "C" {
@@ -1406,7 +1436,7 @@ extern "C" {
14061436
#[wasm_bindgen]
14071437
extern "C" {
14081438
#[wasm_bindgen(extends = Object)]
1409-
#[derive(Clone, Debug)]
1439+
#[derive(Clone)]
14101440
pub type Number;
14111441

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

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

1534+
macro_rules! number_from {
1535+
($($x:ident)*) => ($(
1536+
impl From<$x> for Number {
1537+
#[inline]
1538+
fn from(x: $x) -> Number {
1539+
Number::unchecked_from_js(JsValue::from(x))
1540+
}
1541+
}
1542+
1543+
impl PartialEq<$x> for Number {
1544+
#[inline]
1545+
fn eq(&self, other: &$x) -> bool {
1546+
self.value_of() == f64::from(*other)
1547+
}
1548+
}
1549+
)*)
1550+
}
1551+
number_from!(i8 u8 i16 u16 i32 u32 f32 f64);
1552+
1553+
impl From<Number> for f64 {
1554+
#[inline]
1555+
fn from(n: Number) -> f64 {
1556+
n.value_of()
1557+
}
1558+
}
1559+
1560+
impl fmt::Debug for Number {
1561+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1562+
self.value_of().fmt(f)
1563+
}
1564+
}
1565+
15031566
// Date.
15041567
#[wasm_bindgen]
15051568
extern "C" {

0 commit comments

Comments
 (0)