Skip to content

Commit

Permalink
Bug 1874035 - Add custom Debug derives for bitflags with custom deriv…
Browse files Browse the repository at this point in the history
…es. r=gfx-reviewers,emilio,ErichDonGubler

bitflags 2 has a shortcoming with using custom derives: you can't use
custom derives (for e.g. MallocSizeOf) at the same time as bitflags's for
the derives it supports.
See bitflags/bitflags#395

Differential Revision: https://phabricator.services.mozilla.com/D199941
  • Loading branch information
glandium committed Jan 30, 2024
1 parent 54cc50d commit fb2aa57
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 10 deletions.
13 changes: 12 additions & 1 deletion build/rust/bitflags/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

pub use bitflags::bitflags as bitflags2;
pub use bitflags::parser;

// Copy of the macro from bitflags 1.3.2, with the implicit derives
// removed, because in 2.0, they're expected to be explicitly given
Expand All @@ -30,9 +31,19 @@ macro_rules! bitflags {
$($t:tt)*
) => {
$(#[$($outer)+])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
$vis struct $BitFlags($T);

impl core::fmt::Debug for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
core::write!(f, "{:#x}", Self::empty().bits())
} else {
$crate::parser::to_writer(self, f)
}
}
}

bitflags2! {
impl $BitFlags: $T {
$(
Expand Down
12 changes: 11 additions & 1 deletion gfx/wr/webrender/src/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ impl From<ClipItemKey> for ClipNode {
// Flags that are attached to instances of clip nodes.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
pub struct ClipNodeFlags(u8);

bitflags! {
Expand All @@ -905,6 +905,16 @@ bitflags! {
}
}

impl core::fmt::Debug for ClipNodeFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

// When a clip node is found to be valid for a
// clip chain instance, it's stored in an index
// buffer style structure. This struct contains
Expand Down
12 changes: 11 additions & 1 deletion gfx/wr/webrender/src/gpu_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ pub struct MaskInstance {
/// code should process this instance.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
pub struct BrushFlags(u16);

bitflags! {
Expand Down Expand Up @@ -646,6 +646,16 @@ bitflags! {
}
}

impl core::fmt::Debug for BrushFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

/// Convenience structure to encode into PrimitiveInstanceData.
pub struct BrushInstance {
pub prim_header_index: PrimitiveHeaderIndex,
Expand Down
12 changes: 11 additions & 1 deletion gfx/wr/webrender/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const MAX_SEGMENTS: usize = 64;
/// `write_transform_vertex()` function.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, MallocSizeOf)]
pub struct EdgeAaSegmentMask(u8);

bitflags! {
Expand All @@ -86,6 +86,16 @@ bitflags! {
}
}

impl core::fmt::Debug for EdgeAaSegmentMask {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

bitflags! {
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
pub struct ItemFlags: u8 {
Expand Down
24 changes: 22 additions & 2 deletions gfx/wr/webrender_api/src/display_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub type ItemTag = (u64, u16);
pub type ItemKey = u16;

#[repr(C)]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
pub struct PrimitiveFlags(u8);

bitflags! {
Expand All @@ -59,6 +59,16 @@ bitflags! {
}
}

impl core::fmt::Debug for PrimitiveFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

impl Default for PrimitiveFlags {
fn default() -> Self {
PrimitiveFlags::IS_BACKFACE_VISIBLE
Expand Down Expand Up @@ -933,7 +943,7 @@ impl Hash for RasterSpace {
}

#[repr(C)]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
pub struct StackingContextFlags(u8);

bitflags! {
Expand All @@ -948,6 +958,16 @@ bitflags! {
}
}

impl core::fmt::Debug for StackingContextFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

impl Default for StackingContextFlags {
fn default() -> Self {
StackingContextFlags::empty()
Expand Down
12 changes: 11 additions & 1 deletion gfx/wr/webrender_api/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Default for GlyphOptions {
}

#[repr(C)]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, MallocSizeOf, Serialize, PeekPoke)]
pub struct FontInstanceFlags(u32);

bitflags! {
Expand Down Expand Up @@ -220,6 +220,16 @@ bitflags! {
}
}

impl core::fmt::Debug for FontInstanceFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

impl Default for FontInstanceFlags {
#[cfg(target_os = "windows")]
fn default() -> FontInstanceFlags {
Expand Down
24 changes: 22 additions & 2 deletions gfx/wr/webrender_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ pub enum IntParameter {

/// Flags to track why we are rendering.
#[repr(C)]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Default, Deserialize, MallocSizeOf, Serialize)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Default, Deserialize, MallocSizeOf, Serialize)]
pub struct RenderReasons(u32);

bitflags! {
Expand Down Expand Up @@ -633,13 +633,23 @@ bitflags! {
}
}

impl core::fmt::Debug for RenderReasons {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

impl RenderReasons {
pub const NUM_BITS: u32 = 17;
}

/// Flags to enable/disable various builtin debugging tools.
#[repr(C)]
#[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Default, Deserialize, MallocSizeOf, Serialize)]
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Default, Deserialize, MallocSizeOf, Serialize)]
pub struct DebugFlags(u32);

bitflags! {
Expand Down Expand Up @@ -713,6 +723,16 @@ bitflags! {
}
}

impl core::fmt::Debug for DebugFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

/// Information specific to a primitive type that
/// uniquely identifies a primitive template by key.
#[derive(Debug, Clone, Eq, MallocSizeOf, PartialEq, Hash, Serialize, Deserialize)]
Expand Down
12 changes: 11 additions & 1 deletion servo/components/selectors/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn split_from_end<T>(s: &[T], at: usize) -> (&[T], &[T]) {
}

/// Flags that indicate at which point of parsing a selector are we.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, ToShmem)]
#[derive(Clone, Copy, Default, Eq, PartialEq, ToShmem)]
pub(crate) struct SelectorFlags(u8);

bitflags! {
Expand All @@ -181,6 +181,16 @@ bitflags! {
}
}

impl core::fmt::Debug for SelectorFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_empty() {
write!(f, "{:#x}", Self::empty().bits())
} else {
bitflags::parser::to_writer(self, f)
}
}
}

impl SelectorFlags {
/// When you nest a pseudo-element with something like:
///
Expand Down

0 comments on commit fb2aa57

Please sign in to comment.