Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

WaffleLapkin
Copy link
Member

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 of HashSet, lifted S: Default). The secondary one is being able to reuse allocations. Lastly, they may (or may not) be more performant.

Added public APIs (insta-stable):

#![stable(feature = "set_owned_ops", since = "CURRENT_RUSTC_VERSION")]

impl<T: Eq + Hash, S: BuildHasher> BitOr<HashSet<T, S>> for HashSet<T, S>;
impl<T: Eq + Hash, S: BuildHasher> BitXor<HashSet<T, S>> for HashSet<T, S>;
impl<T: Eq + Hash, S: BuildHasher> BitAnd<&HashSet<T, S>> for HashSet<T, S>;
impl<T: Eq + Hash, S: BuildHasher> Sub<&HashSet<T, S>> for HashSet<T, S>;

impl<T: Ord, A: Allocator + Clone> BitOr<BTreeSet<T, A>> for BTreeSet<T, A>;
impl<T: Ord, A: Allocator + Clone> BitXor<BTreeSet<T, A>> for BTreeSet<T, A>;
impl<T: Ord, A: Allocator + Clone> BitAnd<&BTreeSet<T, A>> for BTreeSet<T, A>;
impl<T: Ord, A: Allocator + Clone> Sub<&BTreeSet<T, A>> for BTreeSet<T, A>;

// For all of the above, `type Output = Self;`

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):

  1. BitAnd<Set<T, ...>> for Set<T, ...> — optimization potential (can keep either allocation/iterate either set)
  2. BitOr<&Set<T, ...>> for Set<T, ...> and BitOr<&Set<T, ...>> for Set<T, ...> — still require T: Clone, but can keep the allocation/hasher
    • Symmetric BitOr<Set<T, ...>> for &Set<T, ...> and BitOr<Set<T, ...>> for &Set<T, ...> could be added for completeness
  3. BitAnd<Set<T, ...>> for &Set<T, ...> — symmetry for completeness
  4. Sub<Set<T, ...>> for Set<T, ...> and Sub<Set<T, ...>> for &Set<T, ...> — completeness (no bounds lifting/optimization gains)

r? libs-api

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 20, 2023
@rustbot

This comment was marked as resolved.

@WaffleLapkin WaffleLapkin added A-collections Area: std::collections. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 20, 2023
@workingjubilee workingjubilee added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Jul 31, 2023
@dtolnay dtolnay assigned dtolnay and unassigned joshtriplett Sep 17, 2023
@dtolnay
Copy link
Member

dtolnay commented Sep 17, 2023

@rust-lang/libs-api:
@rfcbot fcp merge

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 T: Eq + Hash, S: BuildHasher:

  • HashSet<T, S> | HashSet<T, S> (union)
    • existing impl: &HashSet<T, S> | &HashSet<T, S> requires T: Clone, S: Default
  • HashSet<T, S> ^ HashSet<T, S> (symmetric difference)
    • existing impl: &HashSet<T, S> ^ &HashSet<T, S> requires T: Clone, S: Default
  • HashSet<T, S> & &HashSet<T, S> (intersection)
    • existing impl: &HashSet<T, S> & &HashSet<T, S> requires T: Clone, S: Default
  • HashSet<T, S> - &HashSet<T, S> (difference)
    • existing impl: &HashSet<T, S> - &HashSet<T, S> requires T: Clone, S: Default

All of the following require T: Ord, A: Allocator + Clone:

  • BTreeSet<T, A> | BTreeSet<T, A> (union)
    • existing impl: &BTreeSet<T, A> | &BTreeSet<T, A> requires T: Clone
  • BTreeSet<T, A> ^ BTreeSet<T, A> (symmetric difference)
    • existing impl: &BTreeSet<T, A> ^ &BTreeSet<T, A> requires T: Clone
  • BTreeSet<T, A> & &BTreeSet<T, A> (intersection)
    • existing impl: &BTreeSet<T, A> & &BTreeSet<T, A> requires T: Clone
  • BTreeSet<T, A> - &BTreeSet<T, A> (difference)
    • existing impl: &BTreeSet<T, A> - &BTreeSet<T, A> requires T: Clone

@rfcbot
Copy link

rfcbot commented Sep 17, 2023

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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 17, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 18, 2023
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
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 18, 2023
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
@jdahlstrom
Copy link

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 a & &b or &a & &b looks quite confusing.

@dtolnay dtolnay added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 15, 2023
@WaffleLapkin WaffleLapkin removed the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: std::collections. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants