1#![no_std]
2#![allow(non_snake_case)]
3use vstd::arithmetic::power::*;
35use vstd::arithmetic::power2::*;
36use vstd::bits::*;
37use vstd::prelude::*;
38
39pub use paste;
40
41#[doc(hidden)]
42#[macro_export]
43macro_rules! __bitflags_cfg_expr {
44 (() $base:expr) => {
45 $base
46 };
47 ((#[cfg($($cfg:tt)*)] $($rest:tt)*) $base:expr) => {
48 (cfg!($($cfg)*) && $crate::__bitflags_cfg_expr!(($($rest)*) $base))
49 };
50 ((#[$_other:meta] $($rest:tt)*) $base:expr) => {
51 $crate::__bitflags_cfg_expr!(($($rest)*) $base)
52 };
53}
54
55#[doc(hidden)]
56#[macro_export]
57macro_rules! __bitflags_cfg_guarded_expr {
58 (() $e:expr) => {
59 $e
60 };
61 ((#[cfg($($cfg:tt)*)] $($rest:tt)*) $e:expr) => {{
62 #[cfg($($cfg)*)]
63 {
64 $crate::__bitflags_cfg_guarded_expr!(($($rest)*) $e)
65 }
66 #[cfg(not($($cfg)*))]
67 {
68 true
69 }
70 }};
71 ((#[$_other:meta] $($rest:tt)*) $e:expr) => {
72 $crate::__bitflags_cfg_guarded_expr!(($($rest)*) $e)
73 };
74}
75
76#[doc(hidden)]
77#[macro_export]
78macro_rules! __bitflags_cfg_guarded_stmt {
79 (() $stmt:stmt) => {
80 $stmt
81 };
82 ((#[cfg($($cfg:tt)*)] $($rest:tt)*) $stmt:stmt) => {
83 #[cfg($($cfg)*)]
84 {
85 $crate::__bitflags_cfg_guarded_stmt!(($($rest)*) $stmt)
86 }
87 };
88 ((#[$_other:meta] $($rest:tt)*) $stmt:stmt) => {
89 $crate::__bitflags_cfg_guarded_stmt!(($($rest)*) $stmt)
90 };
91}
92
93#[doc(hidden)]
96#[macro_export]
97macro_rules! __bitflags_value {
98 ($T:ty, $value:expr) => {{
99 let value: $T = $value;
100 value
101 }};
102}
103
104#[doc(hidden)]
106#[macro_export]
107macro_rules! __bitflags_cfg_value {
108 (() $T:ty, $value:expr) => { $crate::__bitflags_value!($T, $value) };
109 ((#[cfg($($cfg:tt)*)] $($rest:tt)*) $T:ty, $value:expr) => {{
110 #[cfg($($cfg)*)]
111 { $crate::__bitflags_cfg_value!(($($rest)*) $T, $value) }
112 #[cfg(not($($cfg)*))]
113 { 0 as $T }
114 }};
115 ((#[$_other:meta] $($rest:tt)*) $T:ty, $value:expr) => {
116 $crate::__bitflags_cfg_value!(($($rest)*) $T, $value)
117 };
118}
119#[doc(hidden)]
120#[macro_export]
121macro_rules! __bitflags_name {
122 (() $name:expr) => { $name };
123 ((#[bitflags(flag_name = $name:expr)] $($rest:tt)*) $old:expr) => {
124 $crate::__bitflags_name!(($($rest)*) $name)
125 };
126 ((#[$_other:meta] $($rest:tt)*) $name:expr) => {
127 $crate::__bitflags_name!(($($rest)*) $name)
128 };
129}
130#[doc(hidden)]
131#[macro_export]
132macro_rules! __bitflags_const {
133 ((#[bitflags $($args:tt)*] $($rest:tt)*) [$($attrs:tt)*] $($item:tt)*) => {
134 $crate::__bitflags_const!(($($rest)*) [$($attrs)*] $($item)*);
135 };
136 ((#[$attr:meta] $($rest:tt)*) [$($attrs:tt)*] $($item:tt)*) => {
137 $crate::__bitflags_const!(($($rest)*) [$($attrs)* #[$attr]] $($item)*);
138 };
139 (() [$($attrs:tt)*] $($item:tt)*) => {
140 ::vstd::prelude::verus! { $($attrs)* $($item)* }
141};
142}
143#[doc(hidden)]
144#[macro_export]
145macro_rules! __bitflags_flag {
146 (
147 {
148 name: _,
149 named: { $($named:tt)* },
150 unnamed: { $($unnamed:tt)* },
151 }
152 ) => {
153 $($unnamed)*
154 };
155 (
156 {
157 name: $Flag:ident,
158 named: { $($named:tt)* },
159 unnamed: { $($unnamed:tt)* },
160 }
161 ) => {
162 $($named)*
163 };
164}
165
166#[macro_export]
188macro_rules! bitflags {
189 (
190 $(#[$outer:meta])*
191 $vis:vis struct $name:ident: $T:ty {
192 $(
193 $(#[$inner:ident $($args:tt)*])*
194 const $Flag:ident = $value:expr;
195 )*
196 }
197 $($rest:tt)*
198 ) => {
199 $crate::paste::paste! {
200 ::vstd::prelude::verus! {
201 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
204 #[repr(transparent)]
205 struct [< __Bitflags $name >] { bits: $T }
206
207 $(#[$outer])*
208 $vis struct $name([< __Bitflags $name >]);
209
210 impl ::vstd::view::View for $name {
211 type V = ::vstd::set::Set<[< __ghost $name >]>;
212
213 open spec fn view(&self) -> Self::V {
214 Self::flags_from_bits(self.bits())
215 }
216 }
217
218 #[allow(non_camel_case_types)]
220 pub ghost enum [< __ghost $name >] {
221 $(
222 [< __ghost $Flag >],
223 )*
224 }
225
226 impl [< __ghost $name >] {
227 pub open spec fn enabled(self) -> bool {
228 match self {
229 $(
230 [< __ghost $name >]::[< __ghost $Flag >] => $crate::__bitflags_cfg_expr! {
231 ($(#[$inner $($args)*])*) true
232 },
233 )*
234 }
235 }
236
237 pub open spec fn bit(self) -> $T {
238 match self {
239 $(
240 [< __ghost $name >]::[< __ghost $Flag >] => $name::[< $Flag _SPEC >].bits(),
241 )*
242 }
243 }
244 }
245
246 impl ::vstd::set_lib::FiniteFull for [< __ghost $name >] {
247 proof fn full_properties() {
248 let s = ::vstd::iset::ISet::empty()
249 $(.insert([< __ghost $name >]::[< __ghost $Flag >]))*;
250 assert(::vstd::iset::ISet::new(|a: [< __ghost $name >]| true) =~= s);
251 }
252 }
253
254 impl $name {
255 pub open spec fn flags_spec(&self) -> ::vstd::set::Set<[< __ghost $name >]> {
256 Self::flags_from_bits(self.bits())
257 }
258
259 pub open spec fn flags_from_bits(bits: $T) -> ::vstd::set::Set<[< __ghost $name >]> {
260 ::vstd::set::Set::< [< __ghost $name >] >::from_finite_type(|flag: [< __ghost $name >]| {
261 flag.enabled() && (bits & flag.bit()) == flag.bit()
262 })
263 }
264
265 closed spec fn from_bits_unchecked_spec(bits: $T) -> Self {
266 Self([< __Bitflags $name >] { bits })
267 }
268
269 #[verifier::when_used_as_spec(from_bits_unchecked_spec)]
270 const fn from_bits_unchecked(bits: $T) -> (r: Self)
271 ensures
272 r.bits() == bits,
273 r.flags_spec() == Self::flags_from_bits(bits),
274 returns
275 Self::from_bits_unchecked(bits),
276 {
277 Self([< __Bitflags $name >] { bits })
278 }
279
280 $(
281 pub closed spec const [< $Flag _SPEC >]: Self =
282 Self::from_bits_unchecked_spec($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value));
283 $crate::__bitflags_const! { ($(#[$inner $($args)*])*) []
284 const [< __ $Flag _NAME >]: &'static str =
285 $crate::__bitflags_name!(($(#[$inner $($args)*])*) ::core::stringify!($Flag));
286 }
287
288 $crate::__bitflags_const! { ($(#[$inner $($args)*])*) []
289 #[verifier::when_used_as_spec([< $Flag _SPEC >])]
290 pub exec const $Flag: Self
291 ensures
292 Self::$Flag.bits() == $crate::__bitflags_value!($T, $value),
293 Self::$Flag.flags_spec() == Self::flags_from_bits($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value)),
294 Self::$Flag == Self::[< $Flag _SPEC >],
295 {
296 Self::from_bits_unchecked($crate::__bitflags_value!($T, $value))
297 }
298 }
299 )*
300
301 closed spec fn declared_bits_spec() -> $T {
303 (0 as $T) $(| (
304 if $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true } {
305 ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
306 } else {
307 0 as $T
308 }
309 ))*
310 }
311
312 pub proof fn lemma_all_constant()
314 ensures
315 Self::all().bits() == Self::all_spec().bits_spec(),
316 Self::all_spec().bits_spec() == (0 as $T) $(| (
317 if $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true } {
318 ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
319 } else {
320 0 as $T
321 }
322 ))*,
323 Self::all().bits() == (0 as $T) $(| (
324 if $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true } {
325 ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
326 } else {
327 0 as $T
328 }
329 ))*,
330 {
331 }
332
333 pub broadcast proof fn lemma_consts()
334 ensures
335 #![trigger Self::all().bits()]
336 Self::all().bits() == (0 as $T) $(| (
337 if $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true } {
338 ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
339 } else {
340 0 as $T
341 }
342 ))*,
343 $(
344 #![trigger $crate::__bitflags_cfg_guarded_expr!(
345 ($(#[$inner $($args)*])*)
346 Self::$Flag.bits()
347 )]
348 $crate::__bitflags_cfg_guarded_expr!(
349 ($(#[$inner $($args)*])*)
350 Self::$Flag.bits() == ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
351 ),
352 )*
353 {
354 Self::lemma_all_constant();
355 }
356
357 pub proof fn lemma_from_bits_bits(bits: $T)
358 requires
359 bits & Self::all().bits() == bits,
360 ensures
361 Self::from_bits(bits)->0.bits() == bits,
362 {
363 }
364
365 pub proof fn lemma_eq_from_bits(left: Self, right: Self)
366 requires
367 left.bits() == right.bits(),
368 ensures
369 left == right,
370 {
371 }
372
373 pub closed spec fn bits_spec(&self) -> $T { self.0.bits }
375
376 #[verifier::when_used_as_spec(bits_spec)]
377 pub const fn bits(&self) -> $T
378 returns self.bits(),
379 {
380 self.0.bits
381 }
382
383 pub closed spec fn empty_spec() -> Self {
384 Self::from_bits_unchecked_spec(0)
385 }
386
387 #[verifier::when_used_as_spec(empty_spec)]
388 pub const fn empty() -> (r: Self)
389 ensures
390 r.bits() == 0,
391 r.flags_spec() == Self::flags_from_bits(0),
392 returns Self::empty(),
393 {
394 Self::from_bits_unchecked(0)
395 }
396
397 pub closed spec fn all_spec() -> Self {
398 Self::from_bits_unchecked_spec(Self::declared_bits_spec())
399 }
400
401 #[verifier::when_used_as_spec(all_spec)]
402 pub const fn all() -> (r: Self)
403 ensures
404 r == Self::all_spec(),
405 r.bits() == Self::all().bits(),
406 r.flags_spec() == Self::flags_from_bits(Self::all().bits()),
407 {
408 Self::from_bits_unchecked(
409 (0 as $T) $(| (
410 if $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true } {
411 ($crate::__bitflags_cfg_value!(($(#[$inner $($args)*])*) $T, $value))
412 } else {
413 0 as $T
414 }
415 ))*
416 )
417 }
418
419 pub closed spec fn known_bits_spec(&self) -> $T {
421 self.bits() & Self::all().bits()
422 }
423
424 #[verifier::when_used_as_spec(known_bits_spec)]
425 pub const fn known_bits(&self) -> (r: $T)
426 returns self.known_bits(),
427 {
428 self.0.bits & Self::all().bits()
429 }
430
431 pub closed spec fn unknown_bits_spec(&self) -> $T {
433 self.bits() & !Self::all().bits()
434 }
435
436 #[verifier::when_used_as_spec(unknown_bits_spec)]
437 pub const fn unknown_bits(&self) -> (r: $T)
438 returns self.unknown_bits(),
439 {
440 self.0.bits & !Self::all().bits()
441 }
442
443 pub open spec fn contains_unknown_bits_spec(&self) -> bool {
445 self.unknown_bits() != 0
446 }
447
448 #[verifier::when_used_as_spec(contains_unknown_bits_spec)]
449 pub const fn contains_unknown_bits(&self) -> (r: bool)
450 returns self.contains_unknown_bits(),
451 {
452 self.unknown_bits() != 0
453 }
454
455 pub open spec fn is_empty_spec(&self) -> bool {
457 self.bits() == 0
458 }
459
460 #[verifier::when_used_as_spec(is_empty_spec)]
461 pub const fn is_empty(&self) -> (r: bool)
462 returns self.is_empty(),
463 {
464 self.0.bits == 0
465 }
466
467 pub open spec fn is_all_spec(&self) -> bool {
469 Self::all().bits() | self.bits() == self.bits()
470 }
471
472 #[verifier::when_used_as_spec(is_all_spec)]
473 pub const fn is_all(&self) -> (r: bool)
474 returns self.is_all(),
475 {
476 Self::all().bits() | self.0.bits == self.0.bits
477 }
478
479 pub open spec fn contains_spec(&self, other: Self) -> bool {
481 (self.bits() & other.bits()) == other.bits()
482 }
483
484 pub open spec fn contains_flags_spec(&self, other: Self) -> bool {
485 self.contains(other)
486 }
487
488 #[verifier::when_used_as_spec(contains_spec)]
489 pub const fn contains(&self, other: Self) -> (r: bool)
490 returns self.contains(other),
491 {
492 (self.0.bits & other.0.bits) == other.0.bits
493 }
494
495 pub open spec fn intersects_spec(&self, other: Self) -> bool {
497 (self.bits() & other.bits()) != 0
498 }
499
500 #[verifier::when_used_as_spec(intersects_spec)]
501 pub const fn intersects(&self, other: Self) -> (r: bool)
502 returns self.intersects(other),
503 {
504 (self.0.bits & other.0.bits) != 0
505 }
506
507 pub closed spec fn from_bits_truncate_spec(bits: $T) -> Self {
508 Self::from_bits_unchecked_spec(bits & Self::all().bits())
509 }
510
511 pub closed spec fn from_bits_retain_spec(bits: $T) -> Self {
512 Self::from_bits_unchecked_spec(bits)
513 }
514
515 #[verifier::when_used_as_spec(from_bits_retain_spec)]
516 pub const fn from_bits_retain(bits: $T) -> (r: Self)
517 ensures
518 r.bits() == bits,
519 r.flags_spec() == Self::flags_from_bits(bits),
520 returns Self::from_bits_retain(bits),
521 {
522 Self::from_bits_unchecked(bits)
523 }
524
525 pub open spec fn from_name_spec(name: &str) -> Option<Self> {
526 let found: Option<Self> = None;
527 $(
528 let found = if found is None
529 && $crate::__bitflags_cfg_expr! { ($(#[$inner $($args)*])*) true }
530 && $crate::__bitflags_cfg_guarded_expr! {
531 ($(#[$inner $($args)*])*)
532 ::vstd::prelude::spec_eq(::vstd::view::View::view(name), ::vstd::view::View::view(
533 $crate::__bitflags_name!(($(#[$inner $($args)*])*) ::core::stringify!($Flag))
534 ))
535 }
536 {
537 Some(Self::[< $Flag _SPEC >])
538 } else {
539 found
540 };
541 )*
542 found
543 }
544
545 #[verifier::when_used_as_spec(from_name_spec)]
548 pub fn from_name(name: &str) -> Option<Self>
549 returns Self::from_name(name),
550 {
551 $(
552 $crate::__bitflags_flag!({
553 name: $Flag,
554 named: {
555 $crate::__bitflags_cfg_guarded_stmt!(
556 ($(#[$inner $($args)*])*)
557 {
558 if name == Self::[< __ $Flag _NAME >] {
559 return ::core::option::Option::Some(
560 Self::$Flag
561 );
562 }
563 }
564 );
565 },
566 unnamed: {},
567 });
568 )*
569 ::core::option::Option::None
570 }
571
572 #[verifier::when_used_as_spec(from_bits_truncate_spec)]
573 pub const fn from_bits_truncate(bits: $T) -> (r: Self)
574 ensures
575 r.bits() == (bits & Self::all().bits()),
576 r.flags_spec() == Self::flags_from_bits(bits & Self::all().bits()),
577 returns Self::from_bits_truncate(bits),
578 {
579 Self::from_bits_unchecked(bits & Self::all().bits())
580 }
581
582 pub closed spec fn from_bits_spec(bits: $T) -> Option<Self> {
583 if (bits & Self::all().bits()) == bits {
584 Some(Self::from_bits_unchecked_spec(bits))
585 } else {
586 None
587 }
588 }
589
590 #[verifier::when_used_as_spec(from_bits_spec)]
591 pub const fn from_bits(bits: $T) -> (r: Option<Self>)
592 ensures
593 r is Some == ((bits & Self::all().bits()) == bits),
594 r matches Some(flags_value) ==> {
595 &&& flags_value.bits() == bits
596 &&& flags_value.flags_spec() == Self::flags_from_bits(bits)
597 },
598 returns
599 Self::from_bits(bits),
600 {
601 let truncated = Self::from_bits_truncate(bits).bits();
602 if truncated == bits {
603 Some(Self::from_bits_retain(bits))
604 } else {
605 None
606 }
607 }
608
609 pub closed spec fn remove_spec(self, other: Self) -> Self {
610 Self::from_bits_unchecked_spec(self.bits() & !other.bits())
611 }
612
613 pub fn insert(&mut self, other: Self)
614 ensures
615 final(self).bits() == (old(self).bits() | other.bits()),
616 final(self).flags_spec() == Self::flags_from_bits(
617 old(self).bits() | other.bits(),
618 ),
619 {
620 *self = Self::from_bits_retain(self.bits()).union(other);
621 }
622
623 pub fn remove(&mut self, other: Self)
624 ensures
625 *final(self) == old(self).remove_spec(other),
626 final(self).bits() == (old(self).bits() & !other.bits()),
627 final(self).flags_spec() == Self::flags_from_bits(
628 old(self).bits() & !other.bits(),
629 ),
630 {
631 *self = Self::from_bits_retain(self.bits()).difference(other);
632 }
633
634 pub fn toggle(&mut self, other: Self)
636 ensures
637 final(self).bits() == (old(self).bits() ^ other.bits()),
638 final(self).flags_spec() == Self::flags_from_bits(
639 old(self).bits() ^ other.bits(),
640 ),
641 {
642 *self = Self::from_bits_retain(self.bits()).symmetric_difference(other);
643 }
644
645 pub fn set(&mut self, other: Self, value: bool)
647 ensures
648 value ==> final(self).bits() == (old(self).bits() | other.bits()),
649 !value ==> final(self).bits() == (old(self).bits() & !other.bits()),
650 {
651 if value {
652 self.insert(other);
653 } else {
654 self.remove(other);
655 }
656 }
657
658 pub fn truncate(&mut self)
660 ensures
661 final(self).bits() == (old(self).bits() & Self::all().bits()),
662 final(self).flags_spec() == Self::flags_from_bits(
663 old(self).bits() & Self::all().bits(),
664 ),
665 {
666 *self = Self::from_bits_truncate(self.bits());
667 }
668
669 pub fn clear(&mut self)
671 ensures
672 final(self).bits() == 0,
673 final(self).flags_spec() == Self::flags_from_bits(0),
674 {
675 *self = Self::empty();
676 }
677
678 pub closed spec fn union_spec(self, other: Self) -> Self {
679 Self::from_bits_unchecked_spec(self.bits() | other.bits())
680 }
681
682 #[verifier::when_used_as_spec(union_spec)]
683 pub const fn union(self, other: Self) -> (r: Self)
684 ensures
685 r.bits() == (self.bits() | other.bits()),
686 returns self.union(other),
687 {
688 Self::from_bits_unchecked(self.0.bits | other.0.bits)
689 }
690
691 pub closed spec fn intersection_spec(self, other: Self) -> Self {
692 Self::from_bits_unchecked_spec(self.bits() & other.bits())
693 }
694
695 #[verifier::when_used_as_spec(intersection_spec)]
696 pub const fn intersection(self, other: Self) -> (r: Self)
697 ensures
698 r.bits() == (self.bits() & other.bits()),
699 returns self.intersection(other),
700 {
701 Self::from_bits_unchecked(self.0.bits & other.0.bits)
702 }
703
704 pub closed spec fn difference_spec(self, other: Self) -> Self {
705 Self::from_bits_unchecked_spec(self.bits() & !other.bits())
706 }
707
708 #[verifier::when_used_as_spec(difference_spec)]
709 pub const fn difference(self, other: Self) -> (r: Self)
710 ensures
711 r.bits() == (self.bits() & !other.bits()),
712 returns self.difference(other),
713 {
714 Self::from_bits_unchecked(self.0.bits & !other.0.bits)
715 }
716
717 pub closed spec fn symmetric_difference_spec(self, other: Self) -> Self {
718 Self::from_bits_unchecked_spec(self.bits() ^ other.bits())
719 }
720
721 #[verifier::when_used_as_spec(symmetric_difference_spec)]
722 pub const fn symmetric_difference(self, other: Self) -> (r: Self)
723 ensures
724 r.bits() == (self.bits() ^ other.bits()),
725 returns self.symmetric_difference(other),
726 {
727 Self::from_bits_unchecked(self.0.bits ^ other.0.bits)
728 }
729
730 pub closed spec fn complement_spec(self) -> Self {
731 Self::from_bits_truncate_spec(!self.bits())
732 }
733
734 #[verifier::when_used_as_spec(complement_spec)]
735 pub const fn complement(self) -> (r: Self)
736 ensures
737 r.bits() == (!self.bits() & Self::all().bits()),
738 r.flags_spec() == Self::flags_from_bits(!self.bits() & Self::all().bits()),
739 returns self.complement(),
740 {
741 Self::from_bits_truncate(!self.0.bits)
742 }
743 }
744
745 impl ::vstd::std_specs::ops::BitOrSpecImpl for $name {
746 open spec fn obeys_bitor_spec() -> bool { true }
747
748 open spec fn bitor_req(self, rhs: Self) -> bool { true }
749
750 open spec fn bitor_spec(self, rhs: Self) -> Self::Output {
751 self.union(rhs)
752 }
753 }
754
755 impl core::ops::BitOr for $name {
756 type Output = Self;
757 fn bitor(self, other: Self) -> (r: Self)
758 ensures
759 r.bits() == (self.bits() | other.bits()),
760 {
761 self.union(other)
762 }
763 }
764
765 impl vstd::std_specs::ops::BitAndSpecImpl for $name {
766 open spec fn obeys_bitand_spec() -> bool { true }
767
768 open spec fn bitand_req(self, rhs: Self) -> bool { true }
769
770 open spec fn bitand_spec(self, rhs: Self) -> Self::Output {
771 self.intersection(rhs)
772 }
773 }
774
775 impl core::ops::BitAnd for $name {
776 type Output = Self;
777 fn bitand(self, other: Self) -> (r: Self)
778 ensures
779 r.bits() == (self.bits() & other.bits()),
780 {
781 self.intersection(other)
782 }
783 }
784
785 impl vstd::std_specs::ops::BitXorSpecImpl for $name {
786 open spec fn obeys_bitxor_spec() -> bool { true }
787
788 open spec fn bitxor_req(self, rhs: Self) -> bool { true }
789
790 open spec fn bitxor_spec(self, rhs: Self) -> Self::Output {
791 self.symmetric_difference(rhs)
792 }
793 }
794
795 impl core::ops::BitXor for $name {
796 type Output = Self;
797 fn bitxor(self, other: Self) -> (r: Self)
798 ensures
799 r.bits() == (self.bits() ^ other.bits()),
800 {
801 self.symmetric_difference(other)
802 }
803 }
804
805 impl vstd::std_specs::ops::SubSpecImpl for $name {
806 open spec fn obeys_sub_spec() -> bool { true }
807
808 open spec fn sub_req(self, rhs: Self) -> bool { true }
809
810 open spec fn sub_spec(self, rhs: Self) -> Self::Output {
811 self.difference(rhs)
812 }
813 }
814
815 impl core::ops::Sub for $name {
816 type Output = Self;
817 fn sub(self, other: Self) -> (r: Self)
818 ensures
819 r.bits() == (self.bits() & !other.bits()),
820 {
821 self.difference(other)
822 }
823 }
824
825 impl vstd::std_specs::ops::NotSpecImpl for $name {
826 open spec fn obeys_not_spec() -> bool { true }
827
828 open spec fn not_req(self) -> bool { true }
829
830 closed spec fn not_spec(self) -> Self::Output {
831 self.complement()
832 }
833 }
834
835 impl core::ops::Not for $name {
836 type Output = Self;
837
838 fn not(self) -> (r: Self)
839 ensures
840 r.bits() == (!self.bits() & $name::all().bits()),
841 {
842 self.complement()
843 }
844 }
845
846 } impl ::core::fmt::Debug for [< __Bitflags $name >] {
848 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
849 let source = self.bits;
850 if source == 0 {
851 return ::core::write!(f, "{:#x}", 0 as $T);
852 }
853 let mut remaining = source;
854 let mut first = true;
855 $(
856 $crate::__bitflags_cfg_guarded_stmt!(
857 ($(#[$inner $($args)*])*)
858 {
859 let name = $name::[< __ $Flag _NAME >];
860 let bits = $name::$Flag.bits();
861 if !name.is_empty() && source & bits == bits && remaining & bits != 0 {
862 if !first {
863 f.write_str(" | ")?;
864 }
865 first = false;
866 f.write_str(name)?;
867 remaining &= !bits;
868 }
869 }
870 );
871 )*
872 if remaining != 0 {
873 if !first {
874 f.write_str(" | ")?;
875 }
876 ::core::write!(f, "{:#x}", remaining)?;
877 }
878 Ok(())
879 }
880 }
881
882 impl ::core::ops::BitOrAssign for $name {
883 #[inline]
884 fn bitor_assign(&mut self, other: Self) {
885 self.insert(other);
886 }
887 }
888
889 impl ::core::ops::BitAndAssign for $name {
890 #[inline]
891 fn bitand_assign(&mut self, other: Self) {
892 *self = Self::from_bits_retain(self.bits()).intersection(other);
893 }
894 }
895
896 impl ::core::ops::BitXorAssign for $name {
897 #[inline]
898 fn bitxor_assign(&mut self, other: Self) {
899 self.toggle(other);
900 }
901 }
902
903 impl ::core::ops::SubAssign for $name {
904 #[inline]
905 fn sub_assign(&mut self, other: Self) {
906 self.remove(other);
907 }
908 }
909
910 } $crate::bitflags! { $($rest)* }
912 };
913 () => {};
914}
915
916bitflags! {
922 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
923 pub struct Flags: u32 {
924 const A = 0b00000001;
925 const B = 0b00000010;
926 const C = 0b00000100;
927 const ABC = 0b00000111;
928 }
929}
930
931verus! {
932
933#[allow(dead_code)]
934fn _bitflags_smoke_test() {
935 let a = Flags::A;
936 let cloned = a.clone();
937 assert(cloned == a);
938 let b = Flags::B;
939 let c = Flags::C;
940 let mut flags = Flags::ABC;
941
942 flags.remove(a);
943 flags.insert(a);
944 flags.toggle(b);
945 flags.set(c, false);
946 flags.set(b, true);
947 flags.insert(c);
948 flags = flags.intersection(Flags::from_bits_retain(0b101u32));
949 flags.toggle(a);
950
951 let unknown = Flags::from_bits_retain(0b1000u32);
952 unknown.known_bits();
953 unknown.unknown_bits();
954 unknown.contains_unknown_bits();
955 Flags::from_bits(0b1000u32);
956 let truncated_unknown = Flags::from_bits_truncate(0b1001u32);
957 truncated_unknown.bits();
958
959}
960
961} mod associated_constants {
964 use vstd::prelude::*;
965
966 use super::Flags;
967
968 verus! {
969
970exec const COMBINED: Flags
971 ensures
972 COMBINED.bits() == 0b11,
973{
974 proof {
975 assert(1u32 | 2u32 == 0b11) by (bit_vector);
976 }
977 Flags::A.union(Flags::B)
978}
979
980fn constant_client() {
981 proof {
982 assert(1u32 | 2u32 == 0b11) by (bit_vector);
983 assert(0b11u32 & 1u32 == 1u32) by (bit_vector);
984 }
985 let flags = Flags::A | Flags::B;
986 let combined = COMBINED;
987 assert(flags.bits() == 0b11);
988 assert(flags.contains(Flags::A));
989 assert(combined.bits() == flags.bits());
990}
991
992proof fn constant_spec_client()
993 ensures
994 Flags::A.bits() == 0b1,
995 Flags::ABC.bits() == 0b111,
996{
997 broadcast use Flags::lemma_consts;
998
999}
1000
1001} }
1003
1004mod inferred_initializers {
1005 use vstd::prelude::*;
1006
1007 bitflags! {
1008 pub struct Flags32: u32 {
1009 const HIGH = 1 << 31;
1010 const COMBINED = (1 << 0) | (1 << 1);
1011 const ALL = !0;
1012 const EXPLICIT = 1u32 << 5;
1013 #[cfg(any())]
1014 const DISABLED = 1 << 30;
1015 }
1016 }
1017
1018 bitflags! {
1019 pub struct Flags64: u64 {
1020 const HIGH = 1 << 63;
1021 const COMBINED = (1 << 40) | (1 << 41);
1022 }
1023 }
1024
1025 verus! {
1026
1027proof fn initializer_values() {
1028 assert(Flags32::HIGH.bits() == 0x8000_0000u32) by (compute_only);
1029 assert(Flags32::COMBINED.bits() == 3u32) by (compute_only);
1030 assert(Flags32::ALL.bits() == u32::MAX) by (compute_only);
1031 assert(Flags32::EXPLICIT.bits() == 32u32) by (compute_only);
1032 assert(Flags64::HIGH.bits() == 0x8000_0000_0000_0000u64) by (compute_only);
1033 assert(Flags64::COMBINED.bits() == 0x0000_0300_0000_0000u64) by (compute_only);
1034 Flags32::lemma_all_constant();
1035 assert(Flags32::all().bits() == u32::MAX) by (compute_only);
1036}
1037
1038} }