wasm-bindgen 0.2.130

Easy support for interacting between JS and Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use core::ops::{Deref, DerefMut};

use crate::convert::JsGeneric;
use crate::sys::JsNullable;
use crate::{JsCast, JsValue};

/// A `T` or `null` at the JS boundary.
///
/// `Nullable<T>` is the Rust-side type for a JS value of type `T | null`, as
/// `Option<T>` is for `T | undefined`, and `Option<Nullable<T>>` is the union
/// `T | null | undefined`. All three are accepted in the same positions: as an
/// argument or return value of exported and imported functions, as a public
/// field of an exported struct, and as a closure argument or return value,
/// including the `&`/`&mut` reference forms `Option<T>` supports.
///
/// | Type | JS type | JS → Rust | Rust → JS |
/// |---|---|---|---|
/// | `Option<T>` | `T \| undefined` | `undefined` and `null` → `None`; anything else goes to `T`'s conversion | `None` → `undefined` |
/// | `Nullable<T>` | `T \| null` | `null` → `Null`; anything else, `undefined` included, goes to `T`'s conversion | `Null` → `null` |
/// | `Option<Nullable<T>>` | `T \| null \| undefined` | `undefined` → `None`, `null` → `Some(Null)`, else `Some(Value(..))` | `None` → `undefined`, `Some(Null)` → `null` |
///
/// The `Option` layer of `Option<Nullable<T>>` treats only `undefined` as
/// `None`, since `null` has to reach the `Nullable` layer.
/// `opt.and_then(Nullable::value)` collapses the union to an
/// `Option<T>`. No other nesting is supported: `Nullable<Nullable<T>>`,
/// `Nullable<Option<T>>` and `Option<Option<T>>` do not cross the boundary.
///
/// Both may also name a slot of a js-sys `Function<fn(..)>` signature or a
/// `Promise<T>` resolution. Where a js-sys container's type-erased methods
/// marshal the element itself, as `Array::get` and `Map::get` do, the
/// element must be a handle: use the JS-side [`JsOption<T>`] and
/// [`JsNullable<T>`] there, which stay JS values rather than converting.
///
/// [`JsOption<T>`]: crate::sys::JsOption
/// [`JsNullable<T>`]: crate::sys::JsNullable
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Nullable<T> {
    /// JS `null`.
    Null,
    /// A value.
    Value(T),
}

impl<T> Default for Nullable<T> {
    #[inline]
    fn default() -> Self {
        Nullable::Null
    }
}

impl<T> Nullable<T> {
    /// Returns `true` if this is `Null`.
    #[inline]
    pub const fn is_null(&self) -> bool {
        matches!(self, Nullable::Null)
    }

    /// Returns `true` if this is a `Value`.
    #[inline]
    pub const fn is_value(&self) -> bool {
        matches!(self, Nullable::Value(_))
    }

    /// Converts from `&Nullable<T>` to `Nullable<&T>`.
    #[inline]
    pub const fn as_ref(&self) -> Nullable<&T> {
        match self {
            Nullable::Null => Nullable::Null,
            Nullable::Value(v) => Nullable::Value(v),
        }
    }

    /// Converts from `&mut Nullable<T>` to `Nullable<&mut T>`.
    #[inline]
    pub fn as_mut(&mut self) -> Nullable<&mut T> {
        match self {
            Nullable::Null => Nullable::Null,
            Nullable::Value(v) => Nullable::Value(v),
        }
    }

    /// Maps a `Nullable<T>` to `Nullable<U>` by applying a function to a
    /// contained value, leaving `Null` as it is.
    #[inline]
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Nullable<U> {
        match self {
            Nullable::Null => Nullable::Null,
            Nullable::Value(v) => Nullable::Value(f(v)),
        }
    }

    /// Returns the contained value as `Some`, or `None` for `Null`.
    #[inline]
    pub fn value(self) -> Option<T> {
        match self {
            Nullable::Value(v) => Some(v),
            Nullable::Null => None,
        }
    }

    /// Returns the contained value as `Some`, or `None` for `Null`.
    #[inline]
    pub fn into_option(self) -> Option<T> {
        self.value()
    }

    /// Returns the contained value.
    ///
    /// # Panics
    ///
    /// Panics if this is `Null`.
    #[inline]
    #[track_caller]
    pub fn unwrap(self) -> T {
        match self {
            Nullable::Value(v) => v,
            Nullable::Null => panic!("called `Nullable::unwrap()` on a `Null` value"),
        }
    }

    /// Returns the contained value.
    ///
    /// # Panics
    ///
    /// Panics with `msg` if this is `Null`.
    #[inline]
    #[track_caller]
    pub fn expect(self, msg: &str) -> T {
        match self {
            Nullable::Value(v) => v,
            Nullable::Null => panic!("{}", msg),
        }
    }

    /// Returns the contained value or `default`.
    #[inline]
    pub fn unwrap_or(self, default: T) -> T {
        self.value().unwrap_or(default)
    }

    /// Returns the contained value or computes it from `f`.
    #[inline]
    pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
        self.value().unwrap_or_else(f)
    }

    /// Returns the contained value or `T::default()`.
    #[inline]
    pub fn unwrap_or_default(self) -> T
    where
        T: Default,
    {
        self.value().unwrap_or_default()
    }

    /// Returns `true` if this is a `Value` and `f` holds for it.
    #[inline]
    pub fn is_value_and(self, f: impl FnOnce(T) -> bool) -> bool {
        match self {
            Nullable::Value(v) => f(v),
            Nullable::Null => false,
        }
    }

    /// Returns `true` if this is `Null`, or a `Value` for which `f` holds.
    #[inline]
    pub fn is_null_or(self, f: impl FnOnce(T) -> bool) -> bool {
        match self {
            Nullable::Value(v) => f(v),
            Nullable::Null => true,
        }
    }

    /// Returns the contained value as `Ok`, or `Err(err)` for `Null`.
    #[inline]
    pub fn ok_or<E>(self, err: E) -> Result<T, E> {
        match self {
            Nullable::Value(v) => Ok(v),
            Nullable::Null => Err(err),
        }
    }

    /// Returns the contained value as `Ok`, or `Err(err())` for `Null`.
    #[inline]
    pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
        match self {
            Nullable::Value(v) => Ok(v),
            Nullable::Null => Err(err()),
        }
    }

    /// Returns `Null` for `Null`, otherwise calls `f` on the contained value.
    #[inline]
    pub fn and_then<U, F: FnOnce(T) -> Nullable<U>>(self, f: F) -> Nullable<U> {
        match self {
            Nullable::Value(v) => f(v),
            Nullable::Null => Nullable::Null,
        }
    }

    /// Returns the contained value if `predicate` holds for it, otherwise
    /// `Null`.
    #[inline]
    pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Nullable<T> {
        match self {
            Nullable::Value(v) if predicate(&v) => Nullable::Value(v),
            _ => Nullable::Null,
        }
    }

    /// Takes the value out, leaving `Null` in its place.
    #[inline]
    pub fn take(&mut self) -> Nullable<T> {
        core::mem::replace(self, Nullable::Null)
    }

    /// Replaces the contents with `Value(value)`, returning the old contents.
    #[inline]
    pub fn replace(&mut self, value: T) -> Nullable<T> {
        core::mem::replace(self, Nullable::Value(value))
    }

    /// Returns an iterator over the contained value, if any.
    #[inline]
    pub fn iter(&self) -> core::option::IntoIter<&T> {
        self.as_ref().value().into_iter()
    }

    /// Returns a mutable iterator over the contained value, if any.
    #[inline]
    pub fn iter_mut(&mut self) -> core::option::IntoIter<&mut T> {
        self.as_mut().value().into_iter()
    }
}

impl<T: Deref> Nullable<T> {
    /// Converts from `Nullable<T>` (or `&Nullable<T>`) to
    /// `Nullable<&T::Target>`.
    #[inline]
    pub fn as_deref(&self) -> Nullable<&T::Target> {
        self.as_ref().map(|v| v.deref())
    }
}

impl<T: DerefMut> Nullable<T> {
    /// Converts from `Nullable<T>` (or `&mut Nullable<T>`) to
    /// `Nullable<&mut T::Target>`.
    #[inline]
    pub fn as_deref_mut(&mut self) -> Nullable<&mut T::Target> {
        self.as_mut().map(|v| v.deref_mut())
    }
}

impl<T: Copy> Nullable<&T> {
    /// Maps a `Nullable<&T>` to a `Nullable<T>` by copying the referent.
    #[inline]
    pub fn copied(self) -> Nullable<T> {
        self.map(|&v| v)
    }
}

impl<T: Clone> Nullable<&T> {
    /// Maps a `Nullable<&T>` to a `Nullable<T>` by cloning the referent.
    #[inline]
    pub fn cloned(self) -> Nullable<T> {
        self.map(|v| v.clone())
    }
}

impl<T: Copy> Nullable<&mut T> {
    /// Maps a `Nullable<&mut T>` to a `Nullable<T>` by copying the referent.
    #[inline]
    pub fn copied(self) -> Nullable<T> {
        self.map(|&mut v| v)
    }
}

impl<T: Clone> Nullable<&mut T> {
    /// Maps a `Nullable<&mut T>` to a `Nullable<T>` by cloning the referent.
    #[inline]
    pub fn cloned(self) -> Nullable<T> {
        self.map(|v| v.clone())
    }
}

impl<T> IntoIterator for Nullable<T> {
    type Item = T;
    type IntoIter = core::option::IntoIter<T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.value().into_iter()
    }
}

impl<'a, T> IntoIterator for &'a Nullable<T> {
    type Item = &'a T;
    type IntoIter = core::option::IntoIter<&'a T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T> IntoIterator for &'a mut Nullable<T> {
    type Item = &'a mut T;
    type IntoIter = core::option::IntoIter<&'a mut T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// `?` on a `Nullable<T>` under `--cfg wasm_bindgen_unstable_try_trait`,
/// which needs nightly for `try_trait_v2`. `Null` short-circuits a function
/// returning `Nullable<U>` to `Null` and one returning `Option<U>` to `None`,
/// and `None` from an `Option<T>` short-circuits a `Nullable<U>` function to
/// `Null`.
#[cfg(wasm_bindgen_unstable_try_trait)]
mod try_trait {
    use core::convert::Infallible;
    use core::ops::{ControlFlow, FromResidual, Residual, Try};

    use super::Nullable;

    impl<T> Residual<T> for Nullable<Infallible> {
        type TryType = Nullable<T>;
    }

    impl<T> Try for Nullable<T> {
        type Output = T;
        type Residual = Nullable<Infallible>;

        #[inline]
        fn from_output(output: T) -> Self {
            Nullable::Value(output)
        }

        #[inline]
        fn branch(self) -> ControlFlow<Self::Residual, T> {
            match self {
                Nullable::Value(v) => ControlFlow::Continue(v),
                Nullable::Null => ControlFlow::Break(Nullable::Null),
            }
        }
    }

    impl<T> FromResidual<Nullable<Infallible>> for Nullable<T> {
        #[inline]
        fn from_residual(_: Nullable<Infallible>) -> Self {
            Nullable::Null
        }
    }

    impl<T> FromResidual<Option<Infallible>> for Nullable<T> {
        #[inline]
        fn from_residual(_: Option<Infallible>) -> Self {
            Nullable::Null
        }
    }

    impl<T> FromResidual<Nullable<Infallible>> for Option<T> {
        #[inline]
        fn from_residual(_: Nullable<Infallible>) -> Self {
            None
        }
    }
}

impl<T> From<T> for Nullable<T> {
    #[inline]
    fn from(value: T) -> Self {
        Nullable::Value(value)
    }
}

impl<T> From<Option<T>> for Nullable<T> {
    #[inline]
    fn from(option: Option<T>) -> Self {
        match option {
            Some(v) => Nullable::Value(v),
            None => Nullable::Null,
        }
    }
}

impl<T> From<Nullable<T>> for Option<T> {
    #[inline]
    fn from(nullable: Nullable<T>) -> Self {
        nullable.value()
    }
}

impl<T: Into<JsValue>> From<Nullable<T>> for JsValue {
    #[inline]
    fn from(nullable: Nullable<T>) -> Self {
        match nullable {
            Nullable::Null => JsValue::NULL,
            Nullable::Value(v) => v.into(),
        }
    }
}

impl<T: JsGeneric> From<Nullable<T>> for JsNullable<T> {
    #[inline]
    fn from(nullable: Nullable<T>) -> Self {
        match nullable {
            Nullable::Null => JsNullable::new(),
            Nullable::Value(v) => JsNullable::wrap(v),
        }
    }
}

/// `None` is `undefined`, which `JsNullable` admits as its other empty state.
impl<T: JsGeneric> From<Option<Nullable<T>>> for JsNullable<T> {
    #[inline]
    fn from(option: Option<Nullable<T>>) -> Self {
        match option {
            Some(nullable) => nullable.into(),
            None => JsValue::UNDEFINED.unchecked_into(),
        }
    }
}

impl<T: JsGeneric> From<JsNullable<T>> for Option<Nullable<T>> {
    #[inline]
    fn from(nullable: JsNullable<T>) -> Self {
        if JsValue::is_undefined(&nullable) {
            None
        } else if JsValue::is_null(&nullable) {
            Some(Nullable::Null)
        } else {
            Some(Nullable::Value(nullable.unchecked_into()))
        }
    }
}