Skip to content

Commit e0e1836

Browse files
authored
Support creating Rc<T> values from JS values. (#568)
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
1 parent 26c3518 commit e0e1836

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

mozjs/src/conversions.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ pub trait FromJSValConvertible: Sized {
167167
) -> Result<ConversionResult<Self>, ()>;
168168
}
169169

170+
/// A trait to convert `JSVal`s to Rust types inside of Rc wrappers.
171+
pub trait FromJSValConvertibleRc: Sized {
172+
/// Convert `val` to type `Self`.
173+
/// If it returns `Err(())`, a JSAPI exception is pending.
174+
/// If it returns `Ok(Failure(reason))`, there is no pending JSAPI exception.
175+
unsafe fn from_jsval(
176+
cx: *mut JSContext,
177+
val: HandleValue,
178+
) -> Result<ConversionResult<Rc<Self>>, ()>;
179+
}
180+
181+
impl<T: FromJSValConvertibleRc> FromJSValConvertible for Rc<T> {
182+
type Config = ();
183+
184+
unsafe fn from_jsval(
185+
cx: *mut JSContext,
186+
val: HandleValue,
187+
_option: (),
188+
) -> Result<ConversionResult<Rc<T>>, ()> {
189+
<T as FromJSValConvertibleRc>::from_jsval(cx, val)
190+
}
191+
}
192+
170193
/// Behavior for converting out-of-range integers.
171194
#[derive(PartialEq, Eq, Clone)]
172195
pub enum ConversionBehavior {

0 commit comments

Comments
 (0)