Skip to content

Derive PartialOrd/Ord/Hash for bitflags structs. Implement Default for io::FilePermission #16074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2014

Conversation

nham
Copy link
Contributor

@nham nham commented Jul 29, 2014

I wanted to add an implementation of Default inside the bitflags macro, but Default isn't in the prelude, which means anyone who wants to use bitflags! needs to import it. This seems not nice, so I've just implemented for FilePermission instead.

@lilyball
Copy link
Contributor

Hash I get, but what meaning does ordering have for bitflags?

@nham
Copy link
Contributor Author

nham commented Jul 29, 2014

A collection of bitflags is just a string of bits, which can be lexicographically ordered. I'm not sure what meaning this has, but it's not very different from implementations of PartialOrd for other types (e.g. the PartialOrd for TreeSet is "list the elements in increasing order and then compare the lists lexicographically". I don't see the meaning of this)

@alexcrichton
Copy link
Member

This should be marked as a [breaking-change] because anyone implementing these traits manually for a bitflags type will have to remove the manual implementations.

@lilyball
Copy link
Contributor

@nham TreeSet is already semantically an ordered collection. While I agree that bitflags can be lexicographically ordered, I'm not convinced that there is in fact any meaning to the order of the bits and that, therefore, there's no actual meaning to ordering of bitflags.

The only argument I can come up with for deriving ordering for bitflags is so it can be used as a key in an ordered container. And maybe that's sufficient justification.

@nham
Copy link
Contributor Author

nham commented Jul 29, 2014

@alexcrichton I think I've done this now. I just needed to add [breaking-change] to the commit message, right?

@kballard I guess maybe I'm not sure what you mean by "meaning". Do you mean whether or not there's a use for this ordering (outside of enabling the type to be used in an ordered container)? I guess that's where my hangup with TreeSet is, because I cannot think of a single use for it. There's also a degree of arbitrariness to it: why not list the TreeSet elements in descending order instead of ascending order before you compare the lists? Is there a strong argument besides "the iter() method currently yields elements in ascending order"? I think the bitflags ordering being defined here is similarly arbitrary and based largely on implementation details.

@alexcrichton
Copy link
Member

You can find more information on the breaking changes policy here, the commit message should be expanded a bit according to the details of that email.

@lilyball
Copy link
Contributor

@nham If it were up to me, I wouldn't have defined PartialOrd on TreeSet. But as long as it is defined, it seems to be to be relatively unambiguously correct to define it as comparing the values in order. Especially because, as you pointed out, it has an iterator that yields elements in order. In general, it seems reasonable to define an ordering for any iterable collection that yields orderable elements.

nham added 2 commits July 30, 2014 16:04
In order to prevent users from having to manually implement Hash and Ord for
bitflags types, this commit derives these traits automatically.

This breaks code that has manually implemented any of these traits for types
created by the bitflags! macro. Change this code by removing implementations
of these traits.

[breaking-change]
@nham
Copy link
Contributor Author

nham commented Jul 30, 2014

@alexcrichton Thanks. I think I'm adhering to the breaking changes policy now, but please let me know if I'm missing anything.

bors added a commit that referenced this pull request Jul 31, 2014
I wanted to add an implementation of `Default` inside the bitflags macro, but `Default` isn't in the prelude, which means anyone who wants to use `bitflags!` needs to import it. This seems not nice, so I've just implemented for `FilePermission` instead.
@bors bors closed this Jul 31, 2014
@bors bors merged commit 96d6126 into rust-lang:master Jul 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants