Add a simple ffi crate to test linking. draft
authorRalph Giles <giles@mozilla.com>
Wed, 31 May 2017 12:14:12 -0700
changeset 589159 34a913eac98229e94d819971d0dd777cf27fe61e
parent 589158 14f7721a3ee18258450fd7c83abbef64325d040e
child 589160 ea980970bd9fc9256e92a82a629b9afbb1eae236
push id62262
push usernalexander@mozilla.com
push dateMon, 05 Jun 2017 18:25:07 +0000
milestone55.0a1
Add a simple ffi crate to test linking. nalexander asked about linking rust code into mozglue instead of xul. This is just a simple demo to check. MozReview-Commit-ID: 6oI61r5SLZa
mozglue/rustifoo/Cargo.toml
mozglue/rustifoo/src/lib.rs
new file mode 100644
--- /dev/null
+++ b/mozglue/rustifoo/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "rustifoo"
+version = "0.1.0"
+authors = ["Ralph Giles <giles@mozilla.com>"]
+
+[dependencies]
+lazy_static = "0.2.8"
new file mode 100644
--- /dev/null
+++ b/mozglue/rustifoo/src/lib.rs
@@ -0,0 +1,24 @@
+#[macro_use]
+extern crate lazy_static;
+
+lazy_static! {
+    static ref MSG: std::ffi::CString =
+        std::ffi::CString::new("Hello from Rust!").unwrap();
+}
+
+#[no_mangle]
+pub extern "C" fn rustifoo_msg() -> *const std::os::raw::c_char {
+    MSG.as_ptr()
+}
+
+#[cfg(test)]
+mod tests {
+    #[test]
+    fn it_works() {
+        use std::ffi::CStr;
+        let r = unsafe {
+            CStr::from_ptr(::rustifoo_msg()).to_str().unwrap()
+        };
+        assert_eq!(r, "Hello from Rust!");
+    }
+}