diff --git a/Cargo.lock b/Cargo.lock index eab78cd13bc9..22573f14acfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. [[package]] name = "Inflector" version = "0.11.2" @@ -358,6 +356,7 @@ name = "bookmark_sync" version = "0.1.0" dependencies = [ "atomic_refcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cstr 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "dogear 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3438,6 +3437,7 @@ name = "xulstore" version = "0.1.0" dependencies = [ "crossbeam-utils 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cstr 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/toolkit/components/places/bookmark_sync/Cargo.toml b/toolkit/components/places/bookmark_sync/Cargo.toml index a461f805930e..adebe959b110 100644 --- a/toolkit/components/places/bookmark_sync/Cargo.toml +++ b/toolkit/components/places/bookmark_sync/Cargo.toml @@ -9,6 +9,7 @@ atomic_refcell = "0.1" dogear = "0.2.3" libc = "0.2" log = "0.4" +cstr = "0.1" moz_task = { path = "../../../../xpcom/rust/moz_task" } nserror = { path = "../../../../xpcom/rust/nserror" } nsstring = { path = "../../../../xpcom/rust/nsstring" } diff --git a/toolkit/components/places/bookmark_sync/src/lib.rs b/toolkit/components/places/bookmark_sync/src/lib.rs index 38ff4d5bc910..a0dadd51c26f 100644 --- a/toolkit/components/places/bookmark_sync/src/lib.rs +++ b/toolkit/components/places/bookmark_sync/src/lib.rs @@ -4,6 +4,8 @@ #![allow(non_snake_case)] +#[macro_use] +extern crate cstr; #[macro_use] extern crate xpcom; diff --git a/toolkit/components/places/bookmark_sync/src/merger.rs b/toolkit/components/places/bookmark_sync/src/merger.rs index f16dfacd0565..52c48c72ef59 100644 --- a/toolkit/components/places/bookmark_sync/src/merger.rs +++ b/toolkit/components/places/bookmark_sync/src/merger.rs @@ -154,7 +154,7 @@ impl MergeTask { .unwrap_or(LevelFilter::Off); let logger = match logger { Some(logger) => Some(ThreadPtrHolder::new( - c_str!("mozISyncedBookmarksMirrorLogger"), + cstr!("mozISyncedBookmarksMirrorLogger"), logger, )?), None => None, @@ -166,7 +166,7 @@ impl MergeTask { local_time_millis: local_time_seconds * 1000, remote_time_millis: remote_time_seconds * 1000, weak_uploads, - callback: ThreadPtrHolder::new(c_str!("mozISyncedBookmarksMirrorCallback"), callback)?, + callback: ThreadPtrHolder::new(cstr!("mozISyncedBookmarksMirrorCallback"), callback)?, result: AtomicRefCell::default(), }) } diff --git a/toolkit/components/xulstore/Cargo.toml b/toolkit/components/xulstore/Cargo.toml index cab38a2e29dc..a27e2493487f 100644 --- a/toolkit/components/xulstore/Cargo.toml +++ b/toolkit/components/xulstore/Cargo.toml @@ -6,6 +6,7 @@ license = "MPL-2.0" [dependencies] crossbeam-utils = "0.6.3" +cstr = "0.1" lazy_static = "1.0" libc = "0.2" lmdb-rkv = "0.11.2" diff --git a/toolkit/components/xulstore/src/lib.rs b/toolkit/components/xulstore/src/lib.rs index 95bb9ba6de4a..a44208a2cfc4 100644 --- a/toolkit/components/xulstore/src/lib.rs +++ b/toolkit/components/xulstore/src/lib.rs @@ -4,6 +4,8 @@ extern crate crossbeam_utils; #[macro_use] +extern crate cstr; +#[macro_use] extern crate failure; #[macro_use] extern crate lazy_static; diff --git a/toolkit/components/xulstore/src/persist.rs b/toolkit/components/xulstore/src/persist.rs index 8868f7099536..01b5286ae642 100644 --- a/toolkit/components/xulstore/src/persist.rs +++ b/toolkit/components/xulstore/src/persist.rs @@ -69,7 +69,7 @@ fn observe_xpcom_shutdown() { let observer = XpcomShutdownObserver::new(); unsafe { obs_svc - .AddObserver(observer.coerce(), c_str!("xpcom-shutdown").as_ptr(), false) + .AddObserver(observer.coerce(), cstr!("xpcom-shutdown").as_ptr(), false) .to_result()? }; Ok(()) diff --git a/toolkit/components/xulstore/src/statics.rs b/toolkit/components/xulstore/src/statics.rs index 289f3b6b2967..0f117db47ffb 100644 --- a/toolkit/components/xulstore/src/statics.rs +++ b/toolkit/components/xulstore/src/statics.rs @@ -79,7 +79,7 @@ fn get_profile_dir() -> XULStoreResult { unsafe { dir_svc .Get( - c_str!("ProfD").as_ptr(), + cstr!("ProfD").as_ptr(), &nsIFile::IID, profile_dir.void_ptr(), ) @@ -87,7 +87,7 @@ fn get_profile_dir() -> XULStoreResult { .or_else(|_| { dir_svc .Get( - c_str!("ProfDS").as_ptr(), + cstr!("ProfDS").as_ptr(), &nsIFile::IID, profile_dir.void_ptr(), ) @@ -131,7 +131,7 @@ fn observe_profile_change() { obs_svc .AddObserver( observer.coerce(), - c_str!("profile-after-change").as_ptr(), + cstr!("profile-after-change").as_ptr(), false, ) .to_result()? diff --git a/xpcom/rust/moz_task/src/lib.rs b/xpcom/rust/moz_task/src/lib.rs index e936c7871694..5e39b7906299 100644 --- a/xpcom/rust/moz_task/src/lib.rs +++ b/xpcom/rust/moz_task/src/lib.rs @@ -18,11 +18,12 @@ use std::{ marker::PhantomData, mem, ptr, sync::atomic::{AtomicBool, Ordering}, + ffi::CStr, }; use xpcom::{ getter_addrefs, interfaces::{nsIEventTarget, nsIRunnable, nsISupports, nsIThread}, - xpcom, xpcom_method, AtomicRefcnt, NulTerminatedCStr, RefCounted, RefPtr, XpCom, + xpcom, xpcom_method, AtomicRefcnt, RefCounted, RefPtr, XpCom, }; extern "C" { @@ -141,7 +142,7 @@ pub type ThreadPtrHandle = RefPtr>; pub struct ThreadPtrHolder { ptr: *const T, marker: PhantomData, - name: NulTerminatedCStr, + name: &'static CStr, owning_thread: RefPtr, refcnt: AtomicRefcnt, } @@ -184,7 +185,7 @@ unsafe impl RefCounted for ThreadPtrHolder { impl ThreadPtrHolder { /// Creates a new owning thread pointer holder. Returns an error if the /// thread manager has shut down. Panics if `name` isn't a valid C string. - pub fn new(name: NulTerminatedCStr, ptr: RefPtr) -> Result, nsresult> { + pub fn new(name: &'static CStr, ptr: RefPtr) -> Result, nsresult> { let owning_thread = get_current_thread()?; // Take ownership of the `RefPtr`. This does _not_ decrement its // refcount, which is what we want. Once we've released all references diff --git a/xpcom/rust/xpcom/src/cstr.rs b/xpcom/rust/xpcom/src/cstr.rs deleted file mode 100644 index 4e658a7c0b21..000000000000 --- a/xpcom/rust/xpcom/src/cstr.rs +++ /dev/null @@ -1,28 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -use libc::c_char; - -/// Creates a static C string by adding a nul terminator to `$str`. -#[macro_export] -macro_rules! c_str { - ($str:expr) => { - $crate::NulTerminatedCStr(concat!($str, '\0').as_bytes()) - }; -} - -/// A nul-terminated, static C string. This should only be created via the -/// `c_str` macro. -pub struct NulTerminatedCStr(pub &'static [u8]); - -impl NulTerminatedCStr { - /// Returns a raw pointer to this string, asserting that it's - /// nul-terminated. This pointer can be passed to any C function expecting a - /// `const char*`, or any XPIDL method expecting an `in string`. - #[inline] - pub fn as_ptr(&self) -> *const c_char { - assert_eq!(self.0.last(), Some(&0), "C strings must be nul-terminated"); - self.0 as *const [u8] as *const c_char - } -} diff --git a/xpcom/rust/xpcom/src/lib.rs b/xpcom/rust/xpcom/src/lib.rs index e77c49b0a230..d6bdfb4192d2 100644 --- a/xpcom/rust/xpcom/src/lib.rs +++ b/xpcom/rust/xpcom/src/lib.rs @@ -28,9 +28,6 @@ pub use xpcom_macros::*; mod base; pub use base::*; -mod cstr; -pub use cstr::*; - // Declarative macro to generate XPCOM method stubs. mod method; pub use method::*;