mfio_derive/
lib.rs

1use proc_macro::TokenStream;
2use quote::*;
3use syn::*;
4
5#[proc_macro_derive(SyncIoRead)]
6pub fn io_read(item: TokenStream) -> TokenStream {
7    let st = parse_macro_input!(item as ItemStruct);
8
9    let ident = &st.ident;
10    let type_gens = st.generics.split_for_impl().1;
11    let impl_bounds = &st.generics.params;
12    let where_bounds = st.generics.where_clause.as_ref().map(|v| &v.predicates);
13
14    let impl_comma = if !impl_bounds.trailing_punct() && !impl_bounds.is_empty() {
15        Some(token::Comma::default())
16    } else {
17        None
18    };
19
20    quote! {
21        impl<#impl_bounds #impl_comma __Pos: 'static> mfio::traits::sync::SyncIoRead<__Pos> for #ident #type_gens where #ident #type_gens: mfio::traits::IoRead<__Pos> + mfio::backend::IoBackend, #where_bounds {}
22    }.into()
23}
24
25#[proc_macro_derive(SyncIoWrite)]
26pub fn io_write(item: TokenStream) -> TokenStream {
27    let st = parse_macro_input!(item as ItemStruct);
28
29    let ident = &st.ident;
30    let type_gens = st.generics.split_for_impl().1;
31    let impl_bounds = &st.generics.params;
32    let where_bounds = st.generics.where_clause.as_ref().map(|v| &v.predicates);
33
34    let impl_comma = if !impl_bounds.trailing_punct() && !impl_bounds.is_empty() {
35        Some(token::Comma::default())
36    } else {
37        None
38    };
39
40    quote! {
41        impl<#impl_bounds #impl_comma __Pos: 'static> mfio::traits::sync::SyncIoWrite<__Pos> for #ident #type_gens where #ident #type_gens: mfio::traits::IoWrite<__Pos> + mfio::backend::IoBackend, #where_bounds {}
42    }.into()
43}