@@ -16,24 +16,17 @@ pub trait JsCast
16
16
where
17
17
Self : AsRef < JsValue > + Into < JsValue > ,
18
18
{
19
- /// Test whether this JS value is an instance of the type `T`.
20
- ///
21
- /// This method performs a dynamic check (at runtime) using the JS
22
- /// `instanceof` operator. This method returns `self instanceof T`.
23
- ///
24
- /// Note that `instanceof` does not work with primitive values or across
25
- /// different realms (e.g. iframes). Prefer using `has_type` instead.
26
- fn is_instance_of < T > ( & self ) -> bool
27
- where
28
- T : JsCast ,
29
- {
30
- T :: instanceof ( self . as_ref ( ) )
31
- }
32
-
33
19
/// Test whether this JS value has a type `T`.
34
20
///
35
- /// Unlike `is_instance_of`, the type can override this to a specialised
36
- /// check that works reliably with primitives and across realms.
21
+ /// This method will dynamically check to see if this JS object can be
22
+ /// casted to the JS object of type `T`. Usually this uses the `instanceof`
23
+ /// operator. This also works with primitive types like
24
+ /// booleans/strings/numbers as well as cross-realm object like `Array`
25
+ /// which can originate from other iframes.
26
+ ///
27
+ /// In general this is intended to be a more robust version of
28
+ /// `is_instance_of`, but if you want strictly the `instanceof` operator
29
+ /// it's recommended to use that instead.
37
30
fn has_type < T > ( & self ) -> bool
38
31
where
39
32
T : JsCast ,
46
39
///
47
40
/// This method will return `Err(self)` if `self.has_type::<T>()`
48
41
/// returns `false`, and otherwise it will return `Ok(T)` manufactured with
49
- /// an unchecked cast (verified correct via the `instanceof ` operation).
42
+ /// an unchecked cast (verified correct via the `has_type ` operation).
50
43
fn dyn_into < T > ( self ) -> Result < T , Self >
51
44
where
52
45
T : JsCast ,
63
56
///
64
57
/// This method will return `None` if `self.has_type::<T>()`
65
58
/// returns `false`, and otherwise it will return `Some(&T)` manufactured
66
- /// with an unchecked cast (verified correct via the `instanceof ` operation).
59
+ /// with an unchecked cast (verified correct via the `has_type ` operation).
67
60
fn dyn_ref < T > ( & self ) -> Option < & T >
68
61
where
69
62
T : JsCast ,
@@ -107,11 +100,28 @@ where
107
100
T :: unchecked_from_js_ref ( self . as_ref ( ) )
108
101
}
109
102
103
+ /// Test whether this JS value is an instance of the type `T`.
104
+ ///
105
+ /// This method performs a dynamic check (at runtime) using the JS
106
+ /// `instanceof` operator. This method returns `self instanceof T`.
107
+ ///
108
+ /// Note that `instanceof` does not always work with primitive values or
109
+ /// across different realms (e.g. iframes). If you're not sure whether you
110
+ /// specifically need only `instanceof` it's recommended to use `has_type`
111
+ /// instead.
112
+ fn is_instance_of < T > ( & self ) -> bool
113
+ where
114
+ T : JsCast ,
115
+ {
116
+ T :: instanceof ( self . as_ref ( ) )
117
+ }
118
+
110
119
/// Performs a dynamic `instanceof` check to see whether the `JsValue`
111
120
/// provided is an instance of this type.
112
121
///
113
122
/// This is intended to be an internal implementation detail, you likely
114
- /// won't need to call this.
123
+ /// won't need to call this. It's generally called through the
124
+ /// `is_instance_of` method instead.
115
125
fn instanceof ( val : & JsValue ) -> bool ;
116
126
117
127
/// Performs a dynamic check to see whether the `JsValue` provided
0 commit comments