Bug 1496622 - convert Rust nsresult type alias to newtype w/#[repr(transparent)] r=froydnj

MozReview-Commit-ID: AkzSS587MvC

Differential Revision: https://phabricator.services.mozilla.com/D7832

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Myk Melez 2018-10-09 16:02:32 +00:00
parent 0fd7f560d6
commit 75ccec036c
3 changed files with 16 additions and 18 deletions

View file

@ -1240,10 +1240,12 @@ use super::nsresult;
""")
output.write("pub const NS_ERROR_MODULE_BASE_OFFSET: u32 = {};\n".format(MODULE_BASE_OFFSET))
output.write("pub const NS_ERROR_MODULE_BASE_OFFSET: nsresult = nsresult({});\n"
.format(MODULE_BASE_OFFSET))
for mod, val in modules.iteritems():
output.write("pub const NS_ERROR_MODULE_{}: u16 = {};\n".format(mod, val.num))
output.write("pub const NS_ERROR_MODULE_{}: nsresult = nsresult({});\n"
.format(mod, val.num))
for error, val in errors.iteritems():
output.write("pub const {}: nsresult = 0x{:X};\n".format(error, val))
output.write("pub const {}: nsresult = nsresult(0x{:X});\n".format(error, val))

View file

@ -142,7 +142,7 @@ def attributeParamList(iface, a, getter):
def attrAsVTableEntry(iface, m, getter):
try:
return "pub %s: unsafe extern \"system\" fn (%s) -> nsresult" % \
return "pub %s: unsafe extern \"system\" fn (%s) -> ::nserror::nsresult" % \
(attributeNativeName(m, getter),
attributeParamList(iface, m, getter))
except xpidl.RustNoncompat as reason:
@ -160,7 +160,7 @@ def methodNativeName(m):
def methodReturnType(m):
if m.notxpcom:
return m.realtype.rustType('in').strip()
return "nsresult"
return "::nserror::nsresult"
def methodRawParamList(iface, m):
@ -257,7 +257,7 @@ def attrAsWrapper(iface, m, getter):
return method_impl_tmpl % {
'name': attributeNativeName(m, getter),
'params': name + ': ' + rust_type,
'ret_ty': 'nsresult',
'ret_ty': '::nserror::nsresult',
'args': name,
}

View file

@ -2,19 +2,15 @@ extern crate nsstring;
use nsstring::{nsCString, nsACString};
/// The type of errors in gecko. This type is currently a type alias, rather
/// than a newtype, in order to conform to the C ABI. In future versions of rust
/// which support RFC #1758 or similar we may be able to use
/// `#[repr(transparent)]` to get a better API for using nsresult.
///
/// The most unfortunate thing about this current implementation is that `u32`
/// and `nsresult` unify.
/// The type of errors in gecko. Uses a newtype to provide additional type
/// safety in Rust and #[repr(transparent)] to ensure the same representation
/// as the C++ equivalent.
#[repr(transparent)]
#[allow(non_camel_case_types)]
pub type nsresult = u32;
#[derive(Clone, Copy, Debug)]
pub struct nsresult(pub u32);
/// An extension trait which is intended to add methods to `nsresult` types.
/// Unfortunately, due to ABI issues, this trait is implemented on all u32
/// types. These methods are meaningless on non-nsresult values.
/// An extension trait that adds methods to `nsresult` types.
pub trait NsresultExt {
fn failed(self) -> bool;
fn succeeded(self) -> bool;
@ -27,7 +23,7 @@ pub trait NsresultExt {
impl NsresultExt for nsresult {
fn failed(self) -> bool {
(self >> 31) != 0
(self.0 >> 31) != 0
}
fn succeeded(self) -> bool {