{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/166531523","pull_request_review_id":94606407,"id":166531523,"diff_hunk":"@@ -0,0 +1,203 @@\n+- Feature Name: convert_identity\n+- Start Date: 2018-01-19\n+- RFC PR: (leave this empty)\n+- Rust Issue: (leave this empty)\n+\n+# Summary\n+[summary]: #summary\n+\n+Adds an identity function `pub const fn identity<T>(x: T) -> T { x }` as\n+`core::convert::identity`. The function is also re-exported to\n+`std::convert::identity` as well as the prelude of\n+both libcore and libstd.\n+\n+# Motivation\n+[motivation]: #motivation\n+\n+## The identity function is useful\n+\n+While it might seem strange to have a function that just returns back the input,\n+there are some cases where the function is useful.\n+\n+### Using `identity` to do nothing among a collection of mappers\n+\n+When you have collections such as maps or arrays of mapping functions like\n+below and you watch to dispatch to those you sometimes need the identity\n+function as a way of not transforming the input. You can use the identity\n+function to achieve this.\n+\n+```rust\n+// Let's assume that this and other functions do something non-trivial.\n+fn do_interesting_stuff(x: u32) -> u32 { .. }\n+\n+// A dispatch-map of mapping functions:\n+let mut map = HashMap::new();\n+map.insert(\"foo\", do_interesting_stuff);\n+map.insert(\"bar\", other_stuff);\n+map.insert(\"baz\", identity);\n+```\n+\n+### Using `identity` as a no-op function in a conditional\n+\n+This reasoning also applies to simpler yes/no dispatch as below:\n+\n+```rust\n+let mapper = if condition { some_manipulation } else { identity };\n+\n+// do more interesting stuff inbetween..\n+\n+do_stuff(42);\n+```\n+\n+### Using `identity` to concatenate an iterator of iterators\n+\n+We can use the identity function to concatenate an iterator of iterators\n+into a single iterator.\n+\n+```rust\n+let vec_vec = vec![vec![1, 3, 4], vec![5, 6]];\n+let iter_iter = vec_vec.into_iter().map(Vec::into_iter);\n+let concatenated = iter_iter.flat_map(identity).collect::<Vec<_>>();\n+assert_eq!(vec![1, 3, 4, 5, 6], concatenated);\n+```\n+\n+### Using `identity` to keep the `Some` variants of an iterator of `Option<T>`\n+\n+We can keep all the maybe variants by simply `iter.filter_map(identity)`.\n+\n+```rust\n+let iter = vec![Some(1), None, Some(3)].into_iter();\n+let filtered = iter.filter_map(identity).collect::<Vec<_>>();\n+assert_eq!(vec![1, 3], filtered);\n+```\n+\n+### To be clear that you intended to use an identity conversion\n+\n+If you instead use a closure as in `|x| x` when you need an\n+identity conversion, it is less clear that this was intentional.\n+With `identity`, this intent becomes clearer.\n+\n+## The `drop` function as a precedent\n+\n+The `drop` function in `core::mem` is defined as `pub fn drop<T>(_x: T) { }`.\n+The same effect can be achieved by writing `{ _x; }`. This presents us\n+with a precendent that such trivial functions are considered useful and\n+includable inside the standard library even though they can be written easily\n+inside a user's crate.\n+\n+## Avoiding repetition in user crates\n+\n+Here are a few examples of the identity function being defined and used:\n+\n++ https://docs.rs/functils/0.0.2/functils/fn.identity.html\n++ https://docs.rs/tool/0.2.0/tool/fn.id.html\n++ https://github.com/hephex/api/blob/ef67b209cd88d0af40af10b4a9f3e0e61a5924da/src/lib.rs\n+\n+There's a smattering of more examples. To reduce duplication, it\n+should be provided in the standard library as a common place it is defined.","path":"text/0000-convert-id.md","position":97,"original_position":97,"commit_id":"1c8c20c0bc386fbcd42a19c7ae8141d50dcf1ed9","original_commit_id":"1c8c20c0bc386fbcd42a19c7ae8141d50dcf1ed9","user":{"login":"scottmcm","id":18526288,"avatar_url":"https://avatars2.githubusercontent.com/u/18526288?v=4","gravatar_id":"","url":"https://api.github.com/users/scottmcm","html_url":"https://github.com/scottmcm","followers_url":"https://api.github.com/users/scottmcm/followers","following_url":"https://api.github.com/users/scottmcm/following{/other_user}","gists_url":"https://api.github.com/users/scottmcm/gists{/gist_id}","starred_url":"https://api.github.com/users/scottmcm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottmcm/subscriptions","organizations_url":"https://api.github.com/users/scottmcm/orgs","repos_url":"https://api.github.com/users/scottmcm/repos","events_url":"https://api.github.com/users/scottmcm/events{/privacy}","received_events_url":"https://api.github.com/users/scottmcm/received_events","type":"User","site_admin":false},"body":"I don't think this argument is persuasive for functions.  There's a case for things like traits and structs having a common definition, since they're nominal and thus the commonality can improve interoperability (not that that place needs to be `std` even there).  But that's not the case for functions, where it doesn't matter if you used the `functils` or `tools` one.","created_at":"2018-02-07T06:56:22Z","updated_at":"2018-02-07T06:56:22Z","html_url":"https://github.com/rust-lang/rfcs/pull/2306#discussion_r166531523","pull_request_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/166531523"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/2306#discussion_r166531523"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306","id":164005929,"html_url":"https://github.com/rust-lang/rfcs/pull/2306","diff_url":"https://github.com/rust-lang/rfcs/pull/2306.diff","patch_url":"https://github.com/rust-lang/rfcs/pull/2306.patch","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/2306","number":2306,"state":"open","locked":false,"title":"RFC: Add `pub fn identity<T>(x: T) -> T { x }` to core::convert","user":{"login":"Centril","id":855702,"avatar_url":"https://avatars2.githubusercontent.com/u/855702?v=4","gravatar_id":"","url":"https://api.github.com/users/Centril","html_url":"https://github.com/Centril","followers_url":"https://api.github.com/users/Centril/followers","following_url":"https://api.github.com/users/Centril/following{/other_user}","gists_url":"https://api.github.com/users/Centril/gists{/gist_id}","starred_url":"https://api.github.com/users/Centril/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Centril/subscriptions","organizations_url":"https://api.github.com/users/Centril/orgs","repos_url":"https://api.github.com/users/Centril/repos","events_url":"https://api.github.com/users/Centril/events{/privacy}","received_events_url":"https://api.github.com/users/Centril/received_events","type":"User","site_admin":false},"body":"### [RENDERED](https://github.com/Centril/rfcs/blob/rfc/convert_id/text/0000-convert-id.md)\r\n\r\n-------------------\r\n\r\nAdds an identity function `pub fn identity<T>(x: T) -> T { x }` as `core::convert::identity`.\r\nThe function is also re-exported to `std::convert::identity` as well as the prelude of\r\nboth libcore and libstd.\r\n\r\n-------------------\r\n\r\nLinking https://github.com/rust-lang/rust/pull/47562\r\ncc @bluss @Manishearth ","created_at":"2018-01-19T16:03:36Z","updated_at":"2018-02-07T06:56:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"18db924b68eac1bd5ec01276c929e3b6cfb3e9fc","assignee":{"login":"aturon","id":709807,"avatar_url":"https://avatars2.githubusercontent.com/u/709807?v=4","gravatar_id":"","url":"https://api.github.com/users/aturon","html_url":"https://github.com/aturon","followers_url":"https://api.github.com/users/aturon/followers","following_url":"https://api.github.com/users/aturon/following{/other_user}","gists_url":"https://api.github.com/users/aturon/gists{/gist_id}","starred_url":"https://api.github.com/users/aturon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aturon/subscriptions","organizations_url":"https://api.github.com/users/aturon/orgs","repos_url":"https://api.github.com/users/aturon/repos","events_url":"https://api.github.com/users/aturon/events{/privacy}","received_events_url":"https://api.github.com/users/aturon/received_events","type":"User","site_admin":false},"assignees":[{"login":"aturon","id":709807,"avatar_url":"https://avatars2.githubusercontent.com/u/709807?v=4","gravatar_id":"","url":"https://api.github.com/users/aturon","html_url":"https://github.com/aturon","followers_url":"https://api.github.com/users/aturon/followers","following_url":"https://api.github.com/users/aturon/following{/other_user}","gists_url":"https://api.github.com/users/aturon/gists{/gist_id}","starred_url":"https://api.github.com/users/aturon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aturon/subscriptions","organizations_url":"https://api.github.com/users/aturon/orgs","repos_url":"https://api.github.com/users/aturon/repos","events_url":"https://api.github.com/users/aturon/events{/privacy}","received_events_url":"https://api.github.com/users/aturon/received_events","type":"User","site_admin":false}],"requested_reviewers":[],"requested_teams":[],"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/issues/2306/comments","statuses_url":"https://api.github.com/repos/rust-lang/rfcs/statuses/1c8c20c0bc386fbcd42a19c7ae8141d50dcf1ed9","head":{"label":"Centril:rfc/convert_id","ref":"rfc/convert_id","sha":"1c8c20c0bc386fbcd42a19c7ae8141d50dcf1ed9","user":{"login":"Centril","id":855702,"avatar_url":"https://avatars2.githubusercontent.com/u/855702?v=4","gravatar_id":"","url":"https://api.github.com/users/Centril","html_url":"https://github.com/Centril","followers_url":"https://api.github.com/users/Centril/followers","following_url":"https://api.github.com/users/Centril/following{/other_user}","gists_url":"https://api.github.com/users/Centril/gists{/gist_id}","starred_url":"https://api.github.com/users/Centril/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Centril/subscriptions","organizations_url":"https://api.github.com/users/Centril/orgs","repos_url":"https://api.github.com/users/Centril/repos","events_url":"https://api.github.com/users/Centril/events{/privacy}","received_events_url":"https://api.github.com/users/Centril/received_events","type":"User","site_admin":false},"repo":{"id":103166643,"name":"rfcs","full_name":"Centril/rfcs","owner":{"login":"Centril","id":855702,"avatar_url":"https://avatars2.githubusercontent.com/u/855702?v=4","gravatar_id":"","url":"https://api.github.com/users/Centril","html_url":"https://github.com/Centril","followers_url":"https://api.github.com/users/Centril/followers","following_url":"https://api.github.com/users/Centril/following{/other_user}","gists_url":"https://api.github.com/users/Centril/gists{/gist_id}","starred_url":"https://api.github.com/users/Centril/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Centril/subscriptions","organizations_url":"https://api.github.com/users/Centril/orgs","repos_url":"https://api.github.com/users/Centril/repos","events_url":"https://api.github.com/users/Centril/events{/privacy}","received_events_url":"https://api.github.com/users/Centril/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Centril/rfcs","description":"RFCs for changes to Rust","fork":true,"url":"https://api.github.com/repos/Centril/rfcs","forks_url":"https://api.github.com/repos/Centril/rfcs/forks","keys_url":"https://api.github.com/repos/Centril/rfcs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Centril/rfcs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Centril/rfcs/teams","hooks_url":"https://api.github.com/repos/Centril/rfcs/hooks","issue_events_url":"https://api.github.com/repos/Centril/rfcs/issues/events{/number}","events_url":"https://api.github.com/repos/Centril/rfcs/events","assignees_url":"https://api.github.com/repos/Centril/rfcs/assignees{/user}","branches_url":"https://api.github.com/repos/Centril/rfcs/branches{/branch}","tags_url":"https://api.github.com/repos/Centril/rfcs/tags","blobs_url":"https://api.github.com/repos/Centril/rfcs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Centril/rfcs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Centril/rfcs/git/refs{/sha}","trees_url":"https://api.github.com/repos/Centril/rfcs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Centril/rfcs/statuses/{sha}","languages_url":"https://api.github.com/repos/Centril/rfcs/languages","stargazers_url":"https://api.github.com/repos/Centril/rfcs/stargazers","contributors_url":"https://api.github.com/repos/Centril/rfcs/contributors","subscribers_url":"https://api.github.com/repos/Centril/rfcs/subscribers","subscription_url":"https://api.github.com/repos/Centril/rfcs/subscription","commits_url":"https://api.github.com/repos/Centril/rfcs/commits{/sha}","git_commits_url":"https://api.github.com/repos/Centril/rfcs/git/commits{/sha}","comments_url":"https://api.github.com/repos/Centril/rfcs/comments{/number}","issue_comment_url":"https://api.github.com/repos/Centril/rfcs/issues/comments{/number}","contents_url":"https://api.github.com/repos/Centril/rfcs/contents/{+path}","compare_url":"https://api.github.com/repos/Centril/rfcs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Centril/rfcs/merges","archive_url":"https://api.github.com/repos/Centril/rfcs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Centril/rfcs/downloads","issues_url":"https://api.github.com/repos/Centril/rfcs/issues{/number}","pulls_url":"https://api.github.com/repos/Centril/rfcs/pulls{/number}","milestones_url":"https://api.github.com/repos/Centril/rfcs/milestones{/number}","notifications_url":"https://api.github.com/repos/Centril/rfcs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Centril/rfcs/labels{/name}","releases_url":"https://api.github.com/repos/Centril/rfcs/releases{/id}","deployments_url":"https://api.github.com/repos/Centril/rfcs/deployments","created_at":"2017-09-11T17:28:49Z","updated_at":"2017-09-10T06:26:55Z","pushed_at":"2018-02-06T00:12:08Z","git_url":"git://github.com/Centril/rfcs.git","ssh_url":"git@github.com:Centril/rfcs.git","clone_url":"https://github.com/Centril/rfcs.git","svn_url":"https://github.com/Centril/rfcs","homepage":"","size":5119,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"743efe4ae2456e443763871cd57b6fe1ffa380b2","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars1.githubusercontent.com/u/5430905?v=4","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":17527989,"name":"rfcs","full_name":"rust-lang/rfcs","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars1.githubusercontent.com/u/5430905?v=4","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rfcs","description":"RFCs for changes to Rust","fork":false,"url":"https://api.github.com/repos/rust-lang/rfcs","forks_url":"https://api.github.com/repos/rust-lang/rfcs/forks","keys_url":"https://api.github.com/repos/rust-lang/rfcs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rfcs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rfcs/teams","hooks_url":"https://api.github.com/repos/rust-lang/rfcs/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rfcs/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rfcs/events","assignees_url":"https://api.github.com/repos/rust-lang/rfcs/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rfcs/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rfcs/tags","blobs_url":"https://api.github.com/repos/rust-lang/rfcs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rfcs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rfcs/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rfcs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rfcs/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rfcs/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rfcs/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rfcs/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rfcs/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rfcs/subscription","commits_url":"https://api.github.com/repos/rust-lang/rfcs/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rfcs/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rfcs/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rfcs/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rfcs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rfcs/merges","archive_url":"https://api.github.com/repos/rust-lang/rfcs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rfcs/downloads","issues_url":"https://api.github.com/repos/rust-lang/rfcs/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rfcs/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rfcs/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rfcs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rfcs/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rfcs/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/rfcs/deployments","created_at":"2014-03-07T21:29:00Z","updated_at":"2018-02-07T06:15:50Z","pushed_at":"2018-02-07T04:00:44Z","git_url":"git://github.com/rust-lang/rfcs.git","ssh_url":"git@github.com:rust-lang/rfcs.git","clone_url":"https://github.com/rust-lang/rfcs.git","svn_url":"https://github.com/rust-lang/rfcs","homepage":"http://rust-lang.github.io/rfcs/","size":12262,"stargazers_count":1313,"watchers_count":1313,"language":"Shell","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":557,"mirror_url":null,"archived":false,"open_issues_count":750,"license":null,"forks":557,"open_issues":750,"watchers":1313,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/2306"},"issue":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/2306"},"comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/2306/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/2306/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rfcs/statuses/1c8c20c0bc386fbcd42a19c7ae8141d50dcf1ed9"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"scottmcm"}}