-
Notifications
You must be signed in to change notification settings - Fork 643
Rust module bindings and macros for defining procedures #3444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ae1fa58
Rust module bindings and macros for defining procedures
gefjon 563aeb7
Clippy
gefjon 160594a
fix misspelled `no_run`
gefjon 679340a
Merge remote-tracking branch 'origin/master' into phoebe/procedure/ru…
gefjon 8d1f528
Make procedures able to be scheduled
gefjon ed78270
Fix doc test's imports
gefjon 99f27ce
Changes from Joshua's review
gefjon 04fd123
Preliminary procedure docs
gefjon 6819183
Fix copy-paste errors
gefjon ac9737f
Empty commit to re-try CI jobs
gefjon 1f163d3
Fix doc tests, add another example/test to module-test
gefjon 62673e4
clippy
gefjon 583c097
Don't define a new type, use an existing type
gefjon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| use crate::reducer::{assert_only_lifetime_generics, extract_typed_args}; | ||
| use crate::sym; | ||
| use crate::util::{check_duplicate, ident_to_litstr, match_meta}; | ||
| use proc_macro2::TokenStream; | ||
| use quote::quote; | ||
| use syn::parse::Parser as _; | ||
| use syn::{ItemFn, LitStr}; | ||
|
|
||
| #[derive(Default)] | ||
| pub(crate) struct ProcedureArgs { | ||
| /// For consistency with reducers: allow specifying a different export name than the Rust function name. | ||
| name: Option<LitStr>, | ||
| } | ||
|
|
||
| impl ProcedureArgs { | ||
| pub(crate) fn parse(input: TokenStream) -> syn::Result<Self> { | ||
| let mut args = Self::default(); | ||
| syn::meta::parser(|meta| { | ||
| match_meta!(match meta { | ||
| sym::name => { | ||
| check_duplicate(&args.name, &meta)?; | ||
| args.name = Some(meta.value()?.parse()?); | ||
| } | ||
| }); | ||
| Ok(()) | ||
| }) | ||
| .parse2(input)?; | ||
| Ok(args) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn procedure_impl(args: ProcedureArgs, original_function: &ItemFn) -> syn::Result<TokenStream> { | ||
| let func_name = &original_function.sig.ident; | ||
| let vis = &original_function.vis; | ||
|
|
||
| let procedure_name = args.name.unwrap_or_else(|| ident_to_litstr(func_name)); | ||
|
|
||
| assert_only_lifetime_generics(original_function, "procedures")?; | ||
|
|
||
| let typed_args = extract_typed_args(original_function)?; | ||
|
|
||
| // TODO: Require that procedures be `async` functions syntactically, | ||
| // and use `futures_util::FutureExt::now_or_never` to poll them. | ||
| // if !&original_function.sig.asyncness.is_some() { | ||
| // return Err(syn::Error::new_spanned( | ||
| // original_function.sig.clone(), | ||
| // "procedures must be `async`", | ||
| // )); | ||
| // }; | ||
|
|
||
| // Extract all function parameter names. | ||
| let opt_arg_names = typed_args.iter().map(|arg| { | ||
| if let syn::Pat::Ident(i) = &*arg.pat { | ||
| let name = i.ident.to_string(); | ||
| quote!(Some(#name)) | ||
| } else { | ||
| quote!(None) | ||
| } | ||
| }); | ||
|
|
||
| let arg_tys = typed_args.iter().map(|arg| arg.ty.as_ref()).collect::<Vec<_>>(); | ||
| let first_arg_ty = arg_tys.first().into_iter(); | ||
| let rest_arg_tys = arg_tys.iter().skip(1); | ||
|
|
||
| // Extract the return type. | ||
| let ret_ty_for_assert = match &original_function.sig.output { | ||
| syn::ReturnType::Default => None, | ||
| syn::ReturnType::Type(_, t) => Some(&**t), | ||
| } | ||
| .into_iter(); | ||
|
|
||
| let ret_ty_for_info = match &original_function.sig.output { | ||
| syn::ReturnType::Default => quote!(()), | ||
| syn::ReturnType::Type(_, t) => quote!(#t), | ||
| }; | ||
|
|
||
| let register_describer_symbol = format!("__preinit__20_register_describer_{}", procedure_name.value()); | ||
|
|
||
| let lifetime_params = &original_function.sig.generics; | ||
| let lifetime_where_clause = &lifetime_params.where_clause; | ||
|
|
||
| let generated_describe_function = quote! { | ||
| #[export_name = #register_describer_symbol] | ||
| pub extern "C" fn __register_describer() { | ||
| spacetimedb::rt::register_procedure::<_, _, #func_name>(#func_name) | ||
| } | ||
| }; | ||
|
|
||
| Ok(quote! { | ||
| const _: () = { | ||
| #generated_describe_function | ||
| }; | ||
| #[allow(non_camel_case_types)] | ||
| #vis struct #func_name { _never: ::core::convert::Infallible } | ||
| const _: () = { | ||
| fn _assert_args #lifetime_params () #lifetime_where_clause { | ||
| #(let _ = <#first_arg_ty as spacetimedb::rt::ProcedureContextArg>::_ITEM;)* | ||
| #(let _ = <#rest_arg_tys as spacetimedb::rt::ProcedureArg>::_ITEM;)* | ||
| #(let _ = <#ret_ty_for_assert as spacetimedb::rt::IntoProcedureResult>::to_result;)* | ||
| } | ||
| }; | ||
| impl #func_name { | ||
| fn invoke(__ctx: spacetimedb::ProcedureContext, __args: &[u8]) -> spacetimedb::ProcedureResult { | ||
| spacetimedb::rt::invoke_procedure(#func_name, __ctx, __args) | ||
| } | ||
| } | ||
| #[automatically_derived] | ||
| impl spacetimedb::rt::FnInfo for #func_name { | ||
| /// The type of this function. | ||
| type Invoke = spacetimedb::rt::ProcedureFn; | ||
|
|
||
| /// The function kind, which will cause scheduled tables to accept procedures. | ||
| type FnKind = spacetimedb::rt::FnKindProcedure<#ret_ty_for_info>; | ||
|
|
||
| /// The name of this function | ||
| const NAME: &'static str = #procedure_name; | ||
|
|
||
| /// The parameter names of this function | ||
| const ARG_NAMES: &'static [Option<&'static str>] = &[#(#opt_arg_names),*]; | ||
|
|
||
| /// The pointer for invoking this function | ||
| const INVOKE: spacetimedb::rt::ProcedureFn = #func_name::invoke; | ||
|
|
||
| /// The return type of this function | ||
| fn return_type(ts: &mut impl spacetimedb::sats::typespace::TypespaceBuilder) -> Option<spacetimedb::sats::AlgebraicType> { | ||
| Some(<#ret_ty_for_info as spacetimedb::SpacetimeType>::make_type(ts)) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.