Uh oh!
There was an error while loading. Please reload this page.
Implement set complement and universe for bitflags - #14009
Conversation
jcmoyer
commented
May 7, 2014
The code compiles and all tests pass when the module is used standalone. |
alexcrichton
commented
May 7, 2014
Is there precedent for a This also has a travis failure which will need to get fixed, it looks like the |
sfackler
commented
May 7, 2014
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. |
jcmoyer
commented
May 8, 2014
@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 |
sfackler
commented
May 8, 2014
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?
brendanzab
commented
May 8, 2014
@alexcrichton The universal set is a common term in set theory. As long as it is documented, I think it is fine. |
emberian
commented
May 8, 2014
I think the name "all" would be better. It's certainly what I would expect, and reads more sensibly without any math background: " |
brendanzab
commented
May 8, 2014
Yeah, |
aturon
commented
May 8, 2014
This is great, and a clever implementation! I'd vote for the less fun, but more obvious |
jcmoyer
commented
May 8, 2014
Renamed |
brendanzab
commented
May 13, 2014
Might need to rebase this. Then I can r+. |
jcmoyer
commented
May 14, 2014
@bjz: Done. |
alexcrichton
commented
May 14, 2014
Could you squash the two commits together, other than that, this looks good to go! |
jcmoyer
commented
May 14, 2014
@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.