Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 2a718d2

Browse files
committed
Bug 1530467 - Add Array<T> support to xpcom rust bindings, r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D21046 --HG-- extra : moz-landing-system : lando
1 parent 5bc9e5b commit 2a718d2

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xpcom/idl-parser/xpidl/xpidl.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,9 @@ def nativeType(self, calltype):
423423

424424
def rustType(self, calltype):
425425
if self.name == 'nsresult':
426-
return "%s::nserror::nsresult" % (calltype != 'in' and '*mut ' or '')
426+
return "%s::nserror::nsresult" % ('*mut ' if 'out' in calltype else '')
427427

428-
return "%s%s" % (calltype != 'in' and '*mut ' or '',
429-
self.name)
428+
return "%s%s" % ('*mut ' if 'out' in calltype else '', self.name)
430429

431430
def __str__(self):
432431
return "typedef %s %s\n" % (self.type, self.name)
@@ -468,6 +467,8 @@ def nativeType(self, calltype):
468467
def rustType(self, calltype):
469468
if rustBlacklistedForward(self.name):
470469
raise RustNoncompat("forward declaration %s is unsupported" % self.name)
470+
if calltype == 'element':
471+
return 'RefPtr<%s>' % self.name
471472
return "%s*const %s" % (calltype != 'in' and '*mut ' or '',
472473
self.name)
473474

@@ -599,6 +600,11 @@ def rustType(self, calltype, const=False, shared=False):
599600
prefix += '*mut '
600601

601602
if self.specialtype == 'nsid':
603+
if 'element' in calltype:
604+
if self.isPtr(calltype):
605+
raise IDLError("Array<nsIDPtr> not yet supported. "
606+
"File an XPConnect bug if you need it.", self.location)
607+
return self.nativename
602608
return prefix + self.nativename
603609
if self.specialtype in ['cstring', 'utf8string']:
604610
if 'element' in calltype:
@@ -762,6 +768,8 @@ def nativeType(self, calltype, const=False):
762768
'*' if 'out' in calltype else '')
763769

764770
def rustType(self, calltype, const=False):
771+
if calltype == 'element':
772+
return 'RefPtr<%s>' % self.name
765773
return "%s*const %s" % ('*mut ' if 'out' in calltype else '',
766774
self.name)
767775

@@ -1331,9 +1339,16 @@ def nativeType(self, calltype):
13311339
return base
13321340

13331341
def rustType(self, calltype):
1334-
# NOTE: To add Rust support, ensure 'element' is handled correctly in
1335-
# all rustType callees.
1336-
raise RustNoncompat("Array<...> types")
1342+
if calltype == 'legacyelement':
1343+
raise IDLError("[array] Array<T> is unsupported", self.location)
1344+
1345+
base = 'thin_vec::ThinVec<%s>' % self.type.rustType('element')
1346+
if 'out' in calltype:
1347+
return '*mut %s' % base
1348+
elif 'in' == calltype:
1349+
return '*const %s' % base
1350+
else:
1351+
return base
13371352

13381353

13391354
TypeId = namedtuple('TypeId', 'name params')

xpcom/rust/xpcom/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ nsstring = { path = "../nsstring" }
99
nserror = { path = "../nserror" }
1010
threadbound = "0.1"
1111
xpcom_macros = { path = "xpcom_macros" }
12+
thin-vec = { version = "0.1.0", features = ["gecko-ffi"] }

xpcom/rust/xpcom/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate libc;
1515
extern crate nsstring;
1616
extern crate nserror;
1717
extern crate threadbound;
18+
extern crate thin_vec;
1819

1920
// re-export the xpcom_macros macro
2021
#[macro_use]

0 commit comments

Comments
 (0)