Skip to content

Add the identity function as core::convert::identity - #47562

Merged
bors merged 12 commits into
rust-lang:masterfrom
Centril:feature/core_convert_id
Aug 20, 2018
Merged

Add the identity function as core::convert::identity#47562
bors merged 12 commits into
rust-lang:masterfrom
Centril:feature/core_convert_id

Conversation

@Centril

@Centril Centril commented Jan 19, 2018

Copy link
Copy Markdown
Contributor

New notes

This implements rust-lang/rfcs#2306 (see #53500).

Old notes (ignore this in new reviews)

Adds the identity function fn id<T>(x: T) -> T { x } to core::convert and the prelude.
Some motivations for why this is useful are explained in the doc tests.
Another is that using the identity function instead of { x } or |x| x makes it clear that you intended to use an identity conversion on purpose.

The reasoning:

  • behind adding this to convert and not mem is that this is an identity conversion.
  • for adding this to the prelude is that it should be easy enough to use that the ease of writing your own identity function or using a closure |x| x doesn't overtake that.

I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately.

cc @bluss

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@kennytm kennytm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 19, 2018
@Manishearth

Copy link
Copy Markdown
Member

I'm wary of adding unstable stuff to the prelude, feels like it could still cause clashes with other glob imports.

(also, shouldn't this go through an rfc first?)

@Centril

Centril commented Jan 19, 2018

Copy link
Copy Markdown
Contributor Author

I'm wary of adding unstable stuff to the prelude, feels like it could still cause clashes with other glob imports.

Any example you have in mind? AFAIK any import or function definition that the user makes, glob or not will override anything in the prelude..

(also, shouldn't this go through an rfc first?)

I can do that if you want...

But the addition is rather minor - there's not much technical to discuss - only if we want this in libcore or not... And there's precedent for this in other languages like Haskell, Scala, Java, Idris

@Manishearth

Copy link
Copy Markdown
Member

@Centril the interaction between two glob imports is the thing here.

Adding stuff to the stdlib usually goes through an RFC, however trivial. This goes double for things in the prelude. It's sometimes fine to add stuff as a feature-gated API before making the RFC but you need some justification for that.

@Centril

Centril commented Jan 19, 2018

Copy link
Copy Markdown
Contributor Author

@Manishearth Alright; I'll write up an RFC. I'll leave this PR open meanwhile so that we can use it if the RFC gets accepted.

@Manishearth

Copy link
Copy Markdown
Member

Probably should close this and reopen if the RFC merges. Unless we intend to get it merged soon, it shouldn't stay open.

@Centril

Centril commented Jan 19, 2018

Copy link
Copy Markdown
Contributor Author

That's fair. Expect and RFC filed by the end of the day.

@bluss

bluss commented Jan 19, 2018

Copy link
Copy Markdown
Contributor

The prelude change seems good to have an rfc for? New minor features do not otherwise need an rfc, I think this change fits well inside what has been added as unstable without rfc recently.

@Centril

Centril commented Jan 19, 2018

Copy link
Copy Markdown
Contributor Author

@bluss Yeah. Tho I think that in this case the function is much less useful outside the prelude.

@Centril Centril changed the title Add the identity function as core::convert::id (+ to prelude) Add the identity function as core::convert::identity Aug 19, 2018
@Centril Centril reopened this Aug 19, 2018
@Centril Centril changed the title Add the identity function as core::convert::identity [WIP] Add the identity function as core::convert::identity Aug 19, 2018
@Centril Centril changed the title [WIP] Add the identity function as core::convert::identity Add the identity function as core::convert::identity Aug 19, 2018
@Centril

Centril commented Aug 19, 2018

Copy link
Copy Markdown
Contributor Author

Once the tests pass, this should be ready to go.

I've added tests for the rustc_const_unstable(..) bit as well.

Comment thread src/libcore/convert.rs Outdated
/// let filtered = iter.filter_map(identity).collect::<Vec<_>>();
/// assert_eq!(vec![1, 3], filtered);
/// ```
#[unstable(feature = "convert_id", issue = "0")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be issue = "53500" now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers; you always miss something :D

@oli-obk

oli-obk commented Aug 20, 2018

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Aug 20, 2018

Copy link
Copy Markdown
Collaborator

📌 Commit 86641d9 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 20, 2018
@bors

bors commented Aug 20, 2018

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 86641d9 with merge bf1e461...

bors added a commit that referenced this pull request Aug 20, 2018
Add the identity function as core::convert::identity

## New notes

This implements rust-lang/rfcs#2306 (see #53500).

## Old notes (ignore this in new reviews)

Adds the identity function `fn id<T>(x: T) -> T { x }` to core::convert and the prelude.
Some motivations for why this is useful are explained in the doc tests.
Another is that using the identity function instead of `{ x }` or `|x| x` makes it clear that you intended to use an identity conversion on purpose.

The reasoning:
+ behind adding this to `convert` and not `mem` is that this is an identity *conversion*.
+ for adding this to the prelude is that it should be easy enough to use that the ease of writing your own identity function or using a closure `|x| x` doesn't overtake that.

I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately.

cc @bluss
@bors

bors commented Aug 20, 2018

Copy link
Copy Markdown
Collaborator

☀️ Test successful - status-appveyor, status-travis
Approved by: oli-obk
Pushing bf1e461 to master...

@bors
bors merged commit 86641d9 into rust-lang:master Aug 20, 2018
@Centril
Centril deleted the feature/core_convert_id branch August 20, 2018 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

9 participants