1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
//! This crate `cpp` provides macros that allow embedding arbitrary C++ code. //! //! # Usage //! //! This crate must be used in tandem with the `cpp_build` crate. A basic Cargo //! project which uses these projects would have a structure like the following: //! //! ```text //! crate //! |-- Cargo.toml //! |-- src //! |-- lib.rs //! |-- build.rs //! ``` //! //! Where the files look like the following: //! //! #### Cargo.toml //! //! ```toml //! [package] //! build = "build.rs" //! //! [dependencies] //! cpp = "0.4" //! //! [build-dependencies] //! cpp_build = "0.4" //! ``` //! //! #### build.rs //! //! ```ignore //! extern crate cpp_build; //! //! fn main() { //! cpp_build::build("src/lib.rs"); //! } //! ``` //! //! #### lib.rs //! //! ```ignore //! #[macro_use] //! extern crate cpp; //! //! cpp!{{ //! #include <stdio.h> //! }} //! //! fn main() { //! unsafe { //! cpp!([] { //! printf("Hello, World!\n"); //! }); //! } //! } //! ``` #[macro_use] #[allow(unused_imports)] extern crate cpp_macros; #[doc(hidden)] pub use cpp_macros::*; /// Internal macro which is used to locate the rust! invocations in the /// C++ code embbeded in cpp! invocation, to translate them into extern /// functions #[doc(hidden)] #[macro_export] macro_rules! __cpp_internal { (@find_rust_macro [$($a:tt)*] rust!($($rust_body:tt)*) $($rest:tt)*) => { __cpp_internal!{ @expand_rust_macro [$($a)*] $($rust_body)* } __cpp_internal!{ @find_rust_macro [$($a)*] $($rest)* } }; (@find_rust_macro [$($a:tt)*] ( $($in:tt)* ) $($rest:tt)* ) => { __cpp_internal!{ @find_rust_macro [$($a)*] $($in)* $($rest)* } }; (@find_rust_macro [$($a:tt)*] [ $($in:tt)* ] $($rest:tt)* ) => { __cpp_internal!{ @find_rust_macro [$($a)*] $($in)* $($rest)* } }; (@find_rust_macro [$($a:tt)*] { $($in:tt)* } $($rest:tt)* ) => { __cpp_internal!{ @find_rust_macro [$($a)*] $($in)* $($rest)* } }; (@find_rust_macro [$($a:tt)*] $t:tt $($rest:tt)*) => { __cpp_internal!{ @find_rust_macro [$($a)*] $($rest)* } }; (@find_rust_macro [$($a:tt)*]) => {}; (@expand_rust_macro [$($a:tt)*] $i:ident [$($an:ident : $at:ty as $ac:tt),*] {$($body:tt)*}) => { #[doc(hidden)] $($a)* extern "C" fn $i($($an : *const $at),*) { $(let $an : $at = unsafe { $an.read() };)* (|| { $($body)* })(); $(::std::mem::forget($an);)* } }; (@expand_rust_macro [$($a:tt)*] $i:ident [$($an:ident : $at:ty as $ac:tt),*] -> $rt:ty as $rc:tt {$($body:tt)*}) => { #[doc(hidden)] $($a)* extern "C" fn $i($($an : *const $at, )* rt : *mut $rt) -> *mut $rt { $(let $an : $at = unsafe { $an.read() };)* { #[allow(unused_mut)] let mut lambda = || {$($body)*}; unsafe { ::std::ptr::write(rt, lambda()) }; } $(::std::mem::forget($an);)* rt } }; (@expand_rust_macro $($invalid:tt)*) => { compile_error!(concat!( "Cannot parse rust! macro: ", stringify!([ $($invalid)* ]) )) }; } /// This macro is used to embed arbitrary C++ code. /// /// There are two variants of the `cpp!` macro. The first variant is used for /// raw text inclusion. Text is included into the generated `C++` file in the /// order which they were defined, inlining module declarations. /// /// ```ignore /// cpp! {{ /// #include <stdint.h> /// #include <stdio.h> /// }} /// ``` /// /// The second variant is used to embed C++ code within rust code. A list of /// variable names which should be captured are taken as the first argument, /// with their corresponding C++ type. The body is compiled as a C++ function. /// /// This variant of the macro may only be invoked in expression context, and /// requires an `unsafe` block, as it is performing FFI. /// /// ```ignore /// let y: i32 = 10; /// let mut z: i32 = 20; /// let x: i32 = unsafe { cpp!([y as "int32_t", mut z as "int32_t"] -> i32 as "int32_t" { /// z++; /// return y + z; /// })}; /// ``` /// /// You can also put the unsafe keyword as the first keyword of the cpp! macro, which /// has the same effect as putting the whole macro in an unsafe block: /// /// ```ignore /// let x: i32 = cpp!(unsafe [y as "int32_t", mut z as "int32_t"] -> i32 as "int32_t" { /// z++; /// return y + z; /// }); /// ``` /// /// ## rust! pseudo-macro /// /// The cpp! macro can contain, in the C++ code, a rust! sub-macro, which allows /// to include rust code in C++ code. This is useful to /// implement callback or override virtual functions. Example: /// /// ```ignore /// trait MyTrait { /// fn compute_value(&self, x : i32) -> i32; /// } /// /// cpp!{{ /// struct TraitPtr { void *a,*b; }; /// class MyClassImpl : public MyClass { /// public: /// TraitPtr m_trait; /// int computeValue(int x) const override { /// return rust!(MCI_computeValue [m_trait : &MyTrait as "TraitPtr", x : i32 as "int"] /// -> i32 as "int" { /// m_trait.compute_value(x) /// }); /// } /// } /// }} /// ``` /// /// The syntax for the rust! macro is: /// ```ignore /// rust!($uniq_ident:ident [$($arg_name:ident : $arg_rust_type:ty as $arg_c_type:tt),*] /// $(-> $ret_rust_type:ty as $rust_c_type:tt)* {$($body:tt)*}) /// ``` /// uniq_ident is an unique identifier which will be used to name the extern function #[macro_export] macro_rules! cpp { // raw text inclusion ({$($body:tt)*}) => { __cpp_internal!{ @find_rust_macro [#[no_mangle] pub] $($body)*} }; // inline closure ([$($captures:tt)*] $($rest:tt)*) => { { __cpp_internal!{ @find_rust_macro [] $($rest)*} #[allow(unused)] #[derive(__cpp_internal_closure)] enum CppClosureInput { Input = (stringify!([$($captures)*] $($rest)*), 0).1 } __cpp_closure_impl![$($captures)*] } }; // wrap unsafe (unsafe $($tail:tt)*) => { unsafe { cpp!($($tail)*) } }; } #[doc(hidden)] pub trait CppTrait { type BaseType; const ARRAY_SIZE: usize; const CPP_TYPE: &'static str; } /// This macro allow to wrap a relocatable C++ struct or class that might have /// destructor or copy constructor, instantiating the Drop and Clone trait /// appropriately. /// /// ```ignore /// cpp_class!(pub unsafe struct MyClass as "MyClass"); /// impl MyClass { /// fn new() -> Self { /// unsafe { cpp!([] -> MyClass as "MyClass" { return MyClass(); }) } /// } /// fn member_function(&self, param : i32) -> i32 { /// unsafe { cpp!([self as "const MyClass*", param as "int"] -> i32 as "int" { /// return self->member_function(param); /// }) } /// } /// } /// ``` /// /// This will create a rust struct MyClass, which has the same size and /// alignment as the the C++ class "MyClass". It will also implement the `Drop` trait /// calling the destructor, the `Clone` trait calling the copy constructor, if the /// class is copyable (or `Copy` if it is trivially copyable), and `Default` if the class /// is default constructible /// /// ## Derived Traits /// /// The `Default`, `Clone` and `Copy` traits are implicitly implemented if the C++ /// type has the corresponding constructors. /// /// You can add the `#[derive(...)]` attribute in the macro in order to get automatic /// implementation of the following traits: /// /// * The trait `PartialEq` will call the C++ `operator==`. /// * You can add the trait `Eq` if the semantics of the C++ operator are those of `Eq` /// * The trait `PartialOrd` need the C++ `operator<` for that type. `lt`, `le`, `gt` and /// `ge` will use the corresponding C++ operator if it is defined, otherwise it will /// fallback to the less than operator. For PartialOrd::partial_cmp, the `operator<` will /// be called twice. Note that it will never return None. /// * The trait `Ord` can also be specified when the semantics of the `operator<` corresponds /// to a total order /// /// ## Safety Warning /// /// Use of this macro is highly unsafe. Only certain C++ classes can be bound /// to, C++ classes may perform arbitrary unsafe operations, and invariants are /// easy to break. /// /// A notable restriction is that this macro only works if the C++ class is /// relocatable, i.e., can be moved in memory using `memmove`. /// /// Unfortunately, as the STL often uses internal self-references for /// optimization purposes, such as the small-string optimization, this disallows /// most std:: classes. This restriction exists because safe rust is allowed to /// move your types around. /// /// Most C++ types which do not contain self-references will be compatible, /// although this property cannot be statically checked by rust-cpp. /// #[macro_export] macro_rules! cpp_class { ($(#[$($attrs:tt)*])* unsafe struct $name:ident as $type:expr) => { __cpp_class_internal!{@parse [ $(#[$($attrs)*])* ] [] [unsafe struct $name as $type] } }; ($(#[$($attrs:tt)*])* pub unsafe struct $name:ident as $type:expr) => { __cpp_class_internal!{@parse [ $(#[$($attrs)*])* ] [pub] [unsafe struct $name as $type] } }; ($(#[$($attrs:tt)*])* pub($($pub:tt)*) unsafe struct $name:ident as $type:expr) => { __cpp_class_internal!{@parse [ $(#[$($attrs)*])* ] [pub($($pub)*)] [unsafe struct $name as $type] } }; } /// Implementation details for cpp_class! #[doc(hidden)] #[macro_export] macro_rules! __cpp_class_internal { (@parse [$($attrs:tt)*] [$($vis:tt)*] [unsafe struct $name:ident as $type:expr]) => { __cpp_class_internal!{@parse_attributes [ $($attrs)* ] [ #[derive(__cpp_internal_class)] #[repr(C)] $($vis)* struct $name { _opaque : [<$name as $crate::CppTrait>::BaseType ; <$name as $crate::CppTrait>::ARRAY_SIZE + (stringify!($($attrs)* $($vis)* unsafe struct $name as $type), 0).1] } ]} }; (@parse_attributes [] [$($result:tt)*]) => ( $($result)* ); (@parse_attributes [#[derive($($der:ident),*)] $($tail:tt)* ] [$($result:tt)*] ) => (__cpp_class_internal!{@parse_derive [$($der),*] @parse_attributes [$($tail)*] [ $($result)* ] } ); (@parse_attributes [ #[$m:meta] $($tail:tt)* ] [$($result:tt)*]) => (__cpp_class_internal!{@parse_attributes [$($tail)*] [ #[$m] $($result)* ] } ); (@parse_derive [] @parse_attributes $($result:tt)*) => (__cpp_class_internal!{@parse_attributes $($result)*} ); (@parse_derive [PartialEq $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [PartialOrd $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [Ord $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [Default $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [Clone $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [Copy $(,$tail:ident)*] $($result:tt)*) => ( __cpp_class_internal!{@parse_derive [$($tail),*] $($result)*} ); (@parse_derive [$i:ident $(,$tail:ident)*] @parse_attributes [$($attr:tt)*] [$($result:tt)*] ) => ( __cpp_class_internal!{@parse_derive [$($tail),*] @parse_attributes [$($attr)*] [ #[derive($i)] $($result)* ] } ); }