-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Implement owned ops for HashSet
and BTreeSet
#109402
base: master
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
@rust-lang/libs-api: This adds the following binary operator impls shown in bold below. There is a pre-existing more restrictive impl corresponding to each one; the additional bounds required by the pre-existing impls are noted below. All of the following require
All of the following require
|
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Add `minmax{,_by,_by_key}` functions to `core::cmp` This PR adds the following functions: ```rust // mod core::cmp #![unstable(feature = "cmp_minmax")] pub fn minmax<T>(v1: T, v2: T) -> [T; 2] where T: Ord; pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2] where F: FnOnce(&T, &T) -> Ordering; pub fn minmax_by_key<T, F, K>(v1: T, v2: T, mut f: F) -> [T; 2] where F: FnMut(&T) -> K, K: Ord; ``` (they are also `const` under `#[feature(const_cmp)]`, I've omitted `const` stuff for simplicity/readability) ---- Semantically these functions are equivalent to `{ let mut arr = [v1, v2]; arr.sort(); arr }`, but since they operate on 2 elements only, they are implemented as a single comparison. Even though that's basically a sort, I think "sort 2 elements" operation is useful on it's own in many cases. Namely, it's a common pattern when you have 2 things, and need to know which one is smaller/bigger to operate on them differently. I've wanted such functions countless times, most recently in rust-lang#109402, so I thought I'd propose them. ---- r? libs-api
Rollup merge of rust-lang#109409 - WaffleLapkin:progamer, r=dtolnay Add `minmax{,_by,_by_key}` functions to `core::cmp` This PR adds the following functions: ```rust // mod core::cmp #![unstable(feature = "cmp_minmax")] pub fn minmax<T>(v1: T, v2: T) -> [T; 2] where T: Ord; pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2] where F: FnOnce(&T, &T) -> Ordering; pub fn minmax_by_key<T, F, K>(v1: T, v2: T, mut f: F) -> [T; 2] where F: FnMut(&T) -> K, K: Ord; ``` (they are also `const` under `#[feature(const_cmp)]`, I've omitted `const` stuff for simplicity/readability) ---- Semantically these functions are equivalent to `{ let mut arr = [v1, v2]; arr.sort(); arr }`, but since they operate on 2 elements only, they are implemented as a single comparison. Even though that's basically a sort, I think "sort 2 elements" operation is useful on it's own in many cases. Namely, it's a common pattern when you have 2 things, and need to know which one is smaller/bigger to operate on them differently. I've wanted such functions countless times, most recently in rust-lang#109402, so I thought I'd propose them. ---- r? libs-api
It's going to be a small but weird asymmetry and an ergonomics papercut if some operators can take owned arguments and others cannot. Adding all the combinations would also fix the issue that code like |
This PR implements
ops::{BitAnd, BitOr, BitXor, Sub}
where one, or both arguments are owned (previously we only had impls for references to sets).The main advantage of these impls is lifted
T: Clone
bounds (and, in case ofHashSet
, liftedS: Default
). The secondary one is being able to reuse allocations. Lastly, they may (or may not) be more performant.Added public APIs (insta-stable):
I only added the "important" impls, i.e. such that allow lifting the bounds. Thus union-like ops (or, xor) have both arguments owned (because they need elements from both sets), while difference-like ops (sub, and) have only
self
owned (that's sufficient to not clone elements).Other potentially interesting implementations (in the order of most to least interesting according to myself):
BitAnd<Set<T, ...>> for Set<T, ...>
— optimization potential (can keep either allocation/iterate either set)BitOr<&Set<T, ...>> for Set<T, ...>
andBitOr<&Set<T, ...>> for Set<T, ...>
— still requireT: Clone
, but can keep the allocation/hasherBitOr<Set<T, ...>> for &Set<T, ...>
andBitOr<Set<T, ...>> for &Set<T, ...>
could be added for completenessBitAnd<Set<T, ...>> for &Set<T, ...>
— symmetry for completenessSub<Set<T, ...>> for Set<T, ...>
andSub<Set<T, ...>> for &Set<T, ...>
— completeness (no bounds lifting/optimization gains)r? libs-api