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

@ -22,6 +22,7 @@ log = { workspace = true }
pixels = { path = '../../pixels' }
script_traits = { workspace = true }
servo_url = { path = "../../url" }
strum_macros = { workspace = true }
style_traits = { workspace = true }
webrender_api = { workspace = true }
webrender_traits = { workspace = true }

View file

@ -14,9 +14,11 @@ use embedder_traits::{
use ipc_channel::ipc::IpcSender;
use script_traits::{AnimationTickType, LogEntry, WindowSizeData, WindowSizeType};
use servo_url::ServoUrl;
use strum_macros::IntoStaticStr;
use webrender_traits::CompositorHitTestResult;
/// Messages to the constellation.
#[derive(IntoStaticStr)]
pub enum ConstellationMsg {
/// Exit the constellation.
Exit,
@ -77,41 +79,7 @@ pub enum ConstellationMsg {
impl fmt::Debug for ConstellationMsg {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "ConstellationMsg::{}", self.variant_name())
}
}
impl ConstellationMsg {
/// Return the variant name, for error logging that happens after the message is consumed.
pub fn variant_name(&self) -> &'static str {
use self::ConstellationMsg::*;
match *self {
Exit => "Exit",
GetBrowsingContext(..) => "GetBrowsingContext",
GetPipeline(..) => "GetPipeline",
GetFocusTopLevelBrowsingContext(..) => "GetFocusTopLevelBrowsingContext",
IsReadyToSaveImage(..) => "IsReadyToSaveImage",
AllowNavigationResponse(..) => "AllowNavigationResponse",
LoadUrl(..) => "LoadUrl",
TraverseHistory(..) => "TraverseHistory",
WindowSize(..) => "WindowSize",
ThemeChange(..) => "ThemeChange",
TickAnimation(..) => "TickAnimation",
WebDriverCommand(..) => "WebDriverCommand",
Reload(..) => "Reload",
LogEntry(..) => "LogEntry",
NewWebView(..) => "NewWebView",
CloseWebView(..) => "CloseWebView",
FocusWebView(..) => "FocusWebView",
BlurWebView => "BlurWebView",
SendError(..) => "SendError",
ForwardInputEvent(..) => "ForwardEvent",
SetCursor(..) => "SetCursor",
ToggleProfiler(..) => "EnableProfiler",
ExitFullScreen(..) => "ExitFullScreen",
MediaSessionAction(..) => "MediaSessionAction",
SetWebViewThrottled(..) => "SetWebViewThrottled",
ClearCache => "ClearCache",
}
let variant_string: &'static str = self.into();
write!(formatter, "ConstellationMsg::{variant_string}")
}
}

View file

@ -18,6 +18,7 @@ use ipc_channel::ipc::IpcSender;
use log::warn;
use pixels::Image;
use script_traits::{AnimationState, ScriptThreadMessage, TouchEventResult};
use strum_macros::IntoStaticStr;
use style_traits::CSSPixel;
use webrender_api::DocumentId;
use webrender_traits::{CrossProcessCompositorApi, CrossProcessCompositorMessage};
@ -57,6 +58,7 @@ impl CompositorReceiver {
}
/// Messages from (or via) the constellation thread to the compositor.
#[derive(IntoStaticStr)]
pub enum CompositorMsg {
/// Alerts the compositor that the given pipeline has changed whether it is running animations.
ChangeRunningAnimationsState(WebViewId, PipelineId, AnimationState),
@ -112,24 +114,8 @@ pub struct CompositionPipeline {
}
impl Debug for CompositorMsg {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match *self {
CompositorMsg::ChangeRunningAnimationsState(_, _, state) => {
write!(f, "ChangeRunningAnimationsState({:?})", state)
},
CompositorMsg::CreateOrUpdateWebView(..) => write!(f, "CreateOrUpdateWebView"),
CompositorMsg::RemoveWebView(..) => write!(f, "RemoveWebView"),
CompositorMsg::TouchEventProcessed(..) => write!(f, "TouchEventProcessed"),
CompositorMsg::CreatePng(..) => write!(f, "CreatePng"),
CompositorMsg::IsReadyToSaveImageReply(..) => write!(f, "IsReadyToSaveImageReply"),
CompositorMsg::SetThrottled(..) => write!(f, "SetThrottled"),
CompositorMsg::PipelineExited(..) => write!(f, "PipelineExited"),
CompositorMsg::NewWebRenderFrameReady(..) => write!(f, "NewWebRenderFrameReady"),
CompositorMsg::PendingPaintMetric(..) => write!(f, "PendingPaintMetric"),
CompositorMsg::LoadComplete(..) => write!(f, "LoadComplete"),
CompositorMsg::WebDriverMouseButtonEvent(..) => write!(f, "WebDriverMouseButtonEvent"),
CompositorMsg::WebDriverMouseMoveEvent(..) => write!(f, "WebDriverMouseMoveEvent"),
CompositorMsg::CrossProcess(..) => write!(f, "CrossProcess"),
}
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
let string: &'static str = self.into();
write!(formatter, "{string}")
}
}