Use strum to iterate through enum variants and get their names (#35933)

`strum` allows us to avoid manually listing enum variant names and also
to get their names as static strings. We cannot use this for all cases
due to https://github.com/Peternator7/strum/issues/152, but we can
still use it to remove a lot of code.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-03-13 13:00:31 +01:00 committed by GitHub
parent 959720db0a
commit 294a649a6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 51 additions and 285 deletions

View file

@ -113,6 +113,8 @@ servo_url = { path = "../url" }
smallvec = { workspace = true, features = ["union"] }
style_malloc_size_of = { workspace = true }
glow = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
style = { workspace = true }
stylo_dom = { workspace = true }
style_traits = { workspace = true }

View file

@ -8,6 +8,7 @@ use std::cell::Ref;
use std::collections::HashMap;
use base::id::PipelineId;
use strum::VariantArray;
use crate::messaging::ScriptEventLoopSender;
use crate::task::TaskCanceller;
@ -36,7 +37,7 @@ impl TaskCancellers {
Self::OnePerTaskSource(..) => {
// We must create the canceller if they aren't created because we want future
// tasks to be ignored completely.
for task_source_name in TaskSourceName::all() {
for task_source_name in TaskSourceName::VARIANTS.iter() {
self.get(*task_source_name)
.cancelled
.store(true, Ordering::SeqCst)

View file

@ -10,6 +10,7 @@ use std::default::Default;
use base::id::PipelineId;
use crossbeam_channel::{self, Receiver, Sender};
use strum::VariantArray;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::worker::TrustedWorkerAddress;
@ -211,8 +212,7 @@ impl<T: QueuedTaskConversion> TaskQueue<T> {
self.process_incoming_tasks(first_msg, &fully_active);
let mut throttled = self.throttled.borrow_mut();
let mut throttled_length: usize = throttled.values().map(|queue| queue.len()).sum();
let task_source_names = TaskSourceName::all();
let mut task_source_cycler = task_source_names.iter().cycle();
let mut task_source_cycler = TaskSourceName::VARIANTS.iter().cycle();
// "being busy", is defined as having more than x tasks for this loop's iteration.
// As long as we're not busy, and there are throttled tasks left:
loop {

View file

@ -6,6 +6,7 @@ use std::fmt;
use base::id::PipelineId;
use malloc_size_of_derive::MallocSizeOf;
use strum_macros::VariantArray;
use stylo_atoms::Atom;
use crate::dom::bindings::refcounted::Trusted;
@ -19,10 +20,7 @@ use crate::task_manager::TaskManager;
/// The names of all task sources, used to differentiate TaskCancellers. Note: When adding a task
/// source, update this enum. Note: The HistoryTraversalTaskSource is not part of this, because it
/// doesn't implement TaskSource.
///
/// Note: When adding or removing a [`TaskSourceName`], be sure to also update the return value of
/// [`TaskSourceName::all`].
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, MallocSizeOf, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, MallocSizeOf, PartialEq, VariantArray)]
pub(crate) enum TaskSourceName {
Canvas,
DOMManipulation,
@ -68,28 +66,6 @@ impl From<TaskSourceName> for ScriptThreadEventCategory {
}
}
impl TaskSourceName {
pub(crate) fn all() -> &'static [TaskSourceName] {
&[
TaskSourceName::Canvas,
TaskSourceName::DOMManipulation,
TaskSourceName::FileReading,
TaskSourceName::FontLoading,
TaskSourceName::HistoryTraversal,
TaskSourceName::Networking,
TaskSourceName::PerformanceTimeline,
TaskSourceName::PortMessage,
TaskSourceName::UserInteraction,
TaskSourceName::RemoteEvent,
TaskSourceName::Rendering,
TaskSourceName::MediaElement,
TaskSourceName::WebSocket,
TaskSourceName::Timer,
TaskSourceName::Gamepad,
]
}
}
pub(crate) struct TaskSource<'task_manager> {
pub(crate) task_manager: &'task_manager TaskManager,
pub(crate) name: TaskSourceName,