├── .github └── workflows │ └── ci.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── foreign-types-macros ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT └── src │ ├── build.rs │ ├── lib.rs │ └── parse.rs ├── foreign-types-shared ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT └── src │ └── lib.rs └── foreign-types ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── src └── lib.rs └── tests └── test.rs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | push: 8 | branches: 9 | - master 10 | 11 | env: 12 | RUSTFLAGS: -Dwarnings 13 | RUST_BACKTRACE: 1 14 | 15 | jobs: 16 | rustfmt: 17 | name: rustfmt 18 | runs-on: ubuntu-latest 19 | steps: 20 | - uses: actions/checkout@v3 21 | - uses: sfackler/actions/rustup@master 22 | - uses: sfackler/actions/rustfmt@master 23 | 24 | clippy: 25 | name: clippy 26 | runs-on: ubuntu-latest 27 | steps: 28 | - uses: actions/checkout@v3 29 | - uses: sfackler/actions/rustup@master 30 | - run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT 31 | id: rust-version 32 | - uses: actions/cache@v3 33 | with: 34 | path: ~/.cargo/registry/index 35 | key: index-${{ runner.os }}-${{ github.run_number }} 36 | restore-keys: | 37 | index-${{ runner.os }}- 38 | - run: cargo generate-lockfile 39 | - uses: actions/cache@v3 40 | with: 41 | path: ~/.cargo/registry/cache 42 | key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }} 43 | - run: cargo fetch 44 | - uses: actions/cache@v3 45 | with: 46 | path: target 47 | key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y 48 | - run: cargo clippy --workspace --all-targets 49 | - run: cargo clippy --workspace --all-targets --no-default-features 50 | 51 | test: 52 | name: test 53 | runs-on: ubuntu-latest 54 | steps: 55 | - uses: actions/checkout@v3 56 | - uses: sfackler/actions/rustup@master 57 | with: 58 | version: 1.58.1 59 | - run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT 60 | id: rust-version 61 | - uses: actions/cache@v3 62 | with: 63 | path: ~/.cargo/registry/index 64 | key: index-${{ runner.os }}-${{ github.run_number }} 65 | restore-keys: | 66 | index-${{ runner.os }}- 67 | - run: cargo generate-lockfile 68 | - uses: actions/cache@v3 69 | with: 70 | path: ~/.cargo/registry/cache 71 | key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }} 72 | - run: cargo fetch 73 | - uses: actions/cache@v3 74 | with: 75 | path: target 76 | key: test-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y 77 | - run: cargo test --workspace 78 | - run: cargo test --workspace --no-default-features 79 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | .idea 4 | *.iml 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "foreign-types", 4 | "foreign-types-macros", 5 | "foreign-types-shared", 6 | ] 7 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 The foreign-types Developers 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # foreign-types 2 | 3 | [![Build Status](https://github.com/sfackler/foreign-types/workflows/CI/badge.svg)](https://github.com/sfackler/foreign-types/actions/workflows/ci.yml) 4 | 5 | [Documentation](https://docs.rs/foreign-types) 6 | 7 | A framework for Rust wrappers over C APIs. 8 | 9 | ## License 10 | 11 | Licensed under either of 12 | 13 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 14 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 15 | 16 | at your option. 17 | 18 | ### Contribution 19 | 20 | Unless you explicitly state otherwise, any contribution intentionally 21 | submitted for inclusion in the work by you, as defined in the Apache-2.0 22 | license, shall be dual licensed as above, without any additional terms or 23 | conditions. 24 | -------------------------------------------------------------------------------- /foreign-types-macros/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "foreign-types-macros" 3 | version = "0.2.3" 4 | authors = ["Steven Fackler "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | description = "An internal crate used by foreign-types" 8 | repository = "https://github.com/sfackler/foreign-types" 9 | 10 | [lib] 11 | proc-macro = true 12 | 13 | [features] 14 | std = [] 15 | 16 | [dependencies] 17 | syn = { version = "2.0", features = ["full"] } 18 | quote = "1.0" 19 | proc-macro2 = "1.0" 20 | -------------------------------------------------------------------------------- /foreign-types-macros/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /foreign-types-macros/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /foreign-types-macros/src/build.rs: -------------------------------------------------------------------------------- 1 | use proc_macro2::TokenStream; 2 | use quote::quote; 3 | use syn::{Ident, Path}; 4 | 5 | use crate::parse::{ForeignType, Input}; 6 | 7 | fn ref_name(input: &ForeignType) -> Ident { 8 | Ident::new(&format!("{}Ref", input.name), input.name.span()) 9 | } 10 | 11 | pub fn build(input: Input) -> TokenStream { 12 | let types = input 13 | .types 14 | .iter() 15 | .map(|t| build_foreign_type(&input.crate_, t)); 16 | quote! { 17 | #(#types)* 18 | } 19 | } 20 | 21 | fn build_foreign_type(crate_: &Path, input: &ForeignType) -> TokenStream { 22 | let decls = build_decls(crate_, input); 23 | let oibits = build_oibits(crate_, input); 24 | let foreign_impls = build_foreign_impls(crate_, input); 25 | let drop_impl = build_drop_impl(crate_, input); 26 | let deref_impls = build_deref_impls(crate_, input); 27 | let borrow_impls = build_borrow_impls(crate_, input); 28 | let as_ref_impls = build_as_ref_impls(crate_, input); 29 | let clone_impl = build_clone_impl(crate_, input); 30 | let to_owned_impl = build_to_owned_impl(crate_, input); 31 | 32 | quote! { 33 | #decls 34 | #oibits 35 | #foreign_impls 36 | #drop_impl 37 | #deref_impls 38 | #borrow_impls 39 | #as_ref_impls 40 | #clone_impl 41 | #to_owned_impl 42 | } 43 | } 44 | 45 | fn build_decls(crate_: &Path, input: &ForeignType) -> TokenStream { 46 | let attrs = &input.attrs; 47 | let vis = &input.visibility; 48 | let name = &input.name; 49 | let generics = &input.generics; 50 | let ctype = &input.ctype; 51 | let phantom_data = input 52 | .phantom_data 53 | .as_ref() 54 | .map(|d| quote!(, #crate_::export::PhantomData<#d>)); 55 | let ref_name = ref_name(input); 56 | let ref_docs = format!( 57 | "A borrowed reference to a [`{name}`](struct.{name}.html).", 58 | name = name 59 | ); 60 | 61 | quote! { 62 | #(#attrs)* 63 | #[repr(transparent)] 64 | #vis struct #name #generics(#crate_::export::NonNull<#ctype> #phantom_data); 65 | 66 | #[doc = #ref_docs] 67 | #vis struct #ref_name #generics(#crate_::Opaque #phantom_data); 68 | } 69 | } 70 | 71 | fn build_oibits(crate_: &Path, input: &ForeignType) -> TokenStream { 72 | let oibits = input.oibits.iter().map(|t| build_oibit(crate_, input, t)); 73 | 74 | quote! { 75 | #(#oibits)* 76 | } 77 | } 78 | 79 | fn build_oibit(crate_: &Path, input: &ForeignType, oibit: &Ident) -> TokenStream { 80 | let name = &input.name; 81 | let ref_name = ref_name(input); 82 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 83 | 84 | quote! { 85 | unsafe impl #impl_generics #crate_::export::#oibit for #name #ty_generics {} 86 | unsafe impl #impl_generics #crate_::export::#oibit for #ref_name #ty_generics {} 87 | } 88 | } 89 | 90 | fn build_foreign_impls(crate_: &Path, input: &ForeignType) -> TokenStream { 91 | let name = &input.name; 92 | let ctype = &input.ctype; 93 | let ref_name = ref_name(input); 94 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 95 | let phantom_data = input 96 | .phantom_data 97 | .as_ref() 98 | .map(|_| quote!(, #crate_::export::PhantomData)); 99 | 100 | quote! { 101 | unsafe impl #impl_generics #crate_::ForeignType for #name #ty_generics { 102 | type CType = #ctype; 103 | type Ref = #ref_name #ty_generics; 104 | 105 | #[inline] 106 | unsafe fn from_ptr(ptr: *mut #ctype) -> #name #ty_generics { 107 | debug_assert!(!ptr.is_null()); 108 | #name(<#crate_::export::NonNull<_>>::new_unchecked(ptr) #phantom_data) 109 | } 110 | 111 | #[inline] 112 | fn as_ptr(&self) -> *mut #ctype { 113 | <#crate_::export::NonNull<_>>::as_ptr(self.0) 114 | } 115 | } 116 | 117 | unsafe impl #impl_generics #crate_::ForeignTypeRef for #ref_name #ty_generics { 118 | type CType = #ctype; 119 | } 120 | } 121 | } 122 | 123 | fn build_drop_impl(crate_: &Path, input: &ForeignType) -> TokenStream { 124 | let name = &input.name; 125 | let drop = &input.drop; 126 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 127 | 128 | quote! { 129 | impl #impl_generics #crate_::export::Drop for #name #ty_generics { 130 | #[inline] 131 | fn drop(&mut self) { 132 | unsafe { 133 | (#drop)(#crate_::ForeignType::as_ptr(self)); 134 | } 135 | } 136 | } 137 | } 138 | } 139 | 140 | fn build_deref_impls(crate_: &Path, input: &ForeignType) -> TokenStream { 141 | let name = &input.name; 142 | let ref_name = ref_name(input); 143 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 144 | 145 | quote! { 146 | impl #impl_generics #crate_::export::Deref for #name #ty_generics { 147 | type Target = #ref_name #ty_generics; 148 | 149 | #[inline] 150 | fn deref(&self) -> &#ref_name #ty_generics { 151 | unsafe { 152 | #crate_::ForeignTypeRef::from_ptr(#crate_::ForeignType::as_ptr(self)) 153 | } 154 | } 155 | } 156 | 157 | impl #impl_generics #crate_::export::DerefMut for #name #ty_generics { 158 | #[inline] 159 | fn deref_mut(&mut self) -> &mut #ref_name #ty_generics { 160 | unsafe { 161 | #crate_::ForeignTypeRef::from_ptr_mut(#crate_::ForeignType::as_ptr(self)) 162 | } 163 | } 164 | } 165 | } 166 | } 167 | 168 | fn build_borrow_impls(crate_: &Path, input: &ForeignType) -> TokenStream { 169 | let name = &input.name; 170 | let ref_name = ref_name(input); 171 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 172 | 173 | quote! { 174 | impl #impl_generics #crate_::export::Borrow<#ref_name #ty_generics> for #name #ty_generics { 175 | #[inline] 176 | fn borrow(&self) -> &#ref_name #ty_generics { 177 | &**self 178 | } 179 | } 180 | 181 | impl #impl_generics #crate_::export::BorrowMut<#ref_name #ty_generics> for #name #ty_generics { 182 | #[inline] 183 | fn borrow_mut(&mut self) -> &mut #ref_name #ty_generics { 184 | &mut **self 185 | } 186 | } 187 | } 188 | } 189 | 190 | fn build_as_ref_impls(crate_: &Path, input: &ForeignType) -> TokenStream { 191 | let name = &input.name; 192 | let ref_name = ref_name(input); 193 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 194 | 195 | quote! { 196 | impl #impl_generics #crate_::export::AsRef<#ref_name #ty_generics> for #name #ty_generics { 197 | #[inline] 198 | fn as_ref(&self) -> &#ref_name #ty_generics { 199 | &**self 200 | } 201 | } 202 | 203 | impl #impl_generics #crate_::export::AsMut<#ref_name #ty_generics> for #name #ty_generics { 204 | #[inline] 205 | fn as_mut(&mut self) -> &mut #ref_name #ty_generics { 206 | &mut **self 207 | } 208 | } 209 | } 210 | } 211 | 212 | fn build_clone_impl(crate_: &Path, input: &ForeignType) -> TokenStream { 213 | let clone = match &input.clone { 214 | Some(clone) => clone, 215 | None => return quote!(), 216 | }; 217 | let name = &input.name; 218 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 219 | 220 | quote! { 221 | impl #impl_generics #crate_::export::Clone for #name #ty_generics { 222 | #[inline] 223 | fn clone(&self) -> #name #ty_generics { 224 | unsafe { 225 | let ptr = (#clone)(#crate_::ForeignType::as_ptr(self)); 226 | #crate_::ForeignType::from_ptr(ptr) 227 | } 228 | } 229 | } 230 | } 231 | } 232 | 233 | #[cfg(feature = "std")] 234 | fn build_to_owned_impl(crate_: &Path, input: &ForeignType) -> TokenStream { 235 | let clone = match &input.clone { 236 | Some(clone) => clone, 237 | None => return quote!(), 238 | }; 239 | let name = &input.name; 240 | let ref_name = ref_name(input); 241 | let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); 242 | 243 | quote! { 244 | impl #impl_generics #crate_::export::ToOwned for #ref_name #ty_generics { 245 | type Owned = #name #ty_generics; 246 | 247 | #[inline] 248 | fn to_owned(&self) -> #name #ty_generics { 249 | unsafe { 250 | let ptr = (#clone)(#crate_::ForeignTypeRef::as_ptr(self)); 251 | #crate_::ForeignType::from_ptr(ptr) 252 | } 253 | } 254 | } 255 | } 256 | } 257 | 258 | #[cfg(not(feature = "std"))] 259 | fn build_to_owned_impl(_: &Path, _: &ForeignType) -> TokenStream { 260 | quote!() 261 | } 262 | -------------------------------------------------------------------------------- /foreign-types-macros/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate proc_macro; 2 | 3 | use proc_macro::TokenStream; 4 | use syn::parse_macro_input; 5 | 6 | use crate::parse::Input; 7 | 8 | mod build; 9 | mod parse; 10 | 11 | #[proc_macro] 12 | pub fn foreign_type_impl(input: TokenStream) -> TokenStream { 13 | let input = parse_macro_input!(input as Input); 14 | build::build(input).into() 15 | } 16 | -------------------------------------------------------------------------------- /foreign-types-macros/src/parse.rs: -------------------------------------------------------------------------------- 1 | use syn::parse::{self, Parse, ParseStream}; 2 | use syn::punctuated::Punctuated; 3 | use syn::token; 4 | use syn::{braced, Attribute, Expr, Generics, Ident, Path, Token, Type, Visibility}; 5 | 6 | mod kw { 7 | syn::custom_keyword!(Sync); 8 | syn::custom_keyword!(Send); 9 | syn::custom_keyword!(PhantomData); 10 | syn::custom_keyword!(CType); 11 | syn::custom_keyword!(drop); 12 | syn::custom_keyword!(clone); 13 | } 14 | 15 | pub struct Input { 16 | pub crate_: Path, 17 | pub types: Vec, 18 | } 19 | 20 | impl Parse for Input { 21 | fn parse(input: ParseStream) -> parse::Result { 22 | let crate_ = input.parse()?; 23 | let mut types = vec![]; 24 | while !input.is_empty() { 25 | types.push(input.parse()?); 26 | } 27 | 28 | Ok(Input { crate_, types }) 29 | } 30 | } 31 | 32 | pub struct ForeignType { 33 | pub attrs: Vec, 34 | pub visibility: Visibility, 35 | pub name: Ident, 36 | pub generics: Generics, 37 | pub oibits: Punctuated, 38 | pub phantom_data: Option, 39 | pub ctype: Type, 40 | pub drop: Expr, 41 | pub clone: Option, 42 | } 43 | 44 | impl Parse for ForeignType { 45 | fn parse(input: ParseStream) -> parse::Result { 46 | let attrs = input.call(Attribute::parse_outer)?; 47 | let visibility = input.parse()?; 48 | input.parse::()?; 49 | input.parse::()?; 50 | let name = input.parse()?; 51 | let generics = input.parse()?; 52 | let oibits = input.call(parse_oibits)?; 53 | let inner; 54 | braced!(inner in input); 55 | let ctype = inner.call(parse_type::)?; 56 | let phantom_data = inner.call(parse_phantom_data)?; 57 | let drop = inner.call(parse_fn::)?; 58 | let clone = inner.call(parse_clone)?; 59 | 60 | Ok(ForeignType { 61 | attrs, 62 | visibility, 63 | name, 64 | generics, 65 | oibits, 66 | ctype, 67 | phantom_data, 68 | drop, 69 | clone, 70 | }) 71 | } 72 | } 73 | 74 | fn parse_oibit(input: ParseStream) -> parse::Result { 75 | let lookahead = input.lookahead1(); 76 | if lookahead.peek(kw::Sync) || lookahead.peek(kw::Send) { 77 | input.parse() 78 | } else { 79 | Err(lookahead.error()) 80 | } 81 | } 82 | 83 | fn parse_oibits(input: ParseStream) -> parse::Result> { 84 | let mut out = Punctuated::new(); 85 | 86 | if input.parse::>()?.is_some() { 87 | loop { 88 | out.push_value(input.call(parse_oibit)?); 89 | if input.peek(token::Brace) { 90 | break; 91 | } 92 | out.push_punct(input.parse()?); 93 | if input.peek(token::Brace) { 94 | break; 95 | } 96 | } 97 | } 98 | 99 | Ok(out) 100 | } 101 | 102 | fn parse_type(input: ParseStream) -> parse::Result 103 | where 104 | T: Parse, 105 | { 106 | input.parse::()?; 107 | input.parse::()?; 108 | input.parse::()?; 109 | let type_ = input.parse()?; 110 | input.parse::()?; 111 | Ok(type_) 112 | } 113 | 114 | fn parse_phantom_data(input: ParseStream) -> parse::Result> { 115 | if input.peek(Token![type]) && input.peek2(kw::PhantomData) { 116 | input.call(parse_type::).map(Some) 117 | } else { 118 | Ok(None) 119 | } 120 | } 121 | 122 | fn parse_fn(input: ParseStream) -> parse::Result 123 | where 124 | T: Parse, 125 | { 126 | input.parse::()?; 127 | input.parse::()?; 128 | input.parse::()?; 129 | let path = input.parse()?; 130 | input.parse::()?; 131 | Ok(path) 132 | } 133 | 134 | fn parse_clone(input: ParseStream) -> parse::Result> { 135 | if input.peek(Token![fn]) && input.peek2(kw::clone) { 136 | input.call(parse_fn::).map(Some) 137 | } else { 138 | Ok(None) 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /foreign-types-shared/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "foreign-types-shared" 3 | version = "0.3.1" 4 | authors = ["Steven Fackler "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | description = "An internal crate used by foreign-types" 8 | repository = "https://github.com/sfackler/foreign-types" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /foreign-types-shared/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /foreign-types-shared/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /foreign-types-shared/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Internal crate used by foreign-types 2 | 3 | #![no_std] 4 | #![warn(missing_docs)] 5 | #![doc(html_root_url = "https://docs.rs/foreign-types-shared/0.3")] 6 | 7 | use core::cell::UnsafeCell; 8 | use core::marker::PhantomData; 9 | use core::mem; 10 | 11 | /// An opaque type used to define `ForeignTypeRef` types. 12 | /// 13 | /// A type implementing `ForeignTypeRef` should simply be a newtype wrapper around this type. 14 | pub struct Opaque(PhantomData>); 15 | 16 | /// A type implemented by wrappers over foreign types. 17 | /// 18 | /// # Safety 19 | /// 20 | /// Implementations of `ForeignType` must guarantee the following: 21 | /// - `Self::from_ptr(x).as_ptr() == x` 22 | /// - `Self::from_ptr(x).into_ptr(x) == x` 23 | /// - `Self::from_ptr(x).deref().as_ptr(x) == x` 24 | /// - `Self::from_ptr(x).deref_mut().as_ptr(x) == x` 25 | /// - `Self::from_ptr(x).as_ref().as_ptr(x) == x` 26 | /// - `Self::from_ptr(x).as_mut().as_ptr(x) == x` 27 | pub unsafe trait ForeignType: Sized { 28 | /// The raw C type. 29 | type CType; 30 | 31 | /// The type representing a reference to this type. 32 | type Ref: ForeignTypeRef; 33 | 34 | /// Constructs an instance of this type from its raw type. 35 | /// 36 | /// # Safety 37 | /// 38 | /// `ptr` must be a valid, owned instance of the native type. 39 | unsafe fn from_ptr(ptr: *mut Self::CType) -> Self; 40 | 41 | /// Returns a raw pointer to the wrapped value. 42 | fn as_ptr(&self) -> *mut Self::CType; 43 | 44 | /// Consumes the wrapper and returns the raw pointer. 45 | #[inline] 46 | fn into_ptr(self) -> *mut Self::CType { 47 | let ptr = self.as_ptr(); 48 | mem::forget(self); 49 | ptr 50 | } 51 | } 52 | 53 | /// A trait implemented by types which reference borrowed foreign types. 54 | /// 55 | /// # Safety 56 | /// 57 | /// Implementations of `ForeignTypeRef` must guarantee the following: 58 | /// 59 | /// - `Self::from_ptr(x).as_ptr() == x` 60 | /// - `Self::from_mut_ptr(x).as_ptr() == x` 61 | pub unsafe trait ForeignTypeRef: Sized { 62 | /// The raw C type. 63 | type CType; 64 | 65 | /// Constructs a shared instance of this type from its raw type. 66 | /// 67 | /// # Safety 68 | /// 69 | /// `ptr` must be a valid, immutable, instance of the type for the `'a` lifetime. 70 | #[inline] 71 | unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self { 72 | debug_assert!(!ptr.is_null()); 73 | &*(ptr as *mut _) 74 | } 75 | 76 | /// Constructs a mutable reference of this type from its raw type. 77 | /// 78 | /// # Safety 79 | /// 80 | /// `ptr` must be a valid, unique, instance of the type for the `'a` lifetime. 81 | #[inline] 82 | unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self { 83 | debug_assert!(!ptr.is_null()); 84 | &mut *(ptr as *mut _) 85 | } 86 | 87 | /// Returns a raw pointer to the wrapped value. 88 | #[inline] 89 | fn as_ptr(&self) -> *mut Self::CType { 90 | self as *const _ as *mut _ 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /foreign-types/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "foreign-types" 3 | version = "0.5.0" 4 | authors = ["Steven Fackler "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | description = "A framework for Rust wrappers over C APIs" 8 | repository = "https://github.com/sfackler/foreign-types" 9 | readme = "README.md" 10 | 11 | [features] 12 | default = ["std"] 13 | std = ["foreign-types-macros/std"] 14 | 15 | [dependencies] 16 | foreign-types-macros = { version = "0.2", path = "../foreign-types-macros" } 17 | foreign-types-shared = { version = "0.3", path = "../foreign-types-shared" } 18 | -------------------------------------------------------------------------------- /foreign-types/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /foreign-types/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /foreign-types/README.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /foreign-types/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A framework for Rust wrappers over C APIs. 2 | //! 3 | //! Ownership is as important in C as it is in Rust, but the semantics are often implicit. In 4 | //! particular, pointer-to-value is commonly used to pass C values both when transferring ownership 5 | //! or a borrow. 6 | //! 7 | //! This crate provides a framework to define a Rust wrapper over these kinds of raw C APIs in a way 8 | //! that allows ownership semantics to be expressed in an ergonomic manner. The framework takes a 9 | //! dual-type approach similar to APIs in the standard library such as `PathBuf`/`Path` or `String`/ 10 | //! `str`. One type represents an owned value and references to the other represent borrowed 11 | //! values. 12 | //! 13 | //! # Examples 14 | //! 15 | //! ``` 16 | //! use foreign_types::{ForeignType, ForeignTypeRef, Opaque}; 17 | //! use std::ops::{Deref, DerefMut}; 18 | //! use std::ptr::NonNull; 19 | //! 20 | //! mod foo_sys { 21 | //! pub enum FOO {} 22 | //! 23 | //! extern { 24 | //! pub fn FOO_free(foo: *mut FOO); 25 | //! } 26 | //! } 27 | //! 28 | //! // The borrowed type is a newtype wrapper around an `Opaque` value. 29 | //! // 30 | //! // `FooRef` values never exist; we instead create references to `FooRef`s 31 | //! // from raw C pointers. 32 | //! pub struct FooRef(Opaque); 33 | //! 34 | //! unsafe impl ForeignTypeRef for FooRef { 35 | //! type CType = foo_sys::FOO; 36 | //! } 37 | //! 38 | //! // The owned type is simply a newtype wrapper around the raw C type. 39 | //! // 40 | //! // It dereferences to `FooRef`, so methods that do not require ownership 41 | //! // should be defined there. 42 | //! pub struct Foo(NonNull); 43 | //! 44 | //! unsafe impl Sync for FooRef {} 45 | //! unsafe impl Send for FooRef {} 46 | //! 47 | //! unsafe impl Sync for Foo {} 48 | //! unsafe impl Send for Foo {} 49 | //! 50 | //! impl Drop for Foo { 51 | //! fn drop(&mut self) { 52 | //! unsafe { foo_sys::FOO_free(self.as_ptr()) } 53 | //! } 54 | //! } 55 | //! 56 | //! unsafe impl ForeignType for Foo { 57 | //! type CType = foo_sys::FOO; 58 | //! type Ref = FooRef; 59 | //! 60 | //! unsafe fn from_ptr(ptr: *mut foo_sys::FOO) -> Foo { 61 | //! Foo(NonNull::new_unchecked(ptr)) 62 | //! } 63 | //! 64 | //! fn as_ptr(&self) -> *mut foo_sys::FOO { 65 | //! self.0.as_ptr() 66 | //! } 67 | //! 68 | //! fn into_ptr(self) -> *mut foo_sys::FOO { 69 | //! let inner = self.as_ptr(); 70 | //! ::core::mem::forget(self); 71 | //! inner 72 | //! } 73 | //! } 74 | //! 75 | //! impl Deref for Foo { 76 | //! type Target = FooRef; 77 | //! 78 | //! fn deref(&self) -> &FooRef { 79 | //! unsafe { FooRef::from_ptr(self.as_ptr()) } 80 | //! } 81 | //! } 82 | //! 83 | //! impl DerefMut for Foo { 84 | //! fn deref_mut(&mut self) -> &mut FooRef { 85 | //! unsafe { FooRef::from_ptr_mut(self.as_ptr()) } 86 | //! } 87 | //! } 88 | //! 89 | //! // add in Borrow, BorrowMut, AsRef, AsRefMut, Clone, ToOwned... 90 | //! ``` 91 | //! 92 | //! The `foreign_type!` macro can generate this boilerplate for you: 93 | //! 94 | //! ``` 95 | //! use foreign_types::foreign_type; 96 | //! 97 | //! mod foo_sys { 98 | //! pub enum FOO {} 99 | //! 100 | //! extern { 101 | //! pub fn FOO_free(foo: *mut FOO); 102 | //! pub fn FOO_duplicate(foo: *mut FOO) -> *mut FOO; // optional 103 | //! } 104 | //! } 105 | //! 106 | //! foreign_type! { 107 | //! /// A Foo. 108 | //! pub unsafe type Foo 109 | //! : Sync + Send // optional 110 | //! { 111 | //! type CType = foo_sys::FOO; 112 | //! fn drop = foo_sys::FOO_free; 113 | //! fn clone = foo_sys::FOO_duplicate; // optional 114 | //! } 115 | //! 116 | //! /// A Foo with generic parameters. 117 | //! pub unsafe type GenericFoo { 118 | //! type CType = foo_sys::FOO; 119 | //! // This type is added as a `PhantomData` field to handle variance 120 | //! // of the parameters. However, it has no impact on trait impls: 121 | //! // `GenericFoo` is always `Clone`, even if `T` is not. 122 | //! type PhantomData = T; 123 | //! fn drop = foo_sys::FOO_free; 124 | //! fn clone = foo_sys::FOO_duplicate; 125 | //! } 126 | //! } 127 | //! 128 | //! # fn main() {} 129 | //! ``` 130 | //! 131 | //! If `fn clone` is specified, then it must take `CType` as an argument and return a copy of it as `CType`. 132 | //! It will be used to implement `Clone`, and if the `std` Cargo feature is enabled, `ToOwned`. 133 | //! 134 | //! Say we then have a separate type in our C API that contains a `FOO`: 135 | //! 136 | //! ``` 137 | //! mod foo_sys { 138 | //! pub enum FOO {} 139 | //! pub enum BAR {} 140 | //! 141 | //! extern { 142 | //! pub fn FOO_free(foo: *mut FOO); 143 | //! pub fn BAR_free(bar: *mut BAR); 144 | //! pub fn BAR_get_foo(bar: *mut BAR) -> *mut FOO; 145 | //! } 146 | //! } 147 | //! ``` 148 | //! 149 | //! The documentation for the C library states that `BAR_get_foo` returns a reference into the `BAR` 150 | //! passed to it, which translates into a reference in Rust. It also says that we're allowed to 151 | //! modify the `FOO`, so we'll define a pair of accessor methods, one immutable and one mutable: 152 | //! 153 | //! ``` 154 | //! use foreign_types::{ForeignTypeRef, foreign_type}; 155 | //! 156 | //! mod foo_sys { 157 | //! pub enum FOO {} 158 | //! pub enum BAR {} 159 | //! 160 | //! extern { 161 | //! pub fn FOO_free(foo: *mut FOO); 162 | //! pub fn BAR_free(bar: *mut BAR); 163 | //! pub fn BAR_get_foo(bar: *mut BAR) -> *mut FOO; 164 | //! } 165 | //! } 166 | //! 167 | //! foreign_type! { 168 | //! /// A Foo. 169 | //! pub unsafe type Foo: Sync + Send { 170 | //! type CType = foo_sys::FOO; 171 | //! fn drop = foo_sys::FOO_free; 172 | //! } 173 | //! 174 | //! /// A Bar. 175 | //! pub unsafe type Bar: Sync + Send { 176 | //! type CType = foo_sys::BAR; 177 | //! fn drop = foo_sys::BAR_free; 178 | //! } 179 | //! } 180 | //! 181 | //! impl BarRef { 182 | //! fn foo(&self) -> &FooRef { 183 | //! unsafe { FooRef::from_ptr(foo_sys::BAR_get_foo(self.as_ptr())) } 184 | //! } 185 | //! 186 | //! fn foo_mut(&mut self) -> &mut FooRef { 187 | //! unsafe { FooRef::from_ptr_mut(foo_sys::BAR_get_foo(self.as_ptr())) } 188 | //! } 189 | //! } 190 | //! 191 | //! # fn main() {} 192 | //! ``` 193 | #![no_std] 194 | #![warn(missing_docs)] 195 | #![doc(html_root_url = "https://docs.rs/foreign-types/0.5")] 196 | 197 | #[cfg(feature = "std")] 198 | extern crate std; 199 | 200 | #[doc(hidden)] 201 | pub use foreign_types_macros::foreign_type_impl; 202 | #[doc(inline)] 203 | pub use foreign_types_shared::{ForeignType, ForeignTypeRef, Opaque}; 204 | 205 | #[doc(hidden)] 206 | pub mod export { 207 | pub use core::borrow::{Borrow, BorrowMut}; 208 | pub use core::clone::Clone; 209 | pub use core::convert::{AsMut, AsRef}; 210 | pub use core::marker::{PhantomData, Send, Sync}; 211 | pub use core::ops::{Deref, DerefMut, Drop}; 212 | pub use core::ptr::NonNull; 213 | 214 | #[cfg(feature = "std")] 215 | pub use std::borrow::ToOwned; 216 | } 217 | 218 | /// A macro to easily define wrappers for foreign types. 219 | /// 220 | /// # Examples 221 | /// 222 | /// ``` 223 | /// use foreign_types::foreign_type; 224 | /// 225 | /// # mod openssl_sys { pub type SSL = (); pub unsafe fn SSL_free(_: *mut SSL) {} pub unsafe fn SSL_dup(x: *mut SSL) -> *mut SSL {x} } 226 | /// # mod foo_sys { pub type THING = (); pub unsafe fn THING_free(_: *mut THING) {} } 227 | /// foreign_type! { 228 | /// /// Documentation for the owned type. 229 | /// pub unsafe type Ssl: Sync + Send { 230 | /// type CType = openssl_sys::SSL; 231 | /// fn drop = openssl_sys::SSL_free; 232 | /// fn clone = openssl_sys::SSL_dup; 233 | /// } 234 | /// 235 | /// /// This type immutably borrows other data and has a limited lifetime! 236 | /// pub unsafe type Thing<'a>: Send { 237 | /// type CType = foo_sys::THING; 238 | /// type PhantomData = &'a (); 239 | /// fn drop = foo_sys::THING_free; 240 | /// } 241 | /// } 242 | /// 243 | /// # fn main() {} 244 | /// ``` 245 | #[macro_export(local_inner_macros)] 246 | macro_rules! foreign_type { 247 | ($($t:tt)*) => { 248 | $crate::foreign_type_impl!($crate $($t)*); 249 | }; 250 | } 251 | -------------------------------------------------------------------------------- /foreign-types/tests/test.rs: -------------------------------------------------------------------------------- 1 | use foreign_types::foreign_type; 2 | 3 | mod foo_sys { 4 | pub enum Foo {} 5 | 6 | pub unsafe extern "C" fn foo_drop(_: *mut Foo) {} 7 | pub unsafe extern "C" fn foo_clone(ptr: *mut Foo) -> *mut Foo { 8 | ptr 9 | } 10 | 11 | pub unsafe extern "C" fn foo_drop_requiring_cast(_: *mut ()) {} 12 | pub unsafe extern "C" fn foo_clone_requiring_cast(ptr: *mut ()) -> *mut () { 13 | ptr 14 | } 15 | } 16 | 17 | foreign_type! { 18 | pub unsafe type Foo<'a, T>: Sync + Send { 19 | type CType = foo_sys::Foo; 20 | type PhantomData = &'a T; 21 | fn drop = foo_sys::foo_drop; 22 | fn clone = foo_sys::foo_clone; 23 | } 24 | 25 | pub unsafe type FooNoClone { 26 | type CType = foo_sys::Foo; 27 | fn drop = foo_sys::foo_drop; 28 | } 29 | 30 | pub unsafe type FooClosure { 31 | type CType = foo_sys::Foo; 32 | fn drop = |p| foo_sys::foo_drop(p); 33 | fn clone = |p| foo_sys::foo_clone(p); 34 | } 35 | 36 | pub unsafe type FooClosureCast { 37 | type CType = foo_sys::Foo; 38 | fn drop = |p| foo_sys::foo_drop_requiring_cast(p as _); 39 | fn clone = |p| foo_sys::foo_clone_requiring_cast(p as _) as _; 40 | } 41 | } 42 | --------------------------------------------------------------------------------