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
/*
* SPDX-License-Identifier: MIT
*/
use proc_macro::TokenStream;
/// Implement RawWrapper trait.
/// `#[raw]` attribute must be added before field with wrapped raw value.
///
/// # Examples
///
/// ```no_run,ignore
/// #[derive(RawWrapper, Debug)]
/// #[repr(transparent)]
/// pub struct MainLoopRef {
/// #[raw]
/// raw: pw_sys::pw_main_loop,
/// }
/// ```
#[proc_macro_derive(RawWrapper, attributes(raw))]
pub fn raw_wrapper(input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::derive_raw_wrapper::derive_raw_wrapper(input.into()).into()
}
/// Implement Wrapper trait.
/// `#[raw_wrapper]` attribute must be added before field with `NonNull<impl RawWrapper>` pointer to *Ref
/// struct, that implements RawWrapper
///
/// # Examples
///
/// ```no_run,ignore
/// #[derive(RawWrapper, Debug)]
/// #[repr(transparent)]
/// pub struct MainLoopRef {
/// #[raw]
/// raw: pw_sys::pw_main_loop,
/// }
///
/// #[derive(Wrapper, Debug)]
/// pub struct MainLoop {
/// #[raw_wrapper]
/// ref_: NonNull<MainLoopRef>,
///
/// pipewire: std::sync::Arc<PipeWire>,
/// }
/// ```
#[proc_macro_derive(Wrapper, attributes(raw_wrapper))]
pub fn wrapper(input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::derive_wrapper::derive_wrapper(input.into()).into()
}
/// Implement SpaInterface
///
/// # Arguments
///
/// * `methods` - struct with interface methods
///
/// ```no_run,ignore
/// #[derive(RawWrapper, Debug)]
/// #[spa_interface(methods=spa_sys::spa_loop_methods)]
/// #[repr(transparent)]
/// pub struct LoopRef {
/// #[raw]
/// raw: spa_sys::spa_loop,
/// }
/// ```
#[proc_macro_attribute]
pub fn spa_interface(attr: TokenStream, input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::spa_interface::spa_interface(attr.into(), input.into()).into()
}
/// Implement SpaInterface and
/// Proxied traits.
///
/// # Arguments
///
/// * `methods` - struct with interface methods
/// * `interface` - interface name
///
/// # Examples
///
/// ```no_run,ignore
/// #[derive(RawWrapper, Debug)]
/// #[interface(methods=pw_sys::pw_node_methods, interface="Node")]
/// #[repr(transparent)]
/// pub struct NodeRef {
/// #[raw]
/// raw: pw_sys::pw_node,
/// }
/// ```
#[proc_macro_attribute]
pub fn interface(attr: TokenStream, input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::interface::interface(attr.into(), input.into()).into()
}
/// Implement Wrapper trait for structure with the `ref_: Proxy<'c>` field.
/// Macros parameter will be used as target type to cast underlying proxy.
///
/// # Examples
///
/// ```no_run,ignore
/// #[derive(RawWrapper, Debug)]
/// #[interface(methods=pw_sys::pw_node_methods, interface="Node")]
/// #[repr(transparent)]
/// pub struct NodeRef {
/// #[raw]
/// raw: pw_sys::pw_node,
/// }
///
/// #[derive(Clone, Debug)]
/// #[proxy_wrapper(NodeRef)]
/// pub struct Node<'c> {
/// ref_: Proxy<'c>,
///
/// listeners: Listeners<Pin<Box<NodeEvents<'c>>>>,
/// }
/// ```
#[proc_macro_attribute]
pub fn proxy_wrapper(attr: TokenStream, input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::proxy_wrapper::proxy_wrapper(attr.into(), input.into()).into()
}
/// Add an *Info and *Builder structures after the enum definition.
/// For each enum variant will be added optional struct fields with value and flags.
///
/// # Examples
///
/// Derive:
/// ```no_run,ignore
/// #[allow(non_camel_case_types)]
/// #[derive(Debug)]
/// #[repr(u32)]
/// #[object_info(OBJECT_PROPS)]
/// pub enum ObjectPropType<'a> {
/// // Device
/// DEVICE(&'a PodStringRef) = Prop::DEVICE.raw,
/// DEVICE_NAME(&'a PodStringRef) = Prop::DEVICE_NAME.raw,
///
/// }
/// ```
///
/// Usage:
/// ```no_run,ignore
/// if let Ok(BasicType::OBJECT(object)) = param.downcast() {
/// match object.body_type() {
/// Type::OBJECT_PROP_INFO => {
/// let info = ObjectPropInfoInfo::try_from(object).unwrap();
/// println!("Prop info: {:?}", info);
/// }
/// _ => todo!(),
/// }
/// }
/// ```
#[proc_macro_attribute]
pub fn object_type_impl(attr: TokenStream, input: TokenStream) -> TokenStream {
pipewire_wrapper_macro_impl::object_type_impl::object_type_impl(attr.into(), input.into())
.into()
}