• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Create an opaque struct suitable for use as an FFI pointer.
2 ///
3 /// The internal representation uses the recommendation in the [nomicon].
4 ///
5 /// [nomicon]: https://doc.rust-lang.org/stable/nomicon/ffi.html#representing-opaque-structs
6 #[macro_export]
7 macro_rules! opaque_type {
8     (
9         $(#[$struct_attrs:meta])*
10         $struct_vis:vis struct $struct_name:ident;
11     ) => {
12         // Create the struct with the fields recommended by the nomicon.
13         $(#[$struct_attrs])*
14         #[repr(C)]
15         $struct_vis struct $struct_name {
16             _data: [u8; 0],
17             _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
18         }
19 
20         // Impl Debug, just show the struct name.
21         impl core::fmt::Debug for $struct_name {
22             fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23                 f.debug_struct(stringify!($struct_name)).finish()
24             }
25         }
26     }
27 }
28