Implement set complement and universe for bitflags#14009
Implement set complement and universe for bitflags#14009bors merged 1 commit intorust-lang:masterfrom jcmoyer:bitflags-complement
Conversation
|
The code compiles and all tests pass when the module is used standalone. |
|
Is there precedent for a This also has a travis failure which will need to get fixed, it looks like the |
|
Universe is a pretty standard term in set theory. I'm not sure if there's a different term used for this kind of thing, though. |
|
@alexcrichton: D'oh! I didn't realize the macro was already being used elsewhere in the stdlib. I'll fix this immediately. It was a late night. :) My decision to use the term |
|
One possible alternative to |
There was a problem hiding this comment.
A common use case would also contain masks. If so, would this be correct? We could add another section to the macro perhaps) eg. flags { ... } masks { ... }
There was a problem hiding this comment.
According to @alexcrichton, masks are usually subsets of the universal set, so this shouldn't be a problem. I think we should mention though in the macro's comment that the universe is not statically checked (some C bindings are probably weird like that), and in that case it is up to the user to define those outside the macro, eg. static NotInUniverse: Flags = Flags { bits: ??? }.
There was a problem hiding this comment.
Maybe we could auto-generate tests for the validity of the universe and complement functions?
There was a problem hiding this comment.
@bjz: Could you give me an example where universe or complement would return an invalid value? How would you test for validity?
|
@alexcrichton The universal set is a common term in set theory. As long as it is documented, I think it is fine. |
|
I think the name "all" would be better. It's certainly what I would expect, and reads more sensibly without any math background: " |
|
Yeah, |
|
This is great, and a clever implementation! I'd vote for the less fun, but more obvious |
|
Renamed |
|
Might need to rebase this. Then I can r+. |
|
@bjz: Done. |
|
Could you squash the two commits together, other than that, this looks good to go! |
|
@alexcrichton: sure thing! |
I feel that this is a very vital, missing piece of functionality. This adds on to #13072. Only bits used in the definition of the bitflag are considered for the universe set. This is a bit safer than simply inverting all of the bits in the wrapped value. ```rust bitflags!(flags Flags: u32 { FlagA = 0x00000001, FlagB = 0x00000010, FlagC = 0x00000100, FlagABC = FlagA.bits | FlagB.bits | FlagC.bits }) ... // `Not` implements set complement assert!(!(FlagB | FlagC) == FlagA); // `all` and `is_all` are the inverses of `empty` and `is_empty` assert!(Flags::all() - FlagA == !FlagA); assert!(FlagABC.is_all()); ```
I feel that this is a very vital, missing piece of functionality. This adds on to #13072.
Only bits used in the definition of the bitflag are considered for the universe set. This is a bit safer than simply inverting all of the bits in the wrapped value.