{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/comments/215105317","pull_request_review_id":152299325,"id":215105317,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxNTEwNTMxNw==","diff_hunk":"@@ -3560,6 +3591,29 @@ mod utils {\n         result.extend(old_items.into_iter());\n     }\n \n+    pub fn prepend_block_header(\n+        ctx: &BindgenContext,\n+        result: &mut Vec<quote::Tokens>,\n+    ) {\n+        let use_block = if ctx.options().block_extern_crate {\n+            quote! {\n+                #![allow(improper_ctypes)]\n+","path":"src/codegen/mod.rs","position":160,"original_position":160,"commit_id":"b82c69ac95724ceff6d54ba652463c863a166b73","original_commit_id":"b82c69ac95724ceff6d54ba652463c863a166b73","user":{"login":"emilio","id":1323194,"node_id":"MDQ6VXNlcjEzMjMxOTQ=","avatar_url":"https://avatars1.githubusercontent.com/u/1323194?v=4","gravatar_id":"","url":"https://api.github.com/users/emilio","html_url":"https://github.com/emilio","followers_url":"https://api.github.com/users/emilio/followers","following_url":"https://api.github.com/users/emilio/following{/other_user}","gists_url":"https://api.github.com/users/emilio/gists{/gist_id}","starred_url":"https://api.github.com/users/emilio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emilio/subscriptions","organizations_url":"https://api.github.com/users/emilio/orgs","repos_url":"https://api.github.com/users/emilio/repos","events_url":"https://api.github.com/users/emilio/events{/privacy}","received_events_url":"https://api.github.com/users/emilio/received_events","type":"User","site_admin":false},"body":"nit: stray newline.","created_at":"2018-09-05T00:28:40Z","updated_at":"2018-09-05T00:41:02Z","html_url":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378#discussion_r215105317","pull_request_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/comments/215105317"},"html":{"href":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378#discussion_r215105317"},"pull_request":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378","id":212447271,"node_id":"MDExOlB1bGxSZXF1ZXN0MjEyNDQ3Mjcx","html_url":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378","diff_url":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378.diff","patch_url":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378.patch","issue_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/1378","number":1378,"state":"open","locked":false,"title":"generate type alias for the `block` type","user":{"login":"flier","id":61618,"node_id":"MDQ6VXNlcjYxNjE4","avatar_url":"https://avatars0.githubusercontent.com/u/61618?v=4","gravatar_id":"","url":"https://api.github.com/users/flier","html_url":"https://github.com/flier","followers_url":"https://api.github.com/users/flier/followers","following_url":"https://api.github.com/users/flier/following{/other_user}","gists_url":"https://api.github.com/users/flier/gists{/gist_id}","starred_url":"https://api.github.com/users/flier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flier/subscriptions","organizations_url":"https://api.github.com/users/flier/orgs","repos_url":"https://api.github.com/users/flier/repos","events_url":"https://api.github.com/users/flier/events{/privacy}","received_events_url":"https://api.github.com/users/flier/received_events","type":"User","site_admin":false},"body":"Consider the c/c++ code with [the clang `Block` type](http://clang.llvm.org/docs/BlockLanguageSpec.html).\r\n\r\n```c++\r\n// bindgen-flags: --block-extern-crate -- -fblocks\r\n\r\ntypedef unsigned long long size_t;\r\n\r\nvoid atexit_b(void (^)(void));\r\n\r\ntypedef void *dispatch_data_t;\r\ntypedef bool (^dispatch_data_applier_t)(dispatch_data_t region,\r\n                                        size_t offset,\r\n                                        const void *buffer,\r\n                                        size_t size);\r\nbool dispatch_data_apply(dispatch_data_t data,\r\n                         dispatch_data_applier_t applier); \r\n```\r\nThe generated code will use the [block](https://crates.io/crates/block) crate to wrap function/closure as a `block` pointer, instead of a `*const c_void` pointer without arguments/return type.\r\n```rust\r\n/* automatically generated by rust-bindgen */\r\n\r\n#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]\r\n\r\n#[macro_use]\r\nextern crate block;\r\nextern \"C\" {\r\n    #[link_name = \"\\u{1}_Z8atexit_bU13block_pointerFvvE\"]\r\n    pub fn atexit_b(arg1: _bindgen_ty_id_19);\r\n}\r\npub type dispatch_data_t = *mut ::std::os::raw::c_void;\r\npub type dispatch_data_applier_t = _bindgen_ty_id_26;\r\nextern \"C\" {\r\n    #[link_name = \"\\u{1}_Z19dispatch_data_applyPvU13block_pointerFbS_yPKvyE\"]\r\n    pub fn dispatch_data_apply(data: dispatch_data_t, applier: dispatch_data_applier_t) -> bool;\r\n}\r\npub type _bindgen_ty_id_19 = *const ::block::Block<(), ()>;\r\npub type _bindgen_ty_id_26 =\r\n    *const ::block::Block<(dispatch_data_t, usize, *const ::std::os::raw::c_void, usize), bool>;\r\n```\r\nThen, we can use [a simple wrapper](https://github.com/SSheldon/rust-dispatch/blob/e979ba2e7b8d52f2487516931e73f5d9736f16c4/src/data.rs#L144) to call those functions.\r\n\r\n```rust\r\npub fn block<A, R, F>(closure: F) -> *const Block<A, R>\r\nwhere\r\n    A: BlockArguments,\r\n    F: 'static + IntoConcreteBlock<A, Ret = R>,\r\n{\r\n    let block = ConcreteBlock::new(closure);\r\n    let block = block.copy();\r\n    let ptr = (&*block) as *const _;\r\n    let ptr = unsafe { _Block_copy(ptr as *const c_void) };\r\n\r\n    ptr as *const Block<A, R>\r\n}\r\n\r\n#[derive(Debug)]\r\npub struct Data {\r\n    ptr: dispatch_data_t,\r\n}\r\n\r\nimpl Data\r\n    /// Traverses the memory of a dispatch data object and executes custom code on each region.\r\n    pub fn apply<F>(&self, applier: F) -> bool\r\n    where\r\n        F: 'static + Fn(&Self, usize, &[u8]) -> bool,\r\n    {\r\n        let data = self.clone();\r\n        let applier = block(\r\n            move |ptr: dispatch_data_t, offset: usize, buffer: *const c_void, size: usize| {\r\n                assert_eq!(data.ptr, ptr);\r\n\r\n                let buf = unsafe { slice::from_raw_parts(buffer as *const u8, size) };\r\n\r\n                applier(&data, offset, buf)\r\n            },\r\n        );\r\n\r\n        unsafe { dispatch_data_apply(self.ptr, applier) }\r\n    }\r\n}\r\n\r\n    #[test]\r\n    fn test_data() {\r\n         //...\r\n        assert!(data.apply(move |_data: &Data, offset: usize, buf: &[u8]| {\r\n            assert_eq!(offset, 0);\r\n            assert_eq!(buf, b\"lowor\");\r\n\r\n            c.set(buf.len());\r\n\r\n            true\r\n        }));\r\n    }\r\n```\r\n\r\n","created_at":"2018-08-31T17:25:08Z","updated_at":"2018-09-05T00:41:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f1e9396235af4924820b5197e3f3cb46fab7cbd6","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":401540323,"node_id":"MDU6TGFiZWw0MDE1NDAzMjM=","url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/labels/S-awaiting-review","name":"S-awaiting-review","color":"ededed","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378/commits","review_comments_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378/comments","review_comment_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/1378/comments","statuses_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/statuses/b82c69ac95724ceff6d54ba652463c863a166b73","head":{"label":"flier:blocks","ref":"blocks","sha":"b82c69ac95724ceff6d54ba652463c863a166b73","user":{"login":"flier","id":61618,"node_id":"MDQ6VXNlcjYxNjE4","avatar_url":"https://avatars0.githubusercontent.com/u/61618?v=4","gravatar_id":"","url":"https://api.github.com/users/flier","html_url":"https://github.com/flier","followers_url":"https://api.github.com/users/flier/followers","following_url":"https://api.github.com/users/flier/following{/other_user}","gists_url":"https://api.github.com/users/flier/gists{/gist_id}","starred_url":"https://api.github.com/users/flier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flier/subscriptions","organizations_url":"https://api.github.com/users/flier/orgs","repos_url":"https://api.github.com/users/flier/repos","events_url":"https://api.github.com/users/flier/events{/privacy}","received_events_url":"https://api.github.com/users/flier/received_events","type":"User","site_admin":false},"repo":{"id":80283106,"node_id":"MDEwOlJlcG9zaXRvcnk4MDI4MzEwNg==","name":"rust-bindgen","full_name":"flier/rust-bindgen","owner":{"login":"flier","id":61618,"node_id":"MDQ6VXNlcjYxNjE4","avatar_url":"https://avatars0.githubusercontent.com/u/61618?v=4","gravatar_id":"","url":"https://api.github.com/users/flier","html_url":"https://github.com/flier","followers_url":"https://api.github.com/users/flier/followers","following_url":"https://api.github.com/users/flier/following{/other_user}","gists_url":"https://api.github.com/users/flier/gists{/gist_id}","starred_url":"https://api.github.com/users/flier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flier/subscriptions","organizations_url":"https://api.github.com/users/flier/orgs","repos_url":"https://api.github.com/users/flier/repos","events_url":"https://api.github.com/users/flier/events{/privacy}","received_events_url":"https://api.github.com/users/flier/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/flier/rust-bindgen","description":"Automatically generates Rust FFI bindings to C and C++ libraries.","fork":true,"url":"https://api.github.com/repos/flier/rust-bindgen","forks_url":"https://api.github.com/repos/flier/rust-bindgen/forks","keys_url":"https://api.github.com/repos/flier/rust-bindgen/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flier/rust-bindgen/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flier/rust-bindgen/teams","hooks_url":"https://api.github.com/repos/flier/rust-bindgen/hooks","issue_events_url":"https://api.github.com/repos/flier/rust-bindgen/issues/events{/number}","events_url":"https://api.github.com/repos/flier/rust-bindgen/events","assignees_url":"https://api.github.com/repos/flier/rust-bindgen/assignees{/user}","branches_url":"https://api.github.com/repos/flier/rust-bindgen/branches{/branch}","tags_url":"https://api.github.com/repos/flier/rust-bindgen/tags","blobs_url":"https://api.github.com/repos/flier/rust-bindgen/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flier/rust-bindgen/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flier/rust-bindgen/git/refs{/sha}","trees_url":"https://api.github.com/repos/flier/rust-bindgen/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flier/rust-bindgen/statuses/{sha}","languages_url":"https://api.github.com/repos/flier/rust-bindgen/languages","stargazers_url":"https://api.github.com/repos/flier/rust-bindgen/stargazers","contributors_url":"https://api.github.com/repos/flier/rust-bindgen/contributors","subscribers_url":"https://api.github.com/repos/flier/rust-bindgen/subscribers","subscription_url":"https://api.github.com/repos/flier/rust-bindgen/subscription","commits_url":"https://api.github.com/repos/flier/rust-bindgen/commits{/sha}","git_commits_url":"https://api.github.com/repos/flier/rust-bindgen/git/commits{/sha}","comments_url":"https://api.github.com/repos/flier/rust-bindgen/comments{/number}","issue_comment_url":"https://api.github.com/repos/flier/rust-bindgen/issues/comments{/number}","contents_url":"https://api.github.com/repos/flier/rust-bindgen/contents/{+path}","compare_url":"https://api.github.com/repos/flier/rust-bindgen/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flier/rust-bindgen/merges","archive_url":"https://api.github.com/repos/flier/rust-bindgen/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flier/rust-bindgen/downloads","issues_url":"https://api.github.com/repos/flier/rust-bindgen/issues{/number}","pulls_url":"https://api.github.com/repos/flier/rust-bindgen/pulls{/number}","milestones_url":"https://api.github.com/repos/flier/rust-bindgen/milestones{/number}","notifications_url":"https://api.github.com/repos/flier/rust-bindgen/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flier/rust-bindgen/labels{/name}","releases_url":"https://api.github.com/repos/flier/rust-bindgen/releases{/id}","deployments_url":"https://api.github.com/repos/flier/rust-bindgen/deployments","created_at":"2017-01-28T12:26:17Z","updated_at":"2017-01-28T12:26:19Z","pushed_at":"2018-09-04T16:09:38Z","git_url":"git://github.com/flier/rust-bindgen.git","ssh_url":"git@github.com:flier/rust-bindgen.git","clone_url":"https://github.com/flier/rust-bindgen.git","svn_url":"https://github.com/flier/rust-bindgen","homepage":"","size":9799,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang-nursery:master","ref":"master","sha":"0db9588e84463c211340f0bc2cb6da2b3a99c441","user":{"login":"rust-lang-nursery","id":14631425,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE0NjMxNDI1","avatar_url":"https://avatars1.githubusercontent.com/u/14631425?v=4","gravatar_id":"","url":"https://api.github.com/users/rust-lang-nursery","html_url":"https://github.com/rust-lang-nursery","followers_url":"https://api.github.com/users/rust-lang-nursery/followers","following_url":"https://api.github.com/users/rust-lang-nursery/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang-nursery/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang-nursery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang-nursery/subscriptions","organizations_url":"https://api.github.com/users/rust-lang-nursery/orgs","repos_url":"https://api.github.com/users/rust-lang-nursery/repos","events_url":"https://api.github.com/users/rust-lang-nursery/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang-nursery/received_events","type":"Organization","site_admin":false},"repo":{"id":61728459,"node_id":"MDEwOlJlcG9zaXRvcnk2MTcyODQ1OQ==","name":"rust-bindgen","full_name":"rust-lang-nursery/rust-bindgen","owner":{"login":"rust-lang-nursery","id":14631425,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE0NjMxNDI1","avatar_url":"https://avatars1.githubusercontent.com/u/14631425?v=4","gravatar_id":"","url":"https://api.github.com/users/rust-lang-nursery","html_url":"https://github.com/rust-lang-nursery","followers_url":"https://api.github.com/users/rust-lang-nursery/followers","following_url":"https://api.github.com/users/rust-lang-nursery/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang-nursery/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang-nursery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang-nursery/subscriptions","organizations_url":"https://api.github.com/users/rust-lang-nursery/orgs","repos_url":"https://api.github.com/users/rust-lang-nursery/repos","events_url":"https://api.github.com/users/rust-lang-nursery/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang-nursery/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang-nursery/rust-bindgen","description":"Automatically generates Rust FFI bindings to C (and some C++) libraries.","fork":false,"url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen","forks_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/forks","keys_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/teams","hooks_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/hooks","issue_events_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/events","assignees_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/tags","blobs_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/languages","stargazers_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/stargazers","contributors_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/contributors","subscribers_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/subscribers","subscription_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/subscription","commits_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/merges","archive_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/downloads","issues_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/deployments","created_at":"2016-06-22T15:05:51Z","updated_at":"2018-09-05T00:36:21Z","pushed_at":"2018-09-04T16:09:40Z","git_url":"git://github.com/rust-lang-nursery/rust-bindgen.git","ssh_url":"git@github.com:rust-lang-nursery/rust-bindgen.git","clone_url":"https://github.com/rust-lang-nursery/rust-bindgen.git","svn_url":"https://github.com/rust-lang-nursery/rust-bindgen","homepage":"https://rust-lang-nursery.github.io/rust-bindgen/","size":10376,"stargazers_count":794,"watchers_count":794,"language":"C++","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":283,"mirror_url":null,"archived":false,"open_issues_count":211,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":283,"open_issues":211,"watchers":794,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378"},"html":{"href":"https://github.com/rust-lang-nursery/rust-bindgen/pull/1378"},"issue":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/1378"},"comments":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/issues/1378/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/pulls/1378/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang-nursery/rust-bindgen/statuses/b82c69ac95724ceff6d54ba652463c863a166b73"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"emilio"}}