Skip to content

Commit 04502cf

Browse files
authored
Merge branch 'bitflags:main' into main
2 parents 1f779b7 + 10b9fd3 commit 04502cf

14 files changed

Lines changed: 125 additions & 45 deletions

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
2525

2626
- name: Install Rust Toolchain
27-
run: rustup default ${{ matrix.channel }}
27+
run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }}
2828

2929
- name: Install cargo-hack
3030
run: cargo install cargo-hack

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# 2.7.0
2+
3+
## What's Changed
4+
* Fix `clippy::doc_lazy_continuation` lints by @waywardmonkeys in https://github.com/bitflags/bitflags/pull/414
5+
* Run clippy on extra features in CI. by @waywardmonkeys in https://github.com/bitflags/bitflags/pull/415
6+
* Fix CI: trybuild refresh, allow some clippy restrictions. by @waywardmonkeys in https://github.com/bitflags/bitflags/pull/417
7+
* Update zerocopy version in example by @KodrAus in https://github.com/bitflags/bitflags/pull/422
8+
* Add method to check if unknown bits are set by @wysiwys in https://github.com/bitflags/bitflags/pull/426
9+
* Update error messages by @KodrAus in https://github.com/bitflags/bitflags/pull/427
10+
* Add `truncate(&mut self)` method to unset unknown bits by @wysiwys in https://github.com/bitflags/bitflags/pull/428
11+
* Update error messages by @KodrAus in https://github.com/bitflags/bitflags/pull/429
12+
13+
## New Contributors
14+
* @wysiwys made their first contribution in https://github.com/bitflags/bitflags/pull/426
15+
16+
**Full Changelog**: https://github.com/bitflags/bitflags/compare/2.6.0...2.7.0
17+
118
# 2.6.0
219

320
## What's Changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "bitflags"
33
# NB: When modifying, also modify the number in readme (for breaking changes)
4-
version = "2.6.0"
4+
version = "2.7.0"
55
edition = "2021"
66
rust-version = "1.56.0"
77
authors = ["The Rust Project Developers"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Add this to your `Cargo.toml`:
2828

2929
```toml
3030
[dependencies]
31-
bitflags = "2.6.0"
31+
bitflags = "2.7.0"
3232
```
3333

3434
and this to your source code:

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Add `bitflags` to your `Cargo.toml`:
1717
1818
```toml
1919
[dependencies.bitflags]
20-
version = "2.6.0"
20+
version = "2.7.0"
2121
```
2222
2323
## Generating flags types

src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ mod match_bitflag;
2222
mod parser;
2323
mod remove;
2424
mod symmetric_difference;
25+
mod truncate;
2526
mod union;
27+
mod unknown;
2628

2729
bitflags! {
2830
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]

src/tests/truncate.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use super::*;
2+
3+
use crate::Flags;
4+
5+
#[test]
6+
fn cases() {
7+
case(
8+
TestFlags::ABC | TestFlags::from_bits_retain(1 << 3),
9+
TestFlags::ABC,
10+
);
11+
12+
case(TestZero::empty(), TestZero::empty());
13+
14+
case(TestZero::all(), TestZero::all());
15+
16+
case(
17+
TestFlags::from_bits_retain(1 << 3) | TestFlags::all(),
18+
TestFlags::all(),
19+
);
20+
}
21+
22+
#[track_caller]
23+
fn case<T: Flags + std::fmt::Debug>(mut before: T, after: T)
24+
where
25+
T: std::fmt::Debug + PartialEq + Copy,
26+
{
27+
before.truncate();
28+
assert_eq!(before, after, "{:?}.truncate()", before);
29+
}

src/tests/unknown.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use super::*;
2+
3+
use crate::Flags;
4+
5+
#[test]
6+
fn cases() {
7+
case(false, TestFlags::empty(), TestFlags::contains_unknown_bits);
8+
case(false, TestFlags::A, TestFlags::contains_unknown_bits);
9+
10+
case(
11+
true,
12+
TestFlags::ABC | TestFlags::from_bits_retain(1 << 3),
13+
TestFlags::contains_unknown_bits,
14+
);
15+
16+
case(
17+
true,
18+
TestFlags::empty() | TestFlags::from_bits_retain(1 << 3),
19+
TestFlags::contains_unknown_bits,
20+
);
21+
22+
case(false, TestFlags::all(), TestFlags::contains_unknown_bits);
23+
24+
case(false, TestZero::empty(), TestZero::contains_unknown_bits);
25+
}
26+
#[track_caller]
27+
fn case<T: Flags + std::fmt::Debug>(expected: bool, value: T, inherent: impl FnOnce(&T) -> bool) {
28+
assert_eq!(
29+
expected,
30+
inherent(&value),
31+
"{:?}.contains_unknown_bits()",
32+
value
33+
);
34+
assert_eq!(
35+
expected,
36+
Flags::contains_unknown_bits(&value),
37+
"Flags::contains_unknown_bits({:?})",
38+
value
39+
);
40+
}

src/traits.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ pub trait Flags: Sized + 'static {
152152
Self::from_bits_retain(truncated)
153153
}
154154

155+
/// This method will return `true` if any unknown bits are set.
156+
fn contains_unknown_bits(&self) -> bool {
157+
Self::all().bits() & self.bits() != self.bits()
158+
}
159+
155160
/// Get the underlying bits value.
156161
///
157162
/// The returned value is exactly the bits set in this flags value.
@@ -241,6 +246,14 @@ pub trait Flags: Sized + 'static {
241246
self.bits() & other.bits() == other.bits()
242247
}
243248

249+
/// Remove any unknown bits from the flags.
250+
fn truncate(&mut self)
251+
where
252+
Self: Sized,
253+
{
254+
*self = Self::from_bits_truncate(self.bits());
255+
}
256+
244257
/// The bitwise or (`|`) of the bits in two flags values.
245258
fn insert(&mut self, other: Self)
246259
where

tests/compile-fail/access_outside_visibility.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ note: the struct `Flags2` is defined here
1010
4 | / bitflags! {
1111
5 | | pub struct Flags1: u32 {
1212
6 | | const FLAG_A = 0b00000001;
13-
7 | | }
1413
... |
15-
11 | | }
1614
12 | | }
1715
| |_____^
1816
= note: this error originates in the macro `$crate::__declare_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)