Add a JsCast trait specified in [RFC 2]

[RFC 2]: https://github.com/rustwasm/rfcs/pull/2
This commit is contained in:
Alex Crichton
2018-08-04 09:02:31 -07:00
parent 9b65e57585
commit bea07abd0f
2 changed files with 184 additions and 0 deletions

View File

@ -46,6 +46,9 @@ pub mod prelude {
pub mod convert;
pub mod describe;
mod cast;
pub use cast::JsCast;
if_std! {
extern crate std;
use std::prelude::v1::*;
@ -347,6 +350,22 @@ impl From<bool> for JsValue {
}
}
impl JsCast for JsValue {
// everything is a `JsValue`!
fn instanceof(_val: &JsValue) -> bool { true }
fn unchecked_from_js(val: JsValue) -> Self { val }
fn unchecked_from_js_ref(val: &JsValue) -> &Self { val }
fn unchecked_from_js_mut(val: &mut JsValue) -> &mut Self { val }
}
impl AsMut<JsValue> for JsValue {
fn as_mut(&mut self) -> &mut JsValue { self }
}
impl AsRef<JsValue> for JsValue {
fn as_ref(&self) -> &JsValue { self }
}
macro_rules! numbers {
($($n:ident)*) => ($(
impl PartialEq<$n> for JsValue {