pipewire_wrapper_macro_impl/
proxy_wrapper.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::{parse2, ItemStruct, TypePath};
4
5pub fn proxy_wrapper(attr: TokenStream, input: TokenStream) -> TokenStream {
6    let struct_info: ItemStruct = parse2(input.clone()).unwrap();
7    let interface_attr: TypePath = parse2(attr).unwrap();
8
9    let struct_ident = &struct_info.ident;
10    let ref_type_ident = interface_attr.path.get_ident().unwrap();
11
12    quote!(
13        #input
14
15        impl<'c> #struct_ident<'c> {
16            pub fn proxy(&self) -> &crate::core_api::proxy::Proxy {
17                &self.ref_
18            }
19
20            pub fn is_bound(&self) -> bool {
21                self.ref_.is_bound()
22            }
23        }
24
25        impl<'c> crate::wrapper::Wrapper for #struct_ident <'c> {
26            type RawWrapperType = #ref_type_ident;
27        }
28
29        impl <'c> std::ops::Deref for #struct_ident <'c> {
30            type Target = #ref_type_ident;
31
32            fn deref(&self) -> &'c Self::Target {
33                unsafe { #ref_type_ident::mut_from_raw_ptr(self.ref_.as_raw_ptr().cast()) }
34            }
35        }
36
37        impl<'c> std::ops::DerefMut for #struct_ident <'c> {
38            fn deref_mut(&mut self) -> &'c mut Self::Target {
39                unsafe { #ref_type_ident::mut_from_raw_ptr(self.ref_.as_raw_ptr().cast()) }
40            }
41        }
42
43        impl<'c> AsRef<#ref_type_ident> for #struct_ident <'c> {
44            fn as_ref(&self) -> &'c #ref_type_ident {
45                unsafe { #ref_type_ident::mut_from_raw_ptr(self.ref_.as_raw_ptr().cast()) }
46            }
47        }
48
49        impl<'c> AsMut<#ref_type_ident> for #struct_ident <'c> {
50            fn as_mut(&mut self) -> &'c mut #ref_type_ident {
51                unsafe { #ref_type_ident::mut_from_raw_ptr(self.ref_.as_raw_ptr().cast()) }
52            }
53        }
54    )
55}