Move FromJSValConvertible to associated types (avoids old impl check)

This commit is contained in:
Manish Goregaokar 2015-01-31 16:05:16 +05:30
parent 40c9e7f72e
commit 5c9b1019a9
3 changed files with 34 additions and 20 deletions

View file

@ -3016,7 +3016,7 @@ class CGUnionConversionStruct(CGThing):
post="\n}") post="\n}")
return CGWrapper( return CGWrapper(
CGIndenter(method), CGIndenter(method),
pre="impl FromJSValConvertible<()> for %s {\n" % self.type, pre="impl FromJSValConvertible for %s {\ntype Config = ();\n" % self.type,
post="\n}") post="\n}")
def try_method(self, t): def try_method(self, t):

View file

@ -49,12 +49,13 @@ pub trait ToJSValConvertible {
} }
/// A trait to convert `JSVal`s to Rust types. /// A trait to convert `JSVal`s to Rust types.
pub trait FromJSValConvertible<T> { pub trait FromJSValConvertible {
type Config;
/// Convert `val` to type `Self`. /// Convert `val` to type `Self`.
/// Optional configuration of type `T` can be passed as the `option` /// Optional configuration of type `T` can be passed as the `option`
/// argument. /// argument.
/// If it returns `Err(())`, a JSAPI exception is pending. /// If it returns `Err(())`, a JSAPI exception is pending.
fn from_jsval(cx: *mut JSContext, val: JSVal, option: T) -> Result<Self, ()>; fn from_jsval(cx: *mut JSContext, val: JSVal, option: Self::Config) -> Result<Self, ()>;
} }
@ -92,7 +93,8 @@ impl ToJSValConvertible for bool {
} }
} }
impl FromJSValConvertible<()> for bool { impl FromJSValConvertible for bool {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<bool, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<bool, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToBoolean) }; let result = unsafe { convert_from_jsval(cx, val, JS_ValueToBoolean) };
result.map(|b| b != 0) result.map(|b| b != 0)
@ -105,7 +107,8 @@ impl ToJSValConvertible for i8 {
} }
} }
impl FromJSValConvertible<()> for i8 { impl FromJSValConvertible for i8 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i8, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i8) result.map(|v| v as i8)
@ -118,7 +121,8 @@ impl ToJSValConvertible for u8 {
} }
} }
impl FromJSValConvertible<()> for u8 { impl FromJSValConvertible for u8 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u8, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as u8) result.map(|v| v as u8)
@ -131,7 +135,8 @@ impl ToJSValConvertible for i16 {
} }
} }
impl FromJSValConvertible<()> for i16 { impl FromJSValConvertible for i16 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i16, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i16, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i16) result.map(|v| v as i16)
@ -144,7 +149,8 @@ impl ToJSValConvertible for u16 {
} }
} }
impl FromJSValConvertible<()> for u16 { impl FromJSValConvertible for u16 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u16, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u16, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) } unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) }
} }
@ -156,7 +162,8 @@ impl ToJSValConvertible for i32 {
} }
} }
impl FromJSValConvertible<()> for i32 { impl FromJSValConvertible for i32 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i32, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) } unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }
} }
@ -168,7 +175,8 @@ impl ToJSValConvertible for u32 {
} }
} }
impl FromJSValConvertible<()> for u32 { impl FromJSValConvertible for u32 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u32, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) } unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) }
} }
@ -182,7 +190,8 @@ impl ToJSValConvertible for i64 {
} }
} }
impl FromJSValConvertible<()> for i64 { impl FromJSValConvertible for i64 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i64, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<i64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) } unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) }
} }
@ -196,7 +205,8 @@ impl ToJSValConvertible for u64 {
} }
} }
impl FromJSValConvertible<()> for u64 { impl FromJSValConvertible for u64 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u64, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<u64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) } unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) }
} }
@ -210,7 +220,8 @@ impl ToJSValConvertible for f32 {
} }
} }
impl FromJSValConvertible<()> for f32 { impl FromJSValConvertible for f32 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<f32, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<f32, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }; let result = unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) };
result.map(|f| f as f32) result.map(|f| f as f32)
@ -225,7 +236,8 @@ impl ToJSValConvertible for f64 {
} }
} }
impl FromJSValConvertible<()> for f64 { impl FromJSValConvertible for f64 {
type Config = ();
fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<f64, ()> { fn from_jsval(cx: *mut JSContext, val: JSVal, _option: ()) -> Result<f64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) } unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }
} }
@ -285,7 +297,8 @@ pub fn jsid_to_str(cx: *mut JSContext, id: jsid) -> DOMString {
} }
} }
impl FromJSValConvertible<StringificationBehavior> for DOMString { impl FromJSValConvertible for DOMString {
type Config = StringificationBehavior;
fn from_jsval(cx: *mut JSContext, value: JSVal, fn from_jsval(cx: *mut JSContext, value: JSVal,
null_behavior: StringificationBehavior) null_behavior: StringificationBehavior)
-> Result<DOMString, ()> { -> Result<DOMString, ()> {
@ -317,7 +330,8 @@ impl ToJSValConvertible for ByteString {
} }
} }
impl FromJSValConvertible<()> for ByteString { impl FromJSValConvertible for ByteString {
type Config = ();
fn from_jsval(cx: *mut JSContext, value: JSVal, _option: ()) -> Result<ByteString, ()> { fn from_jsval(cx: *mut JSContext, value: JSVal, _option: ()) -> Result<ByteString, ()> {
unsafe { unsafe {
let string = JS_ValueToString(cx, value); let string = JS_ValueToString(cx, value);
@ -462,7 +476,8 @@ pub fn unwrap_jsmanaged<T>(mut obj: *mut JSObject) -> Result<JS<T>, ()>
} }
} }
impl<T: Reflectable+IDLInterface> FromJSValConvertible<()> for JS<T> { impl<T: Reflectable+IDLInterface> FromJSValConvertible for JS<T> {
type Config = ();
fn from_jsval(_cx: *mut JSContext, value: JSVal, _option: ()) -> Result<JS<T>, ()> { fn from_jsval(_cx: *mut JSContext, value: JSVal, _option: ()) -> Result<JS<T>, ()> {
if !value.is_object() { if !value.is_object() {
return Err(()); return Err(());
@ -498,8 +513,8 @@ impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
} }
} }
#[old_impl_check] impl<X: default::Default, T: FromJSValConvertible<Config=X>> FromJSValConvertible for Option<T> {
impl<X: default::Default, T: FromJSValConvertible<X>> FromJSValConvertible<()> for Option<T> { type Config = ();
fn from_jsval(cx: *mut JSContext, value: JSVal, _: ()) -> Result<Option<T>, ()> { fn from_jsval(cx: *mut JSContext, value: JSVal, _: ()) -> Result<Option<T>, ()> {
if value.is_null_or_undefined() { if value.is_null_or_undefined() {
Ok(None) Ok(None)

View file

@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#![feature(unsafe_destructor, plugin, box_syntax, int_uint)] #![feature(unsafe_destructor, plugin, box_syntax, int_uint)]
#![feature(old_impl_check)]
#![deny(unsafe_blocks)] #![deny(unsafe_blocks)]
#![deny(unused_imports)] #![deny(unused_imports)]