{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/21318","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/21318/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/21318/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/21318/events","html_url":"https://github.com/rust-lang/rust/pull/21318","id":54675793,"number":21318,"title":"box: into_raw, from_raw functions","user":{"login":"stepancheg","id":28969,"avatar_url":"https://avatars.githubusercontent.com/u/28969?v=3","gravatar_id":"","url":"https://api.github.com/users/stepancheg","html_url":"https://github.com/stepancheg","followers_url":"https://api.github.com/users/stepancheg/followers","following_url":"https://api.github.com/users/stepancheg/following{/other_user}","gists_url":"https://api.github.com/users/stepancheg/gists{/gist_id}","starred_url":"https://api.github.com/users/stepancheg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stepancheg/subscriptions","organizations_url":"https://api.github.com/users/stepancheg/orgs","repos_url":"https://api.github.com/users/stepancheg/repos","events_url":"https://api.github.com/users/stepancheg/events{/privacy}","received_events_url":"https://api.github.com/users/stepancheg/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"brson","id":147214,"avatar_url":"https://avatars.githubusercontent.com/u/147214?v=3","gravatar_id":"","url":"https://api.github.com/users/brson","html_url":"https://github.com/brson","followers_url":"https://api.github.com/users/brson/followers","following_url":"https://api.github.com/users/brson/following{/other_user}","gists_url":"https://api.github.com/users/brson/gists{/gist_id}","starred_url":"https://api.github.com/users/brson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brson/subscriptions","organizations_url":"https://api.github.com/users/brson/orgs","repos_url":"https://api.github.com/users/brson/repos","events_url":"https://api.github.com/users/brson/events{/privacy}","received_events_url":"https://api.github.com/users/brson/received_events","type":"User","site_admin":false},"milestone":null,"comments":2,"created_at":"2015-01-17T21:22:24Z","updated_at":"2015-01-17T21:41:36Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/21318","html_url":"https://github.com/rust-lang/rust/pull/21318","diff_url":"https://github.com/rust-lang/rust/pull/21318.diff","patch_url":"https://github.com/rust-lang/rust/pull/21318.patch"},"body":"Functions are needed for safety and convenience.\r\n\r\nIt is a common pattern to use `mem::transmute` to convert between\r\n`Box` and raw pointer, like this:\r\n\r\n```\r\nlet b = Box::new(3);\r\nlet p = mem::transmute(b);\r\n// pass `p` to some C library\r\n```\r\n\r\nAfter this commit, conversion can be written as:\r\n\r\n```\r\nlet p = b.into_raw();\r\n```\r\n\r\n`into_raw` and `from_raw` functions are still unsafe, but they are\r\nmuch safer than `mem::transmute`, because *raw functions do not\r\nconvert between incompatible pointers. For example, this likely\r\nincorrect code can be successfully compiled:\r\n\r\n```\r\nlet p: *mut u64 = ...\r\nlet b: Box<u32> = mem::transmute(p);\r\n```\r\n\r\nUsing `from_raw` results in compile-time error:\r\n\r\n```\r\nlet p: *mut u64 = ...\r\nlet b: Box<u32> = Box::from_raw(p); // compile-time error\r\n```\r\n\r\n`into_raw` and `from_raw` functions are similar to C++ `std::unique_ptr`\r\n`release` function [1] and constructor from pointer [2].\r\n\r\n[1] http://en.cppreference.com/w/cpp/memory/unique_ptr/release\r\n[2] http://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr"},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/70385616","html_url":"https://github.com/rust-lang/rust/pull/21318#issuecomment-70385616","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/21318","id":70385616,"user":{"login":"brson","id":147214,"avatar_url":"https://avatars.githubusercontent.com/u/147214?v=3","gravatar_id":"","url":"https://api.github.com/users/brson","html_url":"https://github.com/brson","followers_url":"https://api.github.com/users/brson/followers","following_url":"https://api.github.com/users/brson/following{/other_user}","gists_url":"https://api.github.com/users/brson/gists{/gist_id}","starred_url":"https://api.github.com/users/brson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brson/subscriptions","organizations_url":"https://api.github.com/users/brson/orgs","repos_url":"https://api.github.com/users/brson/repos","events_url":"https://api.github.com/users/brson/events{/privacy}","received_events_url":"https://api.github.com/users/brson/received_events","type":"User","site_admin":false},"created_at":"2015-01-17T21:41:36Z","updated_at":"2015-01-17T21:41:36Z","body":"Agree this is a common pattern.\r\n\r\nr? @alexcrichton are there any alternate approaches to solving this problem?\r\n\r\nIf accepted this should have tests for boxed DSTs as well - I'm not actually sure whether they will work or not here."}}