Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to allow constructing Attributes in a const context #817

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/style/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ impl BitXor for Attributes {
}

impl Attributes {
/// Returns the empty bitset.
#[inline(always)]
pub const fn none() -> Self {
Self(0)
}

/// Returns a copy of the bitset with the given attribute set.
/// If it's already set, this returns the bitset unmodified.
#[inline(always)]
pub const fn with(self, attribute: Attribute) -> Self {
Self(self.0 | attribute.bytes())
}

/// Returns a copy of the bitset with the given attribute unset.
/// If it's not set, this returns the bitset unmodified.
#[inline(always)]
pub const fn without(self, attribute: Attribute) -> Self {
Self(self.0 & !attribute.bytes())
}

/// Sets the attribute.
/// If it's already set, this does nothing.
#[inline(always)]
Expand Down Expand Up @@ -117,4 +137,11 @@ mod tests {
attributes.toggle(Attribute::Bold);
assert!(attributes.is_empty());
}

#[test]
fn test_attributes_const() {
const ATTRIBUTES: Attributes = Attributes::none().with(Attribute::Bold).with(Attribute::Italic).without(Attribute::Bold);
assert!(!ATTRIBUTES.has(Attribute::Bold));
assert!(ATTRIBUTES.has(Attribute::Italic));
}
}