diff options
author | Keegan McAllister <kmcallister@mozilla.com> | 2013-09-18 14:46:42 -0700 |
---|---|---|
committer | Keegan McAllister <kmcallister@mozilla.com> | 2013-09-18 14:46:42 -0700 |
commit | 68ddc6b4ab653072841489a56421f52dd2c1e87a (patch) | |
tree | 15b39e5cd4347f46580a13244758ff6f770e0b9a /src/components/script/dom/bindings/utils.rs | |
parent | 5be084a3b6fe2557519d4cd8506efb6b026464d3 (diff) | |
download | servo-68ddc6b4ab653072841489a56421f52dd2c1e87a.tar.gz servo-68ddc6b4ab653072841489a56421f52dd2c1e87a.zip |
Make DOMString an alias for Option<~str>
Fixes #898.
Diffstat (limited to 'src/components/script/dom/bindings/utils.rs')
-rw-r--r-- | src/components/script/dom/bindings/utils.rs | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index e37f1d68153..8593125e821 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -83,39 +83,26 @@ extern fn InterfaceObjectToString(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) } let name = jsval_to_str(cx, *v).unwrap(); - let retval = str(~"function " + name + "() {\n [native code]\n}"); + let retval = Some(~"function " + name + "() {\n [native code]\n}"); *vp = domstring_to_jsval(cx, &retval); return 1; } } -#[deriving(Clone)] -pub enum DOMString { - str(~str), - null_string -} - -impl DOMString { - pub fn to_str(&self) -> ~str { - match *self { - str(ref s) => s.clone(), - null_string => ~"" - } - } +pub type DOMString = Option<~str>; - pub fn get_ref<'a>(&'a self) -> &'a str { - match *self { - str(ref s) => s.as_slice(), - null_string => &'a "", - } +pub fn null_str_as_empty(s: &DOMString) -> ~str { + // We don't use map_default because it would allocate ~"" even for Some. + match *s { + Some(ref s) => s.clone(), + None => ~"" } +} - // XXX This is temporary until issue #875 is fixed. - pub fn unwrap(&self) -> ~str { - match self { - &str(ref s) => s.clone(), - &null_string => fail!("Cannot unwrap a null string.") - } +pub fn null_str_as_empty_ref<'a>(s: &'a DOMString) -> &'a str { + match *s { + Some(ref s) => s.as_slice(), + None => &'a "" } } @@ -223,10 +210,10 @@ pub fn jsval_to_str(cx: *JSContext, v: JSVal) -> Result<~str, ()> { #[fixed_stack_segment] pub unsafe fn domstring_to_jsval(cx: *JSContext, string: &DOMString) -> JSVal { match string { - &null_string => { + &None => { JSVAL_NULL } - &str(ref s) => { + &Some(ref s) => { do s.as_imm_buf |buf, len| { let cbuf = cast::transmute(buf); RUST_STRING_TO_JSVAL(JS_NewStringCopyN(cx, cbuf, len as libc::size_t)) |