/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
usecrate::{
dispatch_background_task_runnable, dispatch_runnable, get_current_thread, DispatchOptions,
}; use nserror::{nsresult, NS_OK}; use nsstring::nsACString; use std::sync::Mutex; use xpcom::interfaces::{nsIEventTarget, nsIRunnablePriority}; use xpcom::xpcom;
/// Basic wrapper to convert a FnOnce callback into a `nsIRunnable` to be /// dispatched using XPCOM. #[xpcom(implement(nsIRunnable, nsINamed, nsIRunnablePriority), atomic)] struct RunnableFunction<F: FnOnce() + 'static> {
name: &'static str,
priority: u32,
function: Mutex<Option<F>>,
}
impl<F: FnOnce() + 'static> RunnableFunction<F> { #[allow(non_snake_case)] fn Run(&self) -> nsresult { let function = self.function.lock().unwrap().take();
debug_assert!(function.is_some(), "runnable invoked twice?"); iflet Some(function) = function {
function();
}
NS_OK
}
impl<F> RunnableBuilder<F> where
F: FnOnce() + Send + 'static,
{ /// Dispatch this Runnable to the specified EventTarget. The runnable function must be `Send`. pubfn dispatch(self, target: &nsIEventTarget) -> Result<(), nsresult> { let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
}); unsafe { dispatch_runnable(runnable.coerce(), target, self.options) }
}
/// Dispatch this Runnable to the specified EventTarget as a background /// task. The runnable function must be `Send`. pubfn dispatch_background_task(self) -> Result<(), nsresult> { let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
}); unsafe { dispatch_background_task_runnable(runnable.coerce(), self.options) }
}
}
impl<F> RunnableBuilder<F> where
F: FnOnce() + 'static,
{ /// Dispatch this Runnable to the current thread. /// /// Unlike `dispatch` and `dispatch_background_task`, the runnable does not /// need to be `Send` to dispatch to the current thread. pubfn dispatch_local(self) -> Result<(), nsresult> { let target = get_current_thread()?; let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
}); unsafe { dispatch_runnable(runnable.coerce(), target.coerce(), self.options) }
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.