flagset/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
//
// Copyright 2019 Red Hat, Inc.
//
// Author: Nathaniel McCallum <npmccallum@redhat.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//! # Welcome to FlagSet!
//!
//! FlagSet is a new, ergonomic approach to handling flags that combines the
//! best of existing crates like `bitflags` and `enumflags` without their
//! downsides.
//!
//! ## Existing Implementations
//!
//! The `bitflags` crate has long been part of the Rust ecosystem.
//! Unfortunately, it doesn't feel like natural Rust. The `bitflags` crate
//! uses a wierd struct format to define flags. Flags themselves are just
//! integers constants, so there is little type-safety involved. But it doesn't
//! have any dependencies. It also allows you to define implied flags (otherwise
//! known as overlapping flags).
//!
//! The `enumflags` crate tried to improve on `bitflags` by using enumerations
//! to define flags. This was a big improvement to the natural feel of the code.
//! Unfortunately, there are some design flaws. To generate the flags,
//! procedural macros were used. This implied two separate crates plus
//! additional dependencies. Further, `enumflags` specifies the size of the
//! flags using a `repr($size)` attribute. Unfortunately, this attribute
//! cannot resolve type aliases, such as `c_int`. This makes `enumflags` a
//! poor fit for FFI, which is the most important place for a flags library.
//! The `enumflags` crate also disallows overlapping flags and is not
//! maintained.
//!
//! FlagSet improves on both of these by adopting the `enumflags` natural feel
//! and the `bitflags` mode of flag generation; as well as additional API usage
//! niceties. FlagSet has no dependencies and is extensively documented and
//! tested. It also tries very hard to prevent you from making mistakes by
//! avoiding external usage of the integer types. FlagSet is also a zero-cost
//! abstraction: all functions are inlineable and should reduce to the core
//! integer operations. FlagSet also does not depend on stdlib, so it can be
//! used in `no_std` libraries and applications.
//!
//! ## Defining Flags
//!
//! Flags are defined using the `flags!` macro:
//!
//! ```
//! use flagset::{FlagSet, flags};
//! use std::os::raw::c_int;
//!
//! flags! {
//! enum FlagsA: u8 {
//! Foo,
//! Bar,
//! Baz,
//! }
//!
//! enum FlagsB: c_int {
//! Foo,
//! Bar,
//! Baz,
//! }
//! }
//! ```
//!
//! Notice that a flag definition looks just like a regular enumeration, with
//! the addition of the field-size type. The field-size type is required and
//! can be either a type or a type alias. Both examples are given above.
//!
//! Also note that the field-size type specifies the size of the corresponding
//! `FlagSet` type, not size of the enumeration itself. To specify the size of
//! the enumeration, use the `repr($size)` attribute as specified below.
//!
//! ## Flag Values
//!
//! Flags often need values assigned to them. This can be done implicitly,
//! where the value depends on the order of the flags:
//!
//! ```
//! use flagset::{FlagSet, flags};
//!
//! flags! {
//! enum Flags: u16 {
//! Foo, // Implicit Value: 0b0001
//! Bar, // Implicit Value: 0b0010
//! Baz, // Implicit Value: 0b0100
//! }
//! }
//! ```
//!
//! Alternatively, flag values can be defined explicitly, by specifying any
//! `const` expression:
//!
//! ```
//! use flagset::{FlagSet, flags};
//!
//! flags! {
//! enum Flags: u16 {
//! Foo = 0x01, // Explicit Value: 0b0001
//! Bar = 2, // Explicit Value: 0b0010
//! Baz = 0b0100, // Explicit Value: 0b0100
//! }
//! }
//! ```
//!
//! Flags can also overlap or "imply" other flags:
//!
//! ```
//! use flagset::{FlagSet, flags};
//!
//! flags! {
//! enum Flags: u16 {
//! Foo = 0b0001,
//! Bar = 0b0010,
//! Baz = 0b0110, // Implies Bar
//! All = (Flags::Foo | Flags::Bar | Flags::Baz).bits(),
//! }
//! }
//! ```
//!
//! ## Specifying Attributes
//!
//! Attributes can be used on the enumeration itself or any of the values:
//!
//! ```
//! use flagset::{FlagSet, flags};
//!
//! flags! {
//! #[derive(PartialOrd, Ord)]
//! enum Flags: u8 {
//! Foo,
//! #[deprecated]
//! Bar,
//! Baz,
//! }
//! }
//! ```
//!
//! ## Collections of Flags
//!
//! A collection of flags is a `FlagSet<T>`. If you are storing the flags in
//! memory, the raw `FlagSet<T>` type should be used. However, if you want to
//! receive flags as an input to a function, you should use
//! `impl Into<FlagSet<T>>`. This allows for very ergonomic APIs:
//!
//! ```
//! use flagset::{FlagSet, flags};
//!
//! flags! {
//! enum Flags: u8 {
//! Foo,
//! Bar,
//! Baz,
//! }
//! }
//!
//! struct Container(FlagSet<Flags>);
//!
//! impl Container {
//! fn new(flags: impl Into<FlagSet<Flags>>) -> Container {
//! Container(flags.into())
//! }
//! }
//!
//! assert_eq!(Container::new(Flags::Foo | Flags::Bar).0.bits(), 0b011);
//! assert_eq!(Container::new(Flags::Foo).0.bits(), 0b001);
//! assert_eq!(Container::new(None).0.bits(), 0b000);
//! ```
//!
//! ## Operations
//!
//! Operations can be performed on a `FlagSet<F>` or on individual flags:
//!
//! | Operator | Assignment Operator | Meaning |
//! |----------|---------------------|------------------------|
//! | \| | \|= | Union |
//! | & | &= | Intersection |
//! | ^ | ^= | Toggle specified flags |
//! | - | -= | Difference |
//! | % | %= | Symmetric difference |
//! | ! | | Toggle all flags |
//!
#![cfg_attr(
feature = "serde",
doc = r#"
## Optional Serde support
[Serde] support can be enabled with the 'serde' feature flag. You can then serialize and
deserialize `FlagSet<T>` to and from any of the [supported formats]:
```
use flagset::{FlagSet, flags};
flags! {
enum Flags: u8 {
Foo,
Bar,
}
}
let flagset = Flags::Foo | Flags::Bar;
let json = serde_json::to_string(&flagset).unwrap();
let flagset: FlagSet<Flags> = serde_json::from_str(&json).unwrap();
assert_eq!(flagset.bits(), 0b011);
```
For serialization and deserialization of flags enum itself, you can use the [`serde_repr`] crate
(or implement `serde::ser::Serialize` and `serde:de::Deserialize` manually), combined with the
appropriate `repr` attribute:
```
use flagset::{FlagSet, flags};
use serde_repr::{Serialize_repr, Deserialize_repr};
flags! {
#[repr(u8)]
#[derive(Deserialize_repr, Serialize_repr)]
enum Flags: u8 {
Foo,
Bar,
}
}
let json = serde_json::to_string(&Flags::Foo).unwrap();
let flag: Flags = serde_json::from_str(&json).unwrap();
assert_eq!(flag, Flags::Foo);
```
[Serde]: https://serde.rs/
[supported formats]: https://serde.rs/#data-formats
[`serde_repr`]: https://crates.io/crates/serde_repr
"#
)]
#![allow(unknown_lints)]
#![warn(clippy::all)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use core::fmt::{Debug, Formatter, Result};
use core::ops::*;
/// Error type returned when creating a new flagset from bits is invalid or undefined.
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u16 {
/// Foo = 0b0001,
/// Bar = 0b0010,
/// Baz = 0b0100,
/// Qux = 0b1010, // Implies Bar
/// }
/// }
///
/// assert_eq!(FlagSet::<Flag>::new(0b01101), Err(flagset::InvalidBits)); // Invalid
/// assert_eq!(FlagSet::<Flag>::new(0b10101), Err(flagset::InvalidBits)); // Unknown
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InvalidBits;
impl core::fmt::Display for InvalidBits {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "invalid bits")
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidBits {}
#[doc(hidden)]
pub trait Flags:
Copy
+ Clone
+ Debug
+ PartialEq
+ Eq
+ BitAnd<Self, Output = FlagSet<Self>>
+ BitOr<Self, Output = FlagSet<Self>>
+ BitXor<Self, Output = FlagSet<Self>>
+ Sub<Self, Output = FlagSet<Self>>
+ Rem<Self, Output = FlagSet<Self>>
+ Not<Output = FlagSet<Self>>
+ Into<FlagSet<Self>>
+ 'static
{
type Type: Copy
+ Clone
+ Debug
+ PartialEq
+ Eq
+ Default
+ BitAnd<Self::Type, Output = Self::Type>
+ BitAndAssign<Self::Type>
+ BitOr<Self::Type, Output = Self::Type>
+ BitOrAssign<Self::Type>
+ BitXor<Self::Type, Output = Self::Type>
+ BitXorAssign<Self::Type>
+ Not<Output = Self::Type>;
/// A slice containing all the possible flag values.
const LIST: &'static [Self];
/// Creates an empty `FlagSet` of this type
#[inline]
fn none() -> FlagSet<Self> {
FlagSet::default()
}
}
#[repr(C)]
#[derive(Copy, Clone, Eq, Hash)]
pub struct FlagSet<F: Flags>(F::Type);
#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct Iter<F: Flags>(FlagSet<F>, usize);
impl<F: Flags> Iterator for Iter<F> {
type Item = F;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while self.1 < F::LIST.len() {
let next = F::LIST[self.1];
self.1 += 1;
if self.0.contains(next) {
return Some(next);
}
}
None
}
}
impl<F: Flags> IntoIterator for FlagSet<F> {
type Item = F;
type IntoIter = Iter<F>;
/// Iterate over the flags in the set.
///
/// **NOTE**: The order in which the flags are iterated is undefined.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let set = Flag::Foo | Flag::Bar;
/// let mut iter = set.into_iter();
/// assert_eq!(iter.next(), Some(Flag::Foo));
/// assert_eq!(iter.next(), Some(Flag::Bar));
/// assert_eq!(iter.next(), None);
/// ```
#[inline]
fn into_iter(self) -> Self::IntoIter {
Iter(self, 0)
}
}
impl<F: Flags> Debug for FlagSet<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "FlagSet(")?;
for (i, flag) in self.into_iter().enumerate() {
write!(f, "{}{:?}", if i > 0 { " | " } else { "" }, flag)?;
}
write!(f, ")")
}
}
impl<F: Flags, R: Copy + Into<FlagSet<F>>> PartialEq<R> for FlagSet<F> {
#[inline]
fn eq(&self, rhs: &R) -> bool {
self.0 == (*rhs).into().0
}
}
impl<F: Flags> AsRef<F::Type> for FlagSet<F> {
#[inline]
fn as_ref(&self) -> &F::Type {
&self.0
}
}
impl<F: Flags> From<Option<FlagSet<F>>> for FlagSet<F> {
/// Converts from `Option<FlagSet<F>>` to `FlagSet<F>`.
///
/// Most notably, this allows for the use of `None` in many places to
/// substitute for manually creating an empty `FlagSet<F>`. See below.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// fn convert(v: impl Into<FlagSet<Flag>>) -> u8 {
/// v.into().bits()
/// }
///
/// assert_eq!(convert(Flag::Foo | Flag::Bar), 0b011);
/// assert_eq!(convert(Flag::Foo), 0b001);
/// assert_eq!(convert(None), 0b000);
/// ```
#[inline]
fn from(value: Option<FlagSet<F>>) -> FlagSet<F> {
value.unwrap_or_default()
}
}
impl<F: Flags> Default for FlagSet<F> {
/// Creates a new, empty FlagSet.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let set = FlagSet::<Flag>::default();
/// assert!(set.is_empty());
/// assert!(!set.is_full());
/// assert!(!set.contains(Flag::Foo));
/// assert!(!set.contains(Flag::Bar));
/// assert!(!set.contains(Flag::Baz));
/// ```
#[inline]
fn default() -> Self {
FlagSet(F::Type::default())
}
}
impl<F: Flags> Not for FlagSet<F> {
type Output = Self;
/// Calculates the complement of the current set.
///
/// In common parlance, this returns the set of all possible flags that are
/// not in the current set.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// #[derive(PartialOrd, Ord)]
/// enum Flag: u8 {
/// Foo = 1 << 0,
/// Bar = 1 << 1,
/// Baz = 1 << 2
/// }
/// }
///
/// let set = !FlagSet::from(Flag::Foo);
/// assert!(!set.is_empty());
/// assert!(!set.is_full());
/// assert!(!set.contains(Flag::Foo));
/// assert!(set.contains(Flag::Bar));
/// assert!(set.contains(Flag::Baz));
/// ```
#[inline]
fn not(self) -> Self {
FlagSet(!self.0)
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitAnd<R> for FlagSet<F> {
type Output = Self;
/// Calculates the intersection of the current set and the specified flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// #[derive(PartialOrd, Ord)]
/// pub enum Flag: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// assert_eq!(set0 & set1, Flag::Bar);
/// assert_eq!(set0 & Flag::Foo, Flag::Foo);
/// assert_eq!(set1 & Flag::Baz, Flag::Baz);
/// ```
#[inline]
fn bitand(self, rhs: R) -> Self {
FlagSet(self.0 & rhs.into().0)
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitAndAssign<R> for FlagSet<F> {
/// Assigns the intersection of the current set and the specified flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u64 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// let mut set1 = Flag::Baz | Flag::Bar;
///
/// set0 &= set1;
/// assert_eq!(set0, Flag::Bar);
///
/// set1 &= Flag::Baz;
/// assert_eq!(set0, Flag::Bar);
/// ```
#[inline]
fn bitand_assign(&mut self, rhs: R) {
self.0 &= rhs.into().0
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitOr<R> for FlagSet<F> {
type Output = Self;
/// Calculates the union of the current set with the specified flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// #[derive(PartialOrd, Ord)]
/// pub enum Flag: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// assert_eq!(set0 | set1, FlagSet::full());
/// ```
#[inline]
fn bitor(self, rhs: R) -> Self {
FlagSet(self.0 | rhs.into().0)
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitOrAssign<R> for FlagSet<F> {
/// Assigns the union of the current set with the specified flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u64 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// let mut set1 = Flag::Bar | Flag::Baz;
///
/// set0 |= set1;
/// assert_eq!(set0, FlagSet::full());
///
/// set1 |= Flag::Baz;
/// assert_eq!(set1, Flag::Bar | Flag::Baz);
/// ```
#[inline]
fn bitor_assign(&mut self, rhs: R) {
self.0 |= rhs.into().0
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitXor<R> for FlagSet<F> {
type Output = Self;
/// Calculates the current set with the specified flags toggled.
///
/// This is commonly known as toggling the presence
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u32 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// assert_eq!(set0 ^ set1, Flag::Foo | Flag::Baz);
/// assert_eq!(set0 ^ Flag::Foo, Flag::Bar);
/// ```
#[inline]
fn bitxor(self, rhs: R) -> Self {
FlagSet(self.0 ^ rhs.into().0)
}
}
impl<F: Flags, R: Into<FlagSet<F>>> BitXorAssign<R> for FlagSet<F> {
/// Assigns the current set with the specified flags toggled.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum Flag: u16 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// let mut set1 = Flag::Baz | Flag::Bar;
///
/// set0 ^= set1;
/// assert_eq!(set0, Flag::Foo | Flag::Baz);
///
/// set1 ^= Flag::Baz;
/// assert_eq!(set1, Flag::Bar);
/// ```
#[inline]
fn bitxor_assign(&mut self, rhs: R) {
self.0 ^= rhs.into().0
}
}
impl<F: Flags, R: Into<FlagSet<F>>> Sub<R> for FlagSet<F> {
type Output = Self;
/// Calculates set difference (the current set without the specified flags).
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// assert_eq!(set0 - set1, Flag::Foo);
/// ```
#[inline]
fn sub(self, rhs: R) -> Self {
self & !rhs.into()
}
}
impl<F: Flags, R: Into<FlagSet<F>>> SubAssign<R> for FlagSet<F> {
/// Assigns set difference (the current set without the specified flags).
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// set0 -= Flag::Baz | Flag::Bar;
/// assert_eq!(set0, Flag::Foo);
/// ```
#[inline]
fn sub_assign(&mut self, rhs: R) {
*self &= !rhs.into();
}
}
impl<F: Flags, R: Into<FlagSet<F>>> Rem<R> for FlagSet<F> {
type Output = Self;
/// Calculates the symmetric difference between two sets.
///
/// The symmetric difference between two sets is the set of all flags
/// that appear in one set or the other, but not both.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// assert_eq!(set0 % set1, Flag::Foo | Flag::Baz);
/// ```
#[inline]
fn rem(self, rhs: R) -> Self {
let rhs = rhs.into();
(self - rhs) | (rhs - self)
}
}
impl<F: Flags, R: Into<FlagSet<F>>> RemAssign<R> for FlagSet<F> {
/// Assigns the symmetric difference between two sets.
///
/// The symmetric difference between two sets is the set of all flags
/// that appear in one set or the other, but not both.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// let set1 = Flag::Baz | Flag::Bar;
/// set0 %= set1;
/// assert_eq!(set0, Flag::Foo | Flag::Baz);
/// ```
#[inline]
fn rem_assign(&mut self, rhs: R) {
*self = *self % rhs
}
}
impl<F: Flags, R: Into<FlagSet<F>>> Extend<R> for FlagSet<F> {
/// Add values by iterating over some collection.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let flag_vec = vec![Flag::Bar, Flag::Baz];
/// let mut some_extended_flags = FlagSet::from(Flag::Foo);
/// some_extended_flags.extend(flag_vec);
/// assert_eq!(some_extended_flags, Flag::Foo | Flag::Bar | Flag::Baz);
/// ```
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = R>,
{
for item in iter {
*self |= item;
}
}
}
impl<F: Flags> FlagSet<F> {
/// Creates a new set from bits; returning `Err(InvalidBits)` on invalid/unknown bits.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u16 {
/// Foo = 0b0001,
/// Bar = 0b0010,
/// Baz = 0b0100,
/// Qux = 0b1010, // Implies Bar
/// }
/// }
///
/// assert_eq!(FlagSet::<Flag>::new(0b00101), Ok(Flag::Foo | Flag::Baz));
/// assert_eq!(FlagSet::<Flag>::new(0b01101), Err(flagset::InvalidBits)); // Invalid
/// assert_eq!(FlagSet::<Flag>::new(0b10101), Err(flagset::InvalidBits)); // Unknown
/// ```
#[inline]
pub fn new(bits: F::Type) -> core::result::Result<Self, InvalidBits> {
if Self::new_truncated(bits).0 == bits {
return Ok(FlagSet(bits));
}
Err(InvalidBits)
}
/// Creates a new set from bits; truncating invalid/unknown bits.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u16 {
/// Foo = 0b0001,
/// Bar = 0b0010,
/// Baz = 0b0100,
/// Qux = 0b1010, // Implies Bar
/// }
/// }
///
/// let set = FlagSet::new_truncated(0b11101); // Has invalid and unknown.
/// assert_eq!(set, Flag::Foo | Flag::Baz);
/// assert_eq!(set.bits(), 0b00101); // Has neither.
/// ```
#[inline]
pub fn new_truncated(bits: F::Type) -> Self {
let mut set = Self::default();
for flag in FlagSet::<F>(bits) {
set |= flag;
}
set
}
/// Creates a new set from bits; use of invalid/unknown bits is undefined.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u16 {
/// Foo = 0b0001,
/// Bar = 0b0010,
/// Baz = 0b0100,
/// Qux = 0b1010, // Implies Bar
/// }
/// }
///
/// // Unknown and invalid bits are retained. Behavior is undefined.
/// const set: FlagSet<Flag> = unsafe { FlagSet::<Flag>::new_unchecked(0b11101) };
/// assert_eq!(set.bits(), 0b11101);
/// ```
///
/// # Safety
///
/// This constructor doesn't check that the bits are valid. If you pass
/// undefined flags, undefined behavior may result.
#[inline]
pub const unsafe fn new_unchecked(bits: F::Type) -> Self {
FlagSet(bits)
}
/// Creates a new FlagSet containing all possible flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let set = FlagSet::full();
/// assert!(!set.is_empty());
/// assert!(set.is_full());
/// assert!(set.contains(Flag::Foo));
/// assert!(set.contains(Flag::Bar));
/// assert!(set.contains(Flag::Baz));
/// ```
#[inline]
pub fn full() -> Self {
let mut set = Self::default();
for f in F::LIST {
set |= *f
}
set
}
/// Returns the raw bits of the set.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u16 {
/// Foo = 0b0001,
/// Bar = 0b0010,
/// Baz = 0b0100,
/// }
/// }
///
/// let set = Flag::Foo | Flag::Baz;
/// assert_eq!(set.bits(), 0b0101u16);
/// ```
#[inline]
pub fn bits(self) -> F::Type {
self.0
}
/// Returns true if the FlagSet contains no flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set = Flag::Foo | Flag::Bar;
/// assert!(!set.is_empty());
///
/// set &= Flag::Baz;
/// assert!(set.is_empty());
/// ```
#[inline]
pub fn is_empty(self) -> bool {
self == Self::default()
}
/// Returns true if the FlagSet contains all possible flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set = Flag::Foo | Flag::Bar;
/// assert!(!set.is_full());
///
/// set |= Flag::Baz;
/// assert!(set.is_full());
/// ```
#[inline]
pub fn is_full(self) -> bool {
self == Self::full()
}
/// Returns true if the two `FlagSet`s do not share any flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let set = Flag::Foo | Flag::Bar;
/// assert!(!set.is_disjoint(Flag::Foo));
/// assert!(!set.is_disjoint(Flag::Foo | Flag::Baz));
/// assert!(set.is_disjoint(Flag::Baz));
/// ```
#[inline]
pub fn is_disjoint(self, rhs: impl Into<FlagSet<F>>) -> bool {
self & rhs == Self::default()
}
/// Returns true if this FlagSet is a superset of the specified flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let set = Flag::Foo | Flag::Bar;
/// assert!(set.contains(Flag::Foo));
/// assert!(set.contains(Flag::Foo | Flag::Bar));
/// assert!(!set.contains(Flag::Foo | Flag::Bar | Flag::Baz));
/// ```
#[inline]
pub fn contains(self, rhs: impl Into<FlagSet<F>>) -> bool {
let rhs = rhs.into();
self & rhs == rhs
}
/// Removes all flags from the FlagSet.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set = Flag::Foo | Flag::Bar;
/// assert!(!set.is_empty());
///
/// set.clear();
/// assert!(set.is_empty());
/// ```
#[inline]
pub fn clear(&mut self) {
*self = Self::default();
}
/// Clears the current set and returns an iterator of all removed flags.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set = Flag::Foo | Flag::Bar;
/// let mut iter = set.drain();
/// assert!(set.is_empty());
/// assert_eq!(iter.next(), Some(Flag::Foo));
/// assert_eq!(iter.next(), Some(Flag::Bar));
/// assert_eq!(iter.next(), None);
/// ```
#[inline]
pub fn drain(&mut self) -> Iter<F> {
let iter = self.into_iter();
*self = Self::default();
iter
}
/// Retain only the flags flags specified by the predicate.
///
/// ```
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// pub enum Flag: u8 {
/// Foo = 1,
/// Bar = 2,
/// Baz = 4
/// }
/// }
///
/// let mut set0 = Flag::Foo | Flag::Bar;
/// set0.retain(|f| f != Flag::Foo);
/// assert_eq!(set0, Flag::Bar);
/// ```
#[inline]
pub fn retain(&mut self, func: impl Fn(F) -> bool) {
for f in self.into_iter() {
if !func(f) {
*self -= f
}
}
}
}
#[cfg(feature = "serde")]
impl<F: Flags> serde::Serialize for FlagSet<F>
where
F::Type: serde::ser::Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
self.0.serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de, F: Flags> serde::Deserialize<'de> for FlagSet<F>
where
F::Type: serde::de::Deserialize<'de>,
{
#[inline]
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
Ok(FlagSet(F::Type::deserialize(deserializer)?))
}
}
/// Define flag value using the `enum` syntax. See below for details.
///
/// Each enumeration value **MUST** have a specified value.
///
/// The width of the bitfield **MUST** also be specified by its integer type.
///
/// It is important to note that the size of the flag enumeration itself is
/// unrelated to the size of the corresponding `FlagSet` instance.
///
/// It is also worth noting that this macro automatically implements a variety
/// of standard traits including:
/// * Copy
/// * Clone
/// * Debug
/// * PartialEq
/// * Eq
/// * From<$enum> for $integer
/// * Not
/// * BitAnd
/// * BitOr
/// * BitXor
/// * Sub
/// * Rem
///
/// ```
/// use std::mem::{align_of, size_of};
/// use flagset::{FlagSet, flags};
///
/// flags! {
/// enum FlagEmpty: u32 {}
///
/// enum Flag8: u8 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
///
/// pub enum Flag16: u16 {
/// Foo,
/// Bar,
/// #[deprecated]
/// Baz,
/// }
///
/// #[derive(PartialOrd, Ord)]
/// enum Flag32: u32 {
/// Foo = 0b001,
/// #[deprecated]
/// Bar = 0b010,
/// Baz = 0b100
/// }
///
/// #[repr(u64)]
/// enum Flag64: u64 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
///
/// #[repr(u32)]
/// enum Flag128: u128 {
/// Foo = 0b001,
/// Bar = 0b010,
/// Baz = 0b100
/// }
/// }
///
/// assert_eq!(size_of::<Flag8>(), 1);
/// assert_eq!(size_of::<Flag16>(), 1);
/// assert_eq!(size_of::<Flag32>(), 1);
/// assert_eq!(size_of::<Flag64>(), 8);
/// assert_eq!(size_of::<Flag128>(), 4);
///
/// assert_eq!(align_of::<Flag8>(), 1);
/// assert_eq!(align_of::<Flag16>(), 1);
/// assert_eq!(align_of::<Flag32>(), 1);
/// assert_eq!(align_of::<Flag64>(), align_of::<u64>());
/// assert_eq!(align_of::<Flag128>(), align_of::<u32>());
///
/// assert_eq!(size_of::<FlagSet<Flag8>>(), size_of::<u8>());
/// assert_eq!(size_of::<FlagSet<Flag16>>(), size_of::<u16>());
/// assert_eq!(size_of::<FlagSet<Flag32>>(), size_of::<u32>());
/// assert_eq!(size_of::<FlagSet<Flag64>>(), size_of::<u64>());
/// assert_eq!(size_of::<FlagSet<Flag128>>(), size_of::<u128>());
///
/// assert_eq!(align_of::<FlagSet<Flag8>>(), align_of::<u8>());
/// assert_eq!(align_of::<FlagSet<Flag16>>(), align_of::<u16>());
/// assert_eq!(align_of::<FlagSet<Flag32>>(), align_of::<u32>());
/// assert_eq!(align_of::<FlagSet<Flag64>>(), align_of::<u64>());
/// assert_eq!(align_of::<FlagSet<Flag128>>(), align_of::<u128>());
/// ```
#[macro_export]
macro_rules! flags {
() => {};
// Entry point for enumerations without values.
($(#[$m:meta])* $p:vis enum $n:ident: $t:ty { $($(#[$a:meta])* $k:ident),+ $(,)* } $($next:tt)*) => {
$crate::flags! { $(#[$m])* $p enum $n: $t { $($(#[$a])* $k = (1 << $n::$k as $t)),+ } $($next)* }
};
// Entrypoint for enumerations with values.
($(#[$m:meta])* $p:vis enum $n:ident: $t:ty { $($(#[$a:meta])*$k:ident = $v:expr),* $(,)* } $($next:tt)*) => {
$(#[$m])*
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
$p enum $n { $($(#[$a])* $k),* }
impl $crate::Flags for $n {
type Type = $t;
const LIST: &'static [Self] = &[$($n::$k),*];
}
impl ::core::convert::From<$n> for $crate::FlagSet<$n> {
#[inline]
fn from(value: $n) -> Self {
unsafe {
match value {
$($n::$k => Self::new_unchecked($v)),*
}
}
}
}
impl ::core::ops::Not for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn not(self) -> Self::Output {
!$crate::FlagSet::from(self)
}
}
impl<R: ::core::convert::Into<$crate::FlagSet<$n>>> ::core::ops::BitAnd<R> for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn bitand(self, rhs: R) -> Self::Output {
$crate::FlagSet::from(self) & rhs
}
}
impl<R: ::core::convert::Into<$crate::FlagSet<$n>>> ::core::ops::BitOr<R> for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn bitor(self, rhs: R) -> Self::Output {
$crate::FlagSet::from(self) | rhs
}
}
impl<R: ::core::convert::Into<$crate::FlagSet<$n>>> ::core::ops::BitXor<R> for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn bitxor(self, rhs: R) -> Self::Output {
$crate::FlagSet::from(self) ^ rhs
}
}
impl<R: ::core::convert::Into<$crate::FlagSet<$n>>> ::core::ops::Sub<R> for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn sub(self, rhs: R) -> Self::Output {
$crate::FlagSet::from(self) - rhs
}
}
impl<R: ::core::convert::Into<$crate::FlagSet<$n>>> ::core::ops::Rem<R> for $n {
type Output = $crate::FlagSet<$n>;
#[inline]
fn rem(self, rhs: R) -> Self::Output {
$crate::FlagSet::from(self) % rhs
}
}
$crate::flags! { $($next)* }
};
}