Skip to content

Commit e4098f9

Browse files
Shubham8287rekhoffclockwork-labs-botclockwork-labs-bot
authored
Rust: macro change name -> accessor (#4264)
## Description of Changes This PR primarily affects the `bindings-macro` and `schema` crates to review: ### Core changes 1. Replaces the `name` macro with `accessor` for **Tables, Views, Procedures, and Reducers** in Rust modules. 2. Extends `RawModuleDefV10` with a new section for: * case conversion policies * explicit names New sections are not validated in this PR so not functional. 3. Updates index behavior: * Index names are now always **system-generated** for clients. Which will be fixed in follow-up PR when we start validating RawModuleDef with explicit names. * The `accessor` name for an index is used only inside the module. ## Breaking changes (API/ABI) 1. **Rust modules** * The `name` macro must be replaced with `accessor`. 2. **Client bindings (all languages)** * Index names are now system-generated instead of using explicitly provided names. **Complexity:** 3 A follow-up PR will reintroduce explicit names with support for case conversion. --------- Co-authored-by: rekhoff <r.ekhoff@clockworklabs.io> Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com> Co-authored-by: clockwork-labs-bot <bot@clockworklabs.com>
1 parent 83851fe commit e4098f9

177 files changed

Lines changed: 1139 additions & 829 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/bindings-csharp/Runtime/Internal/Autogen/CaseConversionPolicy.g.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-macro/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod sym {
3939
};
4040
}
4141

42+
symbol!(accessor);
4243
symbol!(at);
4344
symbol!(auto_inc);
4445
symbol!(btree);

crates/bindings-macro/src/procedure.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::reducer::{assert_only_lifetime_generics, extract_typed_args};
1+
use crate::reducer::{assert_only_lifetime_generics, extract_typed_args, generate_explicit_names_impl};
22
use crate::sym;
33
use crate::util::{check_duplicate, ident_to_litstr, match_meta};
44
use proc_macro2::TokenStream;
@@ -29,11 +29,12 @@ impl ProcedureArgs {
2929
}
3030
}
3131

32-
pub(crate) fn procedure_impl(args: ProcedureArgs, original_function: &ItemFn) -> syn::Result<TokenStream> {
32+
pub(crate) fn procedure_impl(_args: ProcedureArgs, original_function: &ItemFn) -> syn::Result<TokenStream> {
3333
let func_name = &original_function.sig.ident;
3434
let vis = &original_function.vis;
35+
let explicit_name = _args.name.as_ref();
3536

36-
let procedure_name = args.name.unwrap_or_else(|| ident_to_litstr(func_name));
37+
let procedure_name = ident_to_litstr(func_name);
3738

3839
assert_only_lifetime_generics(original_function, "procedures")?;
3940

@@ -86,6 +87,8 @@ pub(crate) fn procedure_impl(args: ProcedureArgs, original_function: &ItemFn) ->
8687
}
8788
};
8889

90+
let generate_explicit_names = generate_explicit_names_impl(&procedure_name.value(), func_name, explicit_name);
91+
8992
Ok(quote! {
9093
const _: () = {
9194
#generated_describe_function
@@ -126,5 +129,7 @@ pub(crate) fn procedure_impl(args: ProcedureArgs, original_function: &ItemFn) ->
126129
Some(<#ret_ty_for_info as spacetimedb::SpacetimeType>::make_type(ts))
127130
}
128131
}
132+
133+
#generate_explicit_names
129134
})
130135
}

crates/bindings-macro/src/reducer.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,18 @@ pub(crate) fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn
9696
let func_name = &original_function.sig.ident;
9797
let vis = &original_function.vis;
9898

99-
let reducer_name = args.name.unwrap_or_else(|| ident_to_litstr(func_name));
99+
let reducer_name = ident_to_litstr(func_name);
100100

101101
assert_only_lifetime_generics(original_function, "reducers")?;
102102

103103
let lifecycle = args.lifecycle.iter().filter_map(|lc| lc.to_lifecycle_value());
104104

105105
let typed_args = extract_typed_args(original_function)?;
106106

107+
let explicit_name = args.name.as_ref();
108+
109+
let generate_explicit_names = generate_explicit_names_impl(&reducer_name.value(), func_name, explicit_name);
110+
107111
// Extract all function parameter names.
108112
let opt_arg_names = typed_args.iter().map(|arg| {
109113
if let syn::Pat::Ident(i) = &*arg.pat {
@@ -165,5 +169,36 @@ pub(crate) fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn
165169
const ARG_NAMES: &'static [Option<&'static str>] = &[#(#opt_arg_names),*];
166170
const INVOKE: Self::Invoke = #func_name::invoke;
167171
}
172+
173+
#generate_explicit_names
168174
})
169175
}
176+
177+
pub(crate) fn generate_explicit_names_impl(
178+
func_name: &str,
179+
func_handle: &Ident,
180+
explicit_name: Option<&LitStr>,
181+
) -> TokenStream {
182+
let mut explicit_names_body = Vec::new();
183+
184+
// Table name
185+
if let Some(explicit_name) = explicit_name {
186+
explicit_names_body.push(quote! {
187+
names.insert_function(
188+
#func_name,
189+
#explicit_name,
190+
);
191+
});
192+
};
193+
194+
quote! {
195+
196+
impl spacetimedb::rt::ExplicitNames for #func_handle {
197+
fn explicit_names() -> spacetimedb::spacetimedb_lib::ExplicitNames {
198+
let mut names = spacetimedb::spacetimedb_lib::ExplicitNames::default();
199+
#(#explicit_names_body)*
200+
names
201+
}
202+
}
203+
}
204+
}

0 commit comments

Comments
 (0)