XPCOM components in Rust
XPCOM components can be written in Rust.
A tiny example
The following example shows a new type that implements nsIObserver.
First, create a new empty crate (e.g. with cargo init --lib), and add the
following dependencies in its Cargo.toml file.
[dependencies]
libc = "0.2"
nserror = { path = "../../../xpcom/rust/nserror" }
nsstring = { path = "../../../xpcom/rust/nsstring" }
xpcom = { path = "../../../xpcom/rust/xpcom" }
(The number of ../ occurrences will depend on the depth of the crate in the
file hierarchy.)
Next hook it into the build system according to the build documentation.
The Rust code will need to import some basic types. xpcom::interfaces
contains all the usual nsI interfaces.
use libc::c_char;
use nserror::nsresult;
use std::sync::atomic::{AtomicBool, Ordering};
use xpcom::{interfaces::nsISupports, RefPtr};
The next part declares the implementation.
#[xpcom(implement(nsIObserver), atomic)]
struct MyObserver {
ran: AtomicBool,
}
This defines the implementation type, which will be refcounted in the specified
way and implement the listed xpidl interfaces. It will also declare a second
initializer struct InitMyObserver which can be used to allocate a new
MyObserver using the MyObserver::allocate method.
Next, all interface methods are declared in the impl block as unsafe methods.
impl MyObserver {
#[allow(non_snake_case)]
unsafe fn Observe(
&self,
_subject: *const nsISupports,
_topic: *const c_char,
_data: *