-
Couldn't load subscription status.
- Fork 645
Add macro bindings for views #3429
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
9 commits
Select commit
Hold shift + click to select a range
4bfb4c5
Add macro bindings for views
joshua-spacetime b52a38a
add smoketests for st_view_* tables
joshua-spacetime 86170a9
more tests
joshua-spacetime e51cdd0
more docs
joshua-spacetime c174413
more tests
joshua-spacetime 9ad7873
compile test for scheduled functions
joshua-spacetime 2ec207d
update doc comments
joshua-spacetime 2f13fc2
Merge branch 'master' into joshua/view-macro-bindings
joshua-spacetime 0a79c52
fix doctest
joshua-spacetime 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
Some comments aren't visible on the classic Files Changed page.
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
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,178 @@ | ||
| use proc_macro2::{Span, TokenStream}; | ||
| use quote::quote; | ||
| use syn::parse::Parser; | ||
| use syn::{FnArg, ItemFn}; | ||
|
|
||
| use crate::sym; | ||
| use crate::util::{ident_to_litstr, match_meta}; | ||
|
|
||
| pub(crate) struct ViewArgs { | ||
| #[allow(unused)] | ||
| public: bool, | ||
| } | ||
|
|
||
| impl ViewArgs { | ||
| /// Parse `#[view(public)]` where `public` is required. | ||
| pub(crate) fn parse(input: TokenStream) -> syn::Result<Self> { | ||
| if input.is_empty() { | ||
| return Err(syn::Error::new( | ||
| Span::call_site(), | ||
| "views must be declared as `#[view(public)]`; `public` is required", | ||
| )); | ||
| } | ||
| let mut public = false; | ||
| syn::meta::parser(|meta| { | ||
| match_meta!(match meta { | ||
| sym::public => { | ||
| if public { | ||
| return Err(syn::Error::new( | ||
| Span::call_site(), | ||
| "duplicate attribute argument: `public`", | ||
| )); | ||
| } | ||
| public = true; | ||
| } | ||
| }); | ||
| Ok(()) | ||
| }) | ||
| .parse2(input)?; | ||
joshua-spacetime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !public { | ||
| return Err(syn::Error::new( | ||
| Span::call_site(), | ||
| "views must be declared as `#[view(public)]`; `public` is required", | ||
| )); | ||
| } | ||
| Ok(Self { public }) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn view_impl(_args: ViewArgs, original_function: &ItemFn) -> syn::Result<TokenStream> { | ||
| let func_name = &original_function.sig.ident; | ||
| let view_name = ident_to_litstr(func_name); | ||
| let vis = &original_function.vis; | ||
|
|
||
| for param in &original_function.sig.generics.params { | ||
| let err = |msg| syn::Error::new_spanned(param, msg); | ||
| match param { | ||
| syn::GenericParam::Lifetime(_) => {} | ||
| syn::GenericParam::Type(_) => return Err(err("type parameters are not allowed on views")), | ||
| syn::GenericParam::Const(_) => return Err(err("const parameters are not allowed on views")), | ||
| } | ||
| } | ||
|
|
||
| // Extract parameters | ||
| let typed_args = original_function | ||
| .sig | ||
| .inputs | ||
| .iter() | ||
| .map(|arg| match arg { | ||
| FnArg::Typed(arg) => Ok(arg), | ||
| FnArg::Receiver(_) => Err(syn::Error::new_spanned( | ||
| arg, | ||
| "The `self` parameter is not allowed in views", | ||
| )), | ||
| }) | ||
| .collect::<syn::Result<Vec<_>>>()?; | ||
|
|
||
| // Extract 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<_>>(); | ||
|
|
||
| // Extract the context type and the rest of the parameter types | ||
| let [ctx_ty, arg_tys @ ..] = &arg_tys[..] else { | ||
| return Err(syn::Error::new_spanned( | ||
| &original_function.sig, | ||
| "Views must always have a context parameter: `&ViewContext` or `&AnonymousViewContext`", | ||
| )); | ||
| }; | ||
|
|
||
| // Extract the context type | ||
| let ctx_ty = match ctx_ty { | ||
| syn::Type::Reference(ctx_ty) => ctx_ty.elem.as_ref(), | ||
| _ => { | ||
| return Err(syn::Error::new_spanned( | ||
| ctx_ty, | ||
| "The first parameter of a view must be a context parameter: `&ViewContext` or `&AnonymousViewContext`; passed by reference", | ||
| )); | ||
| } | ||
| }; | ||
|
|
||
| // Views must return a result | ||
| let ret_ty = match &original_function.sig.output { | ||
| syn::ReturnType::Type(_, t) => t.as_ref(), | ||
| syn::ReturnType::Default => { | ||
| return Err(syn::Error::new_spanned( | ||
| &original_function.sig, | ||
| "views must return `Vec<T>` or `Option<T>` where `T` is a `SpacetimeType`", | ||
| )); | ||
| } | ||
| }; | ||
|
|
||
| let register_describer_symbol = format!("__preinit__20_register_describer_{}", view_name.value()); | ||
|
|
||
| let lt_params = &original_function.sig.generics; | ||
| let lt_where_clause = <_params.where_clause; | ||
|
|
||
| let generated_describe_function = quote! { | ||
| #[export_name = #register_describer_symbol] | ||
| pub extern "C" fn __register_describer() { | ||
| spacetimedb::rt::ViewRegistrar::<#ctx_ty>::register::<_, #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 #lt_params () #lt_where_clause { | ||
| let _ = <#ctx_ty as spacetimedb::rt::ViewContextArg>::_ITEM; | ||
| let _ = <#ret_ty as spacetimedb::rt::ViewReturn>::_ITEM; | ||
| } | ||
| }; | ||
|
|
||
| const _: () = { | ||
| fn _assert_args #lt_params () #lt_where_clause { | ||
| #(let _ = <#arg_tys as spacetimedb::rt::ViewArg>::_ITEM;)* | ||
| } | ||
| }; | ||
|
|
||
| impl #func_name { | ||
| fn invoke(__ctx: #ctx_ty, __args: &[u8]) -> Vec<u8> { | ||
| spacetimedb::rt::ViewDispatcher::<#ctx_ty>::invoke::<_, _, _>(#func_name, __ctx, __args) | ||
| } | ||
| } | ||
|
|
||
| #[automatically_derived] | ||
| impl spacetimedb::rt::FnInfo for #func_name { | ||
| /// The type of this function | ||
| type Invoke = <spacetimedb::rt::ViewKind<#ctx_ty> as spacetimedb::rt::ViewKindTrait>::InvokeFn; | ||
|
|
||
| /// The name of this function | ||
| const NAME: &'static str = #view_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: Self::Invoke = #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 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
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.