Skip to content

Commit 879ee2c

Browse files
committed
Implemented type-safe primary keys.
1 parent 0425895 commit 879ee2c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Cargo.lock

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

crates/bindings-macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ proc-macro2.workspace = true
1919
quote.workspace = true
2020
syn.workspace = true
2121
heck.workspace = true
22+
proc-macro-type-name = "0.1.0"
2223

2324
[lints]
2425
workspace = true

crates/bindings-macro/src/table.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::sym;
33
use crate::util::{check_duplicate, check_duplicate_msg, ident_to_litstr, match_meta};
44
use core::slice;
55
use heck::ToSnakeCase;
6+
use proc_macro_type_name::ToTypeName;
67
use proc_macro2::{Span, TokenStream};
78
use quote::{format_ident, quote, quote_spanned, ToTokens};
89
use std::borrow::Cow;
@@ -784,6 +785,9 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
784785
}
785786
};
786787

788+
let pk_type = primary_key_column.clone().map(|x| x.ty);
789+
let pk_type_ident = primary_key_column.clone().map(|x| Ident::new(&(original_struct_ident.to_string() + &(&x.ident).to_type_ident(x.ident.span()).to_string()), x.ident.span()));
790+
787791
let (schedule, schedule_typecheck) = args
788792
.scheduled
789793
.as_ref()
@@ -887,6 +891,18 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
887891
};
888892

889893
// Output all macro data
894+
895+
let trait_def_pk = pk_type.map(|pk_type| quote_spanned! {table_ident.span()=>
896+
#[allow(dead_code)]
897+
#[derive(SpacetimeType)]
898+
struct #pk_type_ident(#pk_type);
899+
impl std::borrow::Borrow<#pk_type> for #pk_type_ident {
900+
fn borrow(&self) -> &#pk_type {
901+
&self.0
902+
}
903+
}
904+
});
905+
890906
let trait_def = quote_spanned! {table_ident.span()=>
891907
#[allow(non_camel_case_types, dead_code)]
892908
#vis trait #table_ident {
@@ -931,6 +947,7 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
931947
#default_type_check
932948
};
933949

950+
#trait_def_pk
934951
#trait_def
935952
#trait_def_view
936953

0 commit comments

Comments
 (0)