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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
//! Proc macros for [Interoptopus](https://github.com/ralfbiedert/interoptopus). //! //! Items in here will be re-exported by [the main crate](https://crates.io/crates/interoptopus). extern crate proc_macro; // Apparently needed to be imported like this. mod constants; mod functions; mod service; mod types; mod util; use proc_macro::TokenStream; use syn::{parse_macro_input, AttributeArgs}; /// Enable a `struct` or `enum` to appear in generated bindings. /// /// This will derive [`CTypeInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.CTypeInfo.html) based on the _visible_ information in the type definition. This /// is the preferred way of enabling FFI types; although in some cases (e.g., when dealing with /// types outside of your control) you will have to implement a **surrogate** manually, see below. /// /// A number of attributes are available: /// /// | Attribute | On | Explanation | /// | --- | --- | --- | /// | `name="X"` | `struct`,`enum` | Uses `name` as the base interop name instead of the item's Rust name.<sup>1</sup> | /// | `namespace="X"` | `struct`,`enum` | Determine which namespace or file item should go. <sup>2</sup> /// | `skip(x)` | `struct,enum` | Skip field or variant `x` in the definition, e.g., some `x` of [`PhantomData`](std::marker::PhantomData). <sup>⚠️</sup> /// | `patterns(p)` | `struct`,`enum` | Mark this type as part of a pattern, see below. <sup>2</sup> /// | `opaque` | `struct` | Creates an opaque type without fields. Can only be used behind a pointer. | /// | `surrogates(x="f")` | `struct` | Invoke function `f` to provide a [`CTypeInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.CTypeInfo.html) for field `x`, see below. <sup>⚠️</sup> /// | `visibility(x="v")` | `struct` | Override visibility for field `x` as `public` or `private`; `_` means all fields. <sup>2</sup> /// | `debug` | * | Print generated helper code in console. /// | `unsafe` | * | Unlocks unsafe options marked: <sup>⚠️</sup> /// /// <sup>1</sup> While a type's name must be unique (even across modules) backends are free to further transform this name, e.g., by converting /// `MyVec` to `LibraryMyVec`. In other words, using `name` will change a type's name, but not using `name` is no guarantee the final name will /// not be modified. /// /// <sup>2</sup> Will not be reflected in C backend, but available to languages supporting them, /// e.g., C# will emit field visibility and generate classes from service patterns. /// /// <sup>⚠️</sup> This attribute can lead to undefined behavior when misapplied. You should only /// suppress fields that have no impact on the type layout (e.g., zero-sized `Phantom` data). /// When using surrogates you must ensure the surrogate matches. /// /// /// # Including Types /// /// In contrast to functions and constants types annotated with `#[ffi_type]` will be detected /// automatically and do not have to be explicitly mentioned for the definition of the `inventory!()`. /// /// # Surrogates /// /// When dealing with types outside of your control you will not be able to implement [`CTypeInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.CTypeInfo.html) for them. /// Instead you need a **surrogate**, a helper function which returns that info for the type. /// /// The surrogate's signature is: /// /// ```rust /// # use interoptopus::lang::c::CType; /// fn some_foreign_type() -> CType { /// // Return an appropriate CType /// # interoptopus::lang::c::CType::Primitive(interoptopus::lang::c::PrimitiveType::U8) /// } /// ``` /// /// # Patterns /// /// Patterns allow you to write, and backends to generate more idiomatic code. The following /// patterns are currently supported by this annotation: /// /// | Pattern | On | Explanation | /// | --- | --- | --- | /// | `ffi_error` | `enum` | Denotes this as a [`FFIError`](https://docs.rs/interoptopus/latest/interoptopus/patterns/result/trait.FFIError.html). | /// /// # Examples /// /// ``` /// use interoptopus::ffi_type; /// /// #[ffi_type(opaque, name = "MyVec")] /// #[derive(Copy, Clone, Debug)] /// #[repr(C)] /// pub struct Vec2f32 { /// pub x: f32, /// pub y: f32, /// pub z: f32, /// } /// ``` /// #[proc_macro_attribute] // Can now be used as `#[my_attribute]` pub fn ffi_type(attr: TokenStream, item: TokenStream) -> TokenStream { let input = proc_macro2::TokenStream::from(item); let attr_args = parse_macro_input!(attr as AttributeArgs); let rval = types::ffi_type(attr_args, input); rval.into() } /// Enable an `extern "C"` function to appear in generated bindings. /// /// This will derive [`FunctionInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.FunctionInfo.html) for a helper struct /// of the same name containing the function's name, parameters and return value. /// /// The following attributes can be provided: /// /// | Attribute | Explanation | /// | --- | --- | /// | `surrogates(x="y")` | Invoke function `y` to provide a [`CTypeInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.CTypeInfo.html) for parameter `x`, see below. <sup>⚠️</sup> /// | `debug` | Print generated helper code in console. /// | `unsafe` | Unlocks unsafe options marked: <sup>⚠️</sup> /// /// <sup>⚠️</sup> This attribute can lead to undefined behavior when misapplied. /// When using surrogates you must ensure the surrogate matches the parameter's type. /// /// # Surrogates /// /// When dealing with types outside of your control you will not be able to implement [`CTypeInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.CTypeInfo.html) for them. /// Instead you need a **surrogate**, a helper function which returns that info for the type. /// /// The surrogate's signature is: /// /// ```rust /// # use interoptopus::lang::c::CType; /// fn some_foreign_type() -> CType { /// // Return an appropriate CType /// # interoptopus::lang::c::CType::Primitive(interoptopus::lang::c::PrimitiveType::U8) /// } /// ``` /// /// # Example /// /// ``` /// use interoptopus::ffi_function; /// /// #[ffi_function] /// #[no_mangle] /// pub extern "C" fn my_function(x: u32) -> u32 { /// x /// } /// ``` #[proc_macro_attribute] // Can now be used as `#[my_attribute]` pub fn ffi_function(attr: TokenStream, item: TokenStream) -> TokenStream { let input = proc_macro2::TokenStream::from(item); let attr_args = parse_macro_input!(attr as AttributeArgs); let rval = functions::ffi_function(attr_args, input); rval.into() } /// Enables a `const` to appear in generated bindings. /// /// This will derive [`ConstantInfo`](https://docs.rs/interoptopus/latest/interoptopus/lang/rust/trait.ConstantInfo.html) for a helper struct of the /// same name containing the const's name and value. /// /// Constant evaluation is supported. /// /// In order to appear in generated bindings the constant has to be mentioned in the definition /// of the libaries [`inventory!()`](https://docs.rs/interoptopus/latest/interoptopus/macro.inventory.html). /// /// # Examples /// /// ``` /// use interoptopus::ffi_constant; /// # const fn double(x: u8) -> u8 { 2 * x } /// /// #[ffi_constant] /// const SOME_CONST: u32 = 314; /// /// #[ffi_constant] /// const COMPUTED_CONST: u8 = double(12); // will export 24 /// /// ``` #[proc_macro_attribute] // Can now be used as `#[my_attribute]` pub fn ffi_constant(attr: TokenStream, item: TokenStream) -> TokenStream { let input = proc_macro2::TokenStream::from(item); let attr_args = parse_macro_input!(attr as AttributeArgs); let rval = constants::ffi_constant(attr_args, input); rval.into() } /// Creates a FFI service from an `impl Service {}` block. /// /// See the [service module](https://docs.rs/interoptopus/latest/interoptopus/patterns/service/index.html) for an introduction into services. /// /// # Requirements /// /// For this attribute to work a number of preconditions must be fulfilled: /// /// - The attribute must be used on `impl SomeType {}` blocks /// - The `error` parameter must be provided and point to an [`FFIError`](https://docs.rs/interoptopus/latest/interoptopus/patterns/result/trait.FFIError.html) type. /// - The respective `SomeType` type must have an [`#[ffi_type(opaque)]`](macro@crate::ffi_type) attribute. /// - Exactly one method inside the `impl {}` must be marked with [`#[ffi_service_ctor]`](macro@crate::ffi_service_ctor). /// /// We recommend to have a look at the [reference project](https://github.com/ralfbiedert/interoptopus/blob/master/interoptopus_reference_project/src/patterns/service.rs). /// /// # Parameters /// /// The following attributes can be provided: /// /// | Attribute | Explanation | /// | --- | --- | /// | `error = "t"` | Use `t` as the [`FFIError`](https://docs.rs/interoptopus/latest/interoptopus/patterns/result/trait.FFIError.html) type, mandatory. /// | `prefix = "p"` | Add `p` to all generated method names. /// /// # Example /// /// ``` /// # use std::fmt::{Display, Formatter}; /// # use interoptopus::patterns::result::FFIError; /// # #[derive(Debug)] /// # pub enum Error { /// # Bad, /// # } /// # /// # impl Display for Error { /// # fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result { /// # Ok(()) /// # } /// # } /// # /// # impl std::error::Error for Error {} /// # /// # #[ffi_type(patterns(ffi_error))] /// # #[repr(C)] /// # pub enum MyFFIError { /// # Ok = 0, /// # NullPassed = 1, /// # Panic = 2, /// # OtherError = 3, /// # } /// # /// # impl FFIError for MyFFIError { /// # const SUCCESS: Self = Self::Ok; /// # const NULL: Self = Self::NullPassed; /// # const PANIC: Self = Self::Panic; /// # } /// # /// # impl From<Error> for MyFFIError { /// # fn from(x: Error) -> Self { /// # match x { /// # Error::Bad => Self::OtherError, /// # } /// # } /// # } /// # /// use interoptopus::{ffi_type, ffi_service, ffi_service_ctor}; /// /// #[ffi_type(opaque)] /// pub struct SimpleService { } /// /// #[ffi_service(error = "MyFFIError", prefix = "simple_service_")] /// impl SimpleService { /// /// #[ffi_service_ctor] /// pub fn new_with(some_value: u32) -> Result<Self, Error> { /// Ok(Self { }) /// } /// } /// ``` /// #[proc_macro_attribute] // Can now be used as `#[my_attribute]` pub fn ffi_service(attr: TokenStream, item: TokenStream) -> TokenStream { let input = proc_macro2::TokenStream::from(item); let attr_args = parse_macro_input!(attr as AttributeArgs); let rval = service::ffi_service(attr_args, input); rval.into() } /// Inside a [`#[ffi_service]`](macro@crate::ffi_service) block, mark the FFI constructor. /// /// See the [service module](https://docs.rs/interoptopus/latest/interoptopus/patterns/service/index.html) for an introduction into services. /// /// # Requirements /// /// For this attribute to work a number of preconditions must be fulfilled: /// /// - The attribute must be used inside an `impl SomeType {}` block marked with [`#[ffi_service]`](macro@crate::ffi_service). /// - It must be applied to exactly one method. /// - The method must return `Result<Self, Error>`. /// /// We recommend to have a look at the [reference project](https://github.com/ralfbiedert/interoptopus/blob/master/interoptopus_reference_project/src/patterns/service.rs). /// /// # Example /// /// ``` /// # use std::fmt::{Display, Formatter}; /// # use interoptopus::patterns::result::FFIError; /// # /// # #[derive(Debug)] /// # pub enum Error { /// # Bad, /// # } /// # /// # impl Display for Error { /// # fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result { /// # Ok(()) /// # } /// # } /// # /// # impl std::error::Error for Error {} /// # /// # #[ffi_type(patterns(ffi_error))] /// # #[repr(C)] /// # pub enum MyFFIError { /// # Ok = 0, /// # NullPassed = 1, /// # Panic = 2, /// # OtherError = 3, /// # } /// # /// # impl FFIError for MyFFIError { /// # const SUCCESS: Self = Self::Ok; /// # const NULL: Self = Self::NullPassed; /// # const PANIC: Self = Self::Panic; /// # } /// # /// # impl From<Error> for MyFFIError { /// # fn from(x: Error) -> Self { /// # match x { /// # Error::Bad => Self::OtherError, /// # } /// # } /// # } /// # /// use interoptopus::{ffi_type, ffi_service, ffi_service_ctor}; /// /// #[ffi_type(opaque)] /// pub struct SimpleService { } /// /// #[ffi_service(error = "MyFFIError", prefix = "simple_service_")] /// impl SimpleService { /// /// #[ffi_service_ctor] /// pub fn new_with(some_value: u32) -> Result<Self, Error> { /// Ok(Self { }) /// } /// } /// ``` #[proc_macro_attribute] pub fn ffi_service_ctor(_attr: TokenStream, item: TokenStream) -> TokenStream { item } /// Inside a [`#[ffi_service]`](macro@crate::ffi_service) block, provide special directives to functions. /// /// This is an optional attribute that can be applied to some methods. /// /// By default service methods /// must return a `Result<(), Error>` return type that will be mapped to an `FFIError` and /// transparently checked in languages supporting the pattern. /// However, sometimes you might want to return an actual value. Using this attribute you can /// opt out of error mapping and instead return the value as-is. /// /// See the [service module](https://docs.rs/interoptopus/latest/interoptopus/patterns/service/index.html) for an introduction into services. /// /// # Parameters /// /// The following attributes can be provided: /// /// | Attribute | Explanation | /// | --- | --- | /// | `direct` | Mark methods not returning a `Result<(), Error>`; will return [`default()`](Default::default) on panic. /// /// ⚠️ Note that generated methods always add panic guards. Since `direct` methods have no /// other way to signal errors they will return [`Default::default()`] instead if a panic /// is encountered. If you compiled Interoptopus with the `log` feature a message will be emitted /// in that case. /// /// # Example /// /// ``` /// # use std::fmt::{Display, Formatter}; /// # use interoptopus::patterns::result::FFIError; /// # /// # #[derive(Debug)] /// # pub enum Error { /// # Bad, /// # } /// # /// # impl Display for Error { /// # fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result { /// # Ok(()) /// # } /// # } /// # /// # impl std::error::Error for Error {} /// # /// # #[ffi_type(patterns(ffi_error))] /// # #[repr(C)] /// # pub enum MyFFIError { /// # Ok = 0, /// # NullPassed = 1, /// # Panic = 2, /// # OtherError = 3, /// # } /// # /// # impl FFIError for MyFFIError { /// # const SUCCESS: Self = Self::Ok; /// # const NULL: Self = Self::NullPassed; /// # const PANIC: Self = Self::Panic; /// # } /// # /// # impl From<Error> for MyFFIError { /// # fn from(x: Error) -> Self { /// # match x { /// # Error::Bad => Self::OtherError, /// # } /// # } /// # } /// # /// use interoptopus::{ffi_type, ffi_service, ffi_service_ctor, ffi_service_method}; /// /// #[ffi_type(opaque)] /// pub struct SimpleService { } /// /// #[ffi_service(error = "MyFFIError", prefix = "simple_service_")] /// impl SimpleService { /// /// #[ffi_service_ctor] /// pub fn new_with(some_value: u32) -> Result<Self, Error> { /// Ok(Self { }) /// } /// /// #[ffi_service_method(direct)] /// pub fn return_value(&self) -> u32 { /// 123 /// } /// /// #[ffi_service_method(direct)] /// #[allow(unconditional_panic)] /// pub fn oops(&self, x: u32) -> u32 { /// let array = [0, 1, 2]; /// /// // This will panic. The method will return 0 instead. /// array[5] /// } /// } /// ``` #[proc_macro_attribute] pub fn ffi_service_method(_attr: TokenStream, item: TokenStream) -> TokenStream { item }