{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/55934562","id":55934562,"diff_hunk":"@@ -0,0 +1,165 @@\n+- Feature Name: try_from\n+- Start Date: 2016-03-10\n+- RFC PR:\n+- Rust Issue:\n+\n+# Summary\n+[summary]: #summary\n+\n+The standard library provides the `From` and `Into` traits as standard ways to\n+convert between types. However, these traits only support *infallable*\n+conversions. This RFC proposes the addition of `TryFrom` and `TryInto` traits\n+to support these use cases in a standard way.\n+\n+# Motivation\n+[motivation]: #motivation\n+\n+Fallible conversions are fairly common, and a collection of ad-hoc traits has\n+arisen to support them, both [within the standard library][from-str] and [in\n+third party crates][into-connect-params]. A standardized set of traits\n+following the pattern set by `From` and `Into` will ease these APIs by\n+providing a standardized interface as we expand the set of fallible\n+conversions.\n+\n+One specific avenue of expansion that has been frequently requested is fallible\n+integer conversion traits. Conversions between integer types may currently be\n+performed with the `as` operator, which will silently truncate the value if it\n+is out of bounds of the target type. Code which needs to down-cast values must\n+manually check that the cast will succeed, which is both tedious and error\n+prone. A fallible conversion trait reduces code like this:\n+\n+```rust\n+let value: isize = ...;\n+\n+let value: u32 = if value < 0 || value > u32::max_value() as isize {\n+    return Err(BogusCast);\n+} else {\n+    value as u32\n+};\n+```\n+\n+to simply:\n+\n+```rust\n+let value: isize = ...;\n+let value: u32 = try!(value.try_into());","path":"text/0000-try-from.md","position":45,"original_position":45,"commit_id":"adc2547baa84fe5288dce7f94b22e02c98bfe346","original_commit_id":"adc2547baa84fe5288dce7f94b22e02c98bfe346","user":{"login":"petrochenkov","id":5751617,"avatar_url":"https://avatars.githubusercontent.com/u/5751617?v=3","gravatar_id":"","url":"https://api.github.com/users/petrochenkov","html_url":"https://github.com/petrochenkov","followers_url":"https://api.github.com/users/petrochenkov/followers","following_url":"https://api.github.com/users/petrochenkov/following{/other_user}","gists_url":"https://api.github.com/users/petrochenkov/gists{/gist_id}","starred_url":"https://api.github.com/users/petrochenkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petrochenkov/subscriptions","organizations_url":"https://api.github.com/users/petrochenkov/orgs","repos_url":"https://api.github.com/users/petrochenkov/repos","events_url":"https://api.github.com/users/petrochenkov/events{/privacy}","received_events_url":"https://api.github.com/users/petrochenkov/received_events","type":"User","site_admin":false},"body":"This example is pretty untypical.\r\nThese overflows, like arithmetic overflows, are very rarely handled, so most of the time people will have to deal with\r\n```\r\nlet value: u32 = value.try_into().unwrap();\r\n```","created_at":"2016-03-13T09:41:48Z","updated_at":"2016-03-13T09:41:48Z","html_url":"https://github.com/rust-lang/rfcs/pull/1542#discussion_r55934562","pull_request_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/55934562"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/1542#discussion_r55934562"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542","id":62653140,"html_url":"https://github.com/rust-lang/rfcs/pull/1542","diff_url":"https://github.com/rust-lang/rfcs/pull/1542.diff","patch_url":"https://github.com/rust-lang/rfcs/pull/1542.patch","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1542","number":1542,"state":"open","locked":false,"title":"Add TryFrom and TryInto traits","user":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"body":"[Rendered](https://github.com/sfackler/rfcs/blob/try-from/text/0000-try-from.md)","created_at":"2016-03-12T21:38:32Z","updated_at":"2016-03-13T09:41:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4fa0795d41f5cb1d291878baf4634712640cf546","assignee":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542/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/1542/comments","statuses_url":"https://api.github.com/repos/rust-lang/rfcs/statuses/adc2547baa84fe5288dce7f94b22e02c98bfe346","head":{"label":"sfackler:try-from","ref":"try-from","sha":"adc2547baa84fe5288dce7f94b22e02c98bfe346","user":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"repo":{"id":17653195,"name":"rfcs","full_name":"sfackler/rfcs","owner":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sfackler/rfcs","description":"RFCs for changes to Rust","fork":true,"url":"https://api.github.com/repos/sfackler/rfcs","forks_url":"https://api.github.com/repos/sfackler/rfcs/forks","keys_url":"https://api.github.com/repos/sfackler/rfcs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sfackler/rfcs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sfackler/rfcs/teams","hooks_url":"https://api.github.com/repos/sfackler/rfcs/hooks","issue_events_url":"https://api.github.com/repos/sfackler/rfcs/issues/events{/number}","events_url":"https://api.github.com/repos/sfackler/rfcs/events","assignees_url":"https://api.github.com/repos/sfackler/rfcs/assignees{/user}","branches_url":"https://api.github.com/repos/sfackler/rfcs/branches{/branch}","tags_url":"https://api.github.com/repos/sfackler/rfcs/tags","blobs_url":"https://api.github.com/repos/sfackler/rfcs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sfackler/rfcs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sfackler/rfcs/git/refs{/sha}","trees_url":"https://api.github.com/repos/sfackler/rfcs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sfackler/rfcs/statuses/{sha}","languages_url":"https://api.github.com/repos/sfackler/rfcs/languages","stargazers_url":"https://api.github.com/repos/sfackler/rfcs/stargazers","contributors_url":"https://api.github.com/repos/sfackler/rfcs/contributors","subscribers_url":"https://api.github.com/repos/sfackler/rfcs/subscribers","subscription_url":"https://api.github.com/repos/sfackler/rfcs/subscription","commits_url":"https://api.github.com/repos/sfackler/rfcs/commits{/sha}","git_commits_url":"https://api.github.com/repos/sfackler/rfcs/git/commits{/sha}","comments_url":"https://api.github.com/repos/sfackler/rfcs/comments{/number}","issue_comment_url":"https://api.github.com/repos/sfackler/rfcs/issues/comments{/number}","contents_url":"https://api.github.com/repos/sfackler/rfcs/contents/{+path}","compare_url":"https://api.github.com/repos/sfackler/rfcs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sfackler/rfcs/merges","archive_url":"https://api.github.com/repos/sfackler/rfcs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sfackler/rfcs/downloads","issues_url":"https://api.github.com/repos/sfackler/rfcs/issues{/number}","pulls_url":"https://api.github.com/repos/sfackler/rfcs/pulls{/number}","milestones_url":"https://api.github.com/repos/sfackler/rfcs/milestones{/number}","notifications_url":"https://api.github.com/repos/sfackler/rfcs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sfackler/rfcs/labels{/name}","releases_url":"https://api.github.com/repos/sfackler/rfcs/releases{/id}","deployments_url":"https://api.github.com/repos/sfackler/rfcs/deployments","created_at":"2014-03-12T02:50:13Z","updated_at":"2014-06-17T22:57:09Z","pushed_at":"2016-03-12T06:48:30Z","git_url":"git://github.com/sfackler/rfcs.git","ssh_url":"git@github.com:sfackler/rfcs.git","clone_url":"https://github.com/sfackler/rfcs.git","svn_url":"https://github.com/sfackler/rfcs","homepage":"","size":2637,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"80152a3c47cdd1f4507e4cfb0f318ca79626b2d5","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","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://avatars.githubusercontent.com/u/5430905?v=3","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":"2016-03-13T03:14:04Z","pushed_at":"2016-03-12T21:38:33Z","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":"","size":5089,"stargazers_count":562,"watchers_count":562,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":280,"mirror_url":null,"open_issues_count":644,"forks":280,"open_issues":644,"watchers":562,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/1542"},"issue":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/1542"},"comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/issues/1542/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/1542/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/1542/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rfcs/statuses/adc2547baa84fe5288dce7f94b22e02c98bfe346"}}}}