diff options
author | Agustin Chiappe Berrini <jnieve@gmail.com> | 2018-09-24 19:31:59 -0400 |
---|---|---|
committer | Agustin Chiappe Berrini <jnieve@gmail.com> | 2018-11-14 06:36:44 -0500 |
commit | 75eb94afcaae2f868ecccba5b5dcea4066998d7a (patch) | |
tree | c70e246d752eaee753ca4b59b27e27218a0cdf5e /components/script/task_source/history_traversal.rs | |
parent | 14bc8ab7542287a18ecdede1ca7ee2af077eb8a9 (diff) | |
download | servo-75eb94afcaae2f868ecccba5b5dcea4066998d7a.tar.gz servo-75eb94afcaae2f868ecccba5b5dcea4066998d7a.zip |
Unify the task source and task canceller API
I moved away from the `Window` struct all the logic to handle task
sources, into a new struct called `TaskManager`. In a happy world, I'd
be able to just have there two functions, of the types:
```rust
fn task_source<T: TaskSource>(&self, name: TaskSourceName) -> Box<T>
fn task_source_with_canceller<T: TaskSource>(&self, name: TaskSourceName)
-> (Box<T>, TaskSourceCanceller)
```
And not so much duplicated code. However, because TaskSource can't be a
trait object (because it has generic type parameters), that's not
possible. Instead, I decided to reduce duplicated logic through macros.
For reasons[1], I have to pass both the name of the function with
canceller and the name of the function without, as I'm not able to
concatenate them in the macro itself. I could probably use
`concat_idents` to create both types already defined and reduce the
amount of arguments by one, but that macro is nightly only. At the same
time, not being able to declare macros inside `impl` forces me to pass
`self` as an argument.
All this makes this solution more verbose than it would be ideally. It
does reduce duplication, but it doesn't reduce the size of the file.
[1](https://github.com/rust-lang/rust/issues/29599)
Diffstat (limited to 'components/script/task_source/history_traversal.rs')
-rw-r--r-- | components/script/task_source/history_traversal.rs | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/components/script/task_source/history_traversal.rs b/components/script/task_source/history_traversal.rs index ee73e7e991e..aa6877c9379 100644 --- a/components/script/task_source/history_traversal.rs +++ b/components/script/task_source/history_traversal.rs @@ -2,28 +2,16 @@ * 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 crate::script_runtime::{CommonScriptMsg, ScriptThreadEventCategory}; +use crate::script_thread::MainThreadScriptMsg; +use crate::task::{TaskCanceller, TaskOnce}; +use crate::task_source::{TaskSource, TaskSourceName}; use msg::constellation_msg::PipelineId; -use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory}; -use script_thread::MainThreadScriptMsg; use servo_channel::Sender; -use task::{TaskCanceller, TaskOnce}; -use task_source::{TaskSource, TaskSourceName}; -#[derive(JSTraceable)] +#[derive(Clone, JSTraceable)] pub struct HistoryTraversalTaskSource(pub Sender<MainThreadScriptMsg>, pub PipelineId); -impl ScriptChan for HistoryTraversalTaskSource { - fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> { - self.0 - .send(MainThreadScriptMsg::Common(msg)) - .map_err(|_| ()) - } - - fn clone(&self) -> Box<ScriptChan + Send> { - Box::new(HistoryTraversalTaskSource((&self.0).clone(), (&self.1).clone())) - } -} - impl TaskSource for HistoryTraversalTaskSource { const NAME: TaskSourceName = TaskSourceName::HistoryTraversal; |