Introduce TaskOnce

Having both TaskBox and TaskOnce allows us to remove the superfluous inner boxing
from CancellableTask<T>.
This commit is contained in:
Anthony Ramine 2017-09-20 10:37:09 +02:00
parent 52527d6f9d
commit 6c9fb5ae7a
26 changed files with 144 additions and 124 deletions

View file

@ -13,7 +13,7 @@ use servo_atoms::Atom;
use std::fmt;
use std::result::Result;
use std::sync::mpsc::Sender;
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
use task_source::TaskSource;
#[derive(Clone, JSTraceable)]
@ -28,15 +28,15 @@ impl fmt::Debug for DOMManipulationTaskSource {
impl TaskSource for DOMManipulationTaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
let msg = MainThreadScriptMsg::Common(CommonScriptMsg::Task(
ScriptThreadEventCategory::ScriptEvent,
canceller.wrap_task(msg),
box canceller.wrap_task(task),
));
self.0.send(msg).map_err(|_| ())
}
@ -50,7 +50,7 @@ impl DOMManipulationTaskSource {
cancelable: EventCancelable,
window: &Window) {
let target = Trusted::new(target);
let task = box EventTask {
let task = EventTask {
target: target,
name: name,
bubbles: bubbles,
@ -61,6 +61,6 @@ impl DOMManipulationTaskSource {
pub fn queue_simple_event(&self, target: &EventTarget, name: Atom, window: &Window) {
let target = Trusted::new(target);
let _ = self.queue(box SimpleEventTask { target, name }, window.upcast());
let _ = self.queue(SimpleEventTask { target, name }, window.upcast());
}
}

View file

@ -6,7 +6,7 @@ use dom::domexception::DOMErrorName;
use dom::filereader::{FileReader, TrustedFileReader, GenerationId, ReadMetaData};
use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory, ScriptChan};
use std::sync::Arc;
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
use task_source::TaskSource;
#[derive(JSTraceable)]
@ -21,21 +21,21 @@ impl Clone for FileReadingTaskSource {
impl TaskSource for FileReadingTaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
self.0.send(CommonScriptMsg::Task(
ScriptThreadEventCategory::FileRead,
canceller.wrap_task(msg),
box canceller.wrap_task(task),
))
}
}
impl TaskBox for FileReadingTask {
fn run_box(self: Box<Self>) {
impl TaskOnce for FileReadingTask {
fn run_once(self) {
self.handle_task();
}
}

View file

@ -11,21 +11,21 @@ pub mod user_interaction;
use dom::globalscope::GlobalScope;
use std::result::Result;
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
pub trait TaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static;
T: TaskOnce + 'static;
fn queue<T>(&self, msg: Box<T>, global: &GlobalScope) -> Result<(), ()>
fn queue<T>(&self, task: T, global: &GlobalScope) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
self.queue_with_canceller(msg, &global.task_canceller())
self.queue_with_canceller(task, &global.task_canceller())
}
}

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
use task_source::TaskSource;
#[derive(JSTraceable)]
@ -18,15 +18,15 @@ impl Clone for NetworkingTaskSource {
impl TaskSource for NetworkingTaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
self.0.send(CommonScriptMsg::Task(
ScriptThreadEventCategory::NetworkEvent,
canceller.wrap_task(msg),
box canceller.wrap_task(task),
))
}
}
@ -34,10 +34,13 @@ impl TaskSource for NetworkingTaskSource {
impl NetworkingTaskSource {
/// This queues a task that will not be cancelled when its associated
/// global scope gets destroyed.
pub fn queue_unconditionally<T>(&self, msg: Box<T>) -> Result<(), ()>
pub fn queue_unconditionally<T>(&self, task: T) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
self.0.send(CommonScriptMsg::Task(ScriptThreadEventCategory::NetworkEvent, msg))
self.0.send(CommonScriptMsg::Task(
ScriptThreadEventCategory::NetworkEvent,
box task,
))
}
}

View file

@ -11,7 +11,7 @@ use dom::globalscope::GlobalScope;
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use std::fmt;
use std::result::Result;
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
use task_source::TaskSource;
#[derive(JSTraceable)]
@ -32,15 +32,15 @@ impl fmt::Debug for PerformanceTimelineTaskSource {
impl TaskSource for PerformanceTimelineTaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
let msg = CommonScriptMsg::Task(
ScriptThreadEventCategory::PerformanceTimelineTask,
canceller.wrap_task(msg)
box canceller.wrap_task(task)
);
self.0.send(msg).map_err(|_| ())
}
@ -51,7 +51,7 @@ impl PerformanceTimelineTaskSource {
let owner = Trusted::new(&*global.performance());
// FIXME(nox): Why are errors silenced here?
let _ = self.queue(
box task!(notify_performance_observers: move || {
task!(notify_performance_observers: move || {
owner.root().notify_observers();
}),
global,

View file

@ -13,7 +13,7 @@ use servo_atoms::Atom;
use std::fmt;
use std::result::Result;
use std::sync::mpsc::Sender;
use task::{TaskBox, TaskCanceller};
use task::{TaskCanceller, TaskOnce};
use task_source::TaskSource;
#[derive(Clone, JSTraceable)]
@ -28,15 +28,15 @@ impl fmt::Debug for UserInteractionTaskSource {
impl TaskSource for UserInteractionTaskSource {
fn queue_with_canceller<T>(
&self,
msg: Box<T>,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskBox + 'static,
T: TaskOnce + 'static,
{
let msg = MainThreadScriptMsg::Common(CommonScriptMsg::Task(
ScriptThreadEventCategory::InputEvent,
canceller.wrap_task(msg),
box canceller.wrap_task(task),
));
self.0.send(msg).map_err(|_| ())
}
@ -50,7 +50,7 @@ impl UserInteractionTaskSource {
cancelable: EventCancelable,
window: &Window) {
let target = Trusted::new(target);
let task = box EventTask { target, name, bubbles, cancelable };
let task = EventTask { target, name, bubbles, cancelable };
let _ = self.queue(task, window.upcast());
}
}