Skip to content

Commit

Permalink
Add PartialOrd and Ord impls for more types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet committed Aug 6, 2021
1 parent 0bc8cd4 commit 14fec9b
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,50 @@ macro_rules! sum_product {
)*)
}

macro_rules! partialord_ord {
($t:ident) => {
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}

#[inline]
fn lt(&self, other: &Self) -> bool {
JsValue::as_ref(self).lt(JsValue::as_ref(other))
}

#[inline]
fn le(&self, other: &Self) -> bool {
JsValue::as_ref(self).le(JsValue::as_ref(other))
}

#[inline]
fn ge(&self, other: &Self) -> bool {
JsValue::as_ref(self).ge(JsValue::as_ref(other))
}

#[inline]
fn gt(&self, other: &Self) -> bool {
JsValue::as_ref(self).gt(JsValue::as_ref(other))
}
}

impl Ord for $t {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
Ordering::Equal
} else if self.lt(other) {
Ordering::Less
} else {
Ordering::Greater
}
}
}
};
}

#[wasm_bindgen]
extern "C" {
/// The `decodeURI()` function decodes a Uniform Resource Identifier (URI)
Expand Down Expand Up @@ -1050,45 +1094,7 @@ forward_js_binop!(impl Mul, mul for BigInt);
forward_js_binop!(impl Rem, rem for BigInt);
sum_product!(BigInt);

impl PartialOrd for BigInt {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}

#[inline]
fn lt(&self, other: &Self) -> bool {
JsValue::as_ref(self).lt(JsValue::as_ref(other))
}

#[inline]
fn le(&self, other: &Self) -> bool {
JsValue::as_ref(self).le(JsValue::as_ref(other))
}

#[inline]
fn ge(&self, other: &Self) -> bool {
JsValue::as_ref(self).ge(JsValue::as_ref(other))
}

#[inline]
fn gt(&self, other: &Self) -> bool {
JsValue::as_ref(self).gt(JsValue::as_ref(other))
}
}

impl Ord for BigInt {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
Ordering::Equal
} else if self.lt(other) {
Ordering::Less
} else {
Ordering::Greater
}
}
}
partialord_ord!(BigInt);

impl Default for BigInt {
fn default() -> Self {
Expand Down Expand Up @@ -1409,6 +1415,8 @@ impl Not for &Boolean {

forward_deref_unop!(impl Not, not for Boolean);

partialord_ord!(Boolean);

// DataView
#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -1680,6 +1688,8 @@ extern "C" {
pub fn to_string(this: &Error) -> JsString;
}

partialord_ord!(JsString);

// EvalError
#[wasm_bindgen]
extern "C" {
Expand Down

0 comments on commit 14fec9b

Please sign in to comment.