Skip to content

Commit

Permalink
update entry! to export interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Jan 12, 2024
1 parent 6d1c626 commit 32fe86a
Show file tree
Hide file tree
Showing 21 changed files with 638 additions and 1,251 deletions.
104 changes: 94 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ log = "0.4.19"
once_cell = "1.18.0"
rrplug_proc = { path = "./rrplug_proc" }
parking_lot = "0.12.1"
windows = { version = "0.52.0", features = ["Win32_Foundation", "Win32_System_LibraryLoader", "Win32_System_SystemServices"] }
bitflags = "2.4.1"

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
Expand Down
2 changes: 1 addition & 1 deletion rrplug_proc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rrplug_proc"
version = "3.0.0"
version = "4.0.0"
authors = ["catornot"]
description = "crate for proc macros of rrplug"
repository = "https://github.com/catornot/rrplug"
Expand Down
55 changes: 39 additions & 16 deletions rrplug_proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{
self, parse_macro_input, parse_str, punctuated::Punctuated, spanned::Spanned,
self, parse, parse_macro_input, parse_str, punctuated::Punctuated, spanned::Spanned,
AngleBracketedGenericArguments, DeriveInput, Error as SynError, FnArg, Ident, ImplItem,
ImplItemFn, ItemFn, ItemImpl, ReturnType, Stmt, Token, Type,
};
Expand Down Expand Up @@ -377,24 +377,44 @@ pub fn as_interface(_attr: TokenStream, item: TokenStream) -> TokenStream {
}
};

let new_impl = match funcs
let mut new_func = match funcs
.iter()
.find(|func| func.sig.ident.to_string() == "new")
.map(|func| func.block.stmts.clone())
.position(|func| func.sig.ident.to_string() == "new")
{
Some(funcs) => funcs,
Some(index) => funcs.remove(index),
None => {
return quote! {compile_error!("a interface must have a new function to initialize it");}
.into()
}
};

funcs.remove(
funcs
.iter()
.position(|func| func.sig.ident.to_string() == "new")
.unwrap(),
);
// the most sane solution me thinks
new_func = {
let ImplItemFn {
attrs,
vis,
defaultness,

mut sig,
block,
} = new_func;
let stmts = block.stmts;
sig.output = parse_str("-> rrplug::interfaces::interface::Interface<Self>").unwrap();

let tk = quote! {
#(#attrs)*
#vis #defaultness #sig {
use rrplug::interfaces::interface::AsInterface;
Self::as_interface({
#(#stmts)*
})
}
};
match parse(tk.into()) {
Ok(s) => s,
Err(err) => return err.to_compile_error().into(),
}
};

if funcs.is_empty() {
return quote! {compile_error!("perhaps you should add a function to your interface");}
Expand All @@ -413,7 +433,11 @@ pub fn as_interface(_attr: TokenStream, item: TokenStream) -> TokenStream {
.inputs
.first()
.map(|selfarg| match selfarg {
FnArg::Receiver(_) => Some(()),
FnArg::Receiver(recveiver)
if recveiver.reference.is_some() && recveiver.mutability.is_none() =>
{
Some(())
}
_ => None,
})
.flatten()
Expand Down Expand Up @@ -455,11 +479,12 @@ pub fn as_interface(_attr: TokenStream, item: TokenStream) -> TokenStream {
#(#attrs)*
#impl_token #generics #self_ty {
#(#funcs)*
#new_func
}

#(#attrs)*
impl #generics rrplug::interfaces::interface::AsInterface for #self_ty {
fn as_interface() -> rrplug::interfaces::interface::Interface<Self> {
fn as_interface(self) -> rrplug::interfaces::interface::Interface<Self> {
// make these extern c wrapped and offset the self since it will have the vtable
// TODO: make generic functions work (big headache)
#(
Expand All @@ -474,9 +499,7 @@ pub fn as_interface(_attr: TokenStream, item: TokenStream) -> TokenStream {

rrplug::interfaces::interface::Interface::new(
unsafe { std::ptr::NonNull::new_unchecked(VTABLE.as_ptr().cast_mut()) },
{
#(#new_impl)*
}
self
)
}
}
Expand Down
11 changes: 3 additions & 8 deletions rrplug_proc/src/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use proc_macro::TokenStream;
use quote::{quote, spanned::Spanned, ToTokens};
use syn::{
self,
parse::ParseStream,
parse::{Parse, Parser},
punctuated::Punctuated,
token::Comma,
FnArg, Ident, LitStr, Result as SynResult, Token, Type,
__private::TokenStream2,
parse_quote, parse_str, Error as SynError,
self, parse::Parse, parse::ParseStream, punctuated::Punctuated, token::Comma, FnArg, Ident,
LitStr, Result as SynResult, Token, Type, __private::TokenStream2, parse_quote, parse_str,
Error as SynError,
};

pub struct Arg {
Expand Down
Loading

0 comments on commit 32fe86a

Please sign in to comment.