script/conversions.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use js::context::JSContext;
6
7/// A version of the `Into<T>` trait from the standard library that can be used
8/// to convert between two types that are not defined in the script crate.
9/// This is intended to be used on dict/enum types generated from WebIDL once
10/// those types are moved out of the script crate.
11pub(crate) trait Convert<T> {
12 fn convert(self) -> T;
13}
14
15/// Same as `Convert`, but also passes a `cx`
16pub(crate) trait ConvertWithCx<T> {
17 fn convert(&self, cx: &mut JSContext) -> T;
18}
19
20/// A version of the `TryInto<T>` trait from the standard library that can be used
21/// to convert between two types that are not defined in the script crate.
22/// This is intended to be used on dict/enum types generated from WebIDL once
23/// those types are moved out of the script crate.
24#[cfg(feature = "webgpu")]
25pub(crate) trait TryConvert<T> {
26 type Error;
27
28 fn try_convert(self) -> Result<T, Self::Error>;
29}