diff options
author | Martin Robinson <mrobinson@igalia.com> | 2025-03-13 13:00:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-13 12:00:31 +0000 |
commit | 294a649a6c96ea0fba05fad59a41dea6c7f1d66d (patch) | |
tree | 5b3de846c3e4954ff7e4d3af666d3c82108b09f9 /components/shared/script/script_msg.rs | |
parent | 959720db0a15d207e31e44ffd58d76e9e31b18c9 (diff) | |
download | servo-294a649a6c96ea0fba05fad59a41dea6c7f1d66d.tar.gz servo-294a649a6c96ea0fba05fad59a41dea6c7f1d66d.zip |
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>
Diffstat (limited to 'components/shared/script/script_msg.rs')
-rw-r--r-- | components/shared/script/script_msg.rs | 73 |
1 files changed, 7 insertions, 66 deletions
diff --git a/components/shared/script/script_msg.rs b/components/shared/script/script_msg.rs index 7af7272c715..c63233cbc3f 100644 --- a/components/shared/script/script_msg.rs +++ b/components/shared/script/script_msg.rs @@ -22,6 +22,7 @@ use net_traits::CoreResourceMsg; use net_traits::storage_thread::StorageType; use serde::{Deserialize, Serialize}; use servo_url::{ImmutableOrigin, ServoUrl}; +use strum_macros::IntoStaticStr; use style_traits::CSSPixel; #[cfg(feature = "webgpu")] use webgpu::{WebGPU, WebGPUResponse, wgc}; @@ -46,7 +47,7 @@ pub struct IFrameSizeMsg { } /// Messages from the layout to the constellation. -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, IntoStaticStr, Serialize)] pub enum LayoutMsg { /// Requests that the constellation inform the compositor that it needs to record /// the time when the frame with the given ID (epoch) is painted. @@ -55,11 +56,8 @@ pub enum LayoutMsg { impl fmt::Debug for LayoutMsg { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - use self::LayoutMsg::*; - let variant = match *self { - PendingPaintMetric(..) => "PendingPaintMetric", - }; - write!(formatter, "LayoutMsg::{}", variant) + let variant_string: &'static str = self.into(); + write!(formatter, "LayoutMsg::{variant_string}") } } @@ -86,7 +84,7 @@ pub enum LogEntry { } /// Messages from the script to the constellation. -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, IntoStaticStr, Serialize)] pub enum ScriptMsg { /// Request to complete the transfer of a set of ports to a router. CompleteMessagePortTransfer(MessagePortRouterId, Vec<MessagePortId>), @@ -252,65 +250,8 @@ pub enum ScriptMsg { impl fmt::Debug for ScriptMsg { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - use self::ScriptMsg::*; - let variant = match *self { - CompleteMessagePortTransfer(..) => "CompleteMessagePortTransfer", - MessagePortTransferResult(..) => "MessagePortTransferResult", - NewMessagePortRouter(..) => "NewMessagePortRouter", - RemoveMessagePortRouter(..) => "RemoveMessagePortRouter", - NewMessagePort(..) => "NewMessagePort", - RerouteMessagePort(..) => "RerouteMessagePort", - RemoveMessagePort(..) => "RemoveMessagePort", - MessagePortShipped(..) => "MessagePortShipped", - EntanglePorts(..) => "EntanglePorts", - NewBroadcastChannelRouter(..) => "NewBroadcastChannelRouter", - RemoveBroadcastChannelRouter(..) => "RemoveBroadcastChannelRouter", - RemoveBroadcastChannelNameInRouter(..) => "RemoveBroadcastChannelNameInRouter", - NewBroadcastChannelNameInRouter(..) => "NewBroadcastChannelNameInRouter", - ScheduleBroadcast(..) => "ScheduleBroadcast", - ForwardToEmbedder(..) => "ForwardToEmbedder", - BroadcastStorageEvent(..) => "BroadcastStorageEvent", - ChangeRunningAnimationsState(..) => "ChangeRunningAnimationsState", - CreateCanvasPaintThread(..) => "CreateCanvasPaintThread", - Focus => "Focus", - GetBrowsingContextInfo(..) => "GetBrowsingContextInfo", - GetTopForBrowsingContext(..) => "GetParentBrowsingContext", - GetChildBrowsingContextId(..) => "GetChildBrowsingContextId", - LoadComplete => "LoadComplete", - LoadUrl(..) => "LoadUrl", - AbortLoadUrl => "AbortLoadUrl", - PostMessage { .. } => "PostMessage", - NavigatedToFragment(..) => "NavigatedToFragment", - TraverseHistory(..) => "TraverseHistory", - PushHistoryState(..) => "PushHistoryState", - ReplaceHistoryState(..) => "ReplaceHistoryState", - JointSessionHistoryLength(..) => "JointSessionHistoryLength", - RemoveIFrame(..) => "RemoveIFrame", - SetThrottledComplete(..) => "SetThrottledComplete", - ScriptLoadedURLInIFrame(..) => "ScriptLoadedURLInIFrame", - ScriptNewIFrame(..) => "ScriptNewIFrame", - CreateAuxiliaryWebView(..) => "ScriptNewAuxiliary", - ActivateDocument => "ActivateDocument", - SetDocumentState(..) => "SetDocumentState", - SetLayoutEpoch(..) => "SetLayoutEpoch", - SetFinalUrl(..) => "SetFinalUrl", - TouchEventProcessed(..) => "TouchEventProcessed", - LogEntry(..) => "LogEntry", - DiscardDocument => "DiscardDocument", - DiscardTopLevelBrowsingContext => "DiscardTopLevelBrowsingContext", - PipelineExited => "PipelineExited", - ForwardDOMMessage(..) => "ForwardDOMMessage", - ScheduleJob(..) => "ScheduleJob", - MediaSessionEvent(..) => "MediaSessionEvent", - #[cfg(feature = "webgpu")] - RequestAdapter(..) => "RequestAdapter", - #[cfg(feature = "webgpu")] - GetWebGPUChan(..) => "GetWebGPUChan", - TitleChanged(..) => "TitleChanged", - IFrameSizes(..) => "IFramSizes", - ReportMemory(..) => "ReportMemory", - }; - write!(formatter, "ScriptMsg::{}", variant) + let variant_string: &'static str = self.into(); + write!(formatter, "ScriptMsg::{variant_string}") } } |