chore: change Borders::NONE to a proper const (#1985)

https://docs.rs/bitflags/latest/bitflags/#zero-bit-flags

> Flags with no bits set should be avoided because they interact
strangely with
[Flags::contains](https://docs.rs/bitflags/latest/bitflags/trait.Flags.html#method.contains)
and
[Flags::intersects](https://docs.rs/bitflags/latest/bitflags/trait.Flags.html#method.intersects).
A zero-bit flag is always contained, but is never intersected. The names
of zero-bit flags can be parsed, but are never formatted.

Removing this simplifies the manual Debug impl that previously had to
check for Borders::NONE and now does not.
This commit is contained in:
Josh McKinney 2025-07-13 22:56:21 -07:00 committed by GitHub
parent 40e96a2a04
commit 0e10170e19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,8 +10,6 @@ bitflags! {
#[derive(Default, Clone, Copy, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Borders: u8 {
/// Show no border (default)
const NONE = 0b0000;
/// Show the top border
const TOP = 0b0001;
/// Show the right border
@ -25,6 +23,11 @@ bitflags! {
}
}
impl Borders {
/// Show no border (default)
pub const NONE: Self = Self::empty();
}
/// The type of border of a [`Block`](crate::block::Block).
///
/// See the [`borders`](crate::block::Block::borders) method of `Block` to configure its borders.
@ -172,13 +175,11 @@ impl BorderType {
}
}
/// Implement the `Debug` trait for the `Borders` bitflags. This is a manual implementation to
/// display the flags in a more readable way. The default implementation would display the
/// flags as 'Border(0x0)' for `Borders::NONE` for example.
impl fmt::Debug for Borders {
/// Display the Borders bitflags as a list of names. For example, `Borders::NONE` will be
/// displayed as `NONE` and `Borders::ALL` will be displayed as `ALL`. If multiple flags are
/// set, they will be displayed separated by a pipe character.
/// Display the Borders bitflags as a list of names.
///
/// `Borders::NONE` is displayed as `NONE` and `Borders::ALL` is displayed as `ALL`. If multiple
/// flags are set, they are otherwise displayed separated by a pipe character.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_empty() {
return write!(f, "NONE");
@ -186,17 +187,12 @@ impl fmt::Debug for Borders {
if self.is_all() {
return write!(f, "ALL");
}
let mut first = true;
for (name, border) in self.iter_names() {
if border == Self::NONE {
continue;
}
if first {
write!(f, "{name}")?;
first = false;
} else {
write!(f, " | {name}")?;
}
let mut names = self.iter_names().map(|(name, _)| name);
if let Some(first) = names.next() {
write!(f, "{first}")?;
}
for name in names {
write!(f, " | {name}")?;
}
Ok(())
}