Skip to content

Commit 4d27030

Browse files
committed
[release/v1.4.0]: Revert "rust: default macro (#3177)"
This reverts commit cfb185a.
1 parent c3845fd commit 4d27030

5 files changed

Lines changed: 8 additions & 95 deletions

File tree

crates/bindings-macro/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ mod sym {
5757
symbol!(scheduled);
5858
symbol!(unique);
5959
symbol!(update);
60-
symbol!(default);
6160

6261
symbol!(u8);
6362
symbol!(i8);
@@ -168,7 +167,7 @@ pub fn table(args: StdTokenStream, item: StdTokenStream) -> StdTokenStream {
168167
///
169168
/// Provides helper attributes for `#[spacetimedb::table]`, so that we don't get unknown attribute errors.
170169
#[doc(hidden)]
171-
#[proc_macro_derive(__TableHelper, attributes(sats, unique, auto_inc, primary_key, index, default))]
170+
#[proc_macro_derive(__TableHelper, attributes(sats, unique, auto_inc, primary_key, index))]
172171
pub fn table_helper(input: StdTokenStream) -> StdTokenStream {
173172
schema_type(input)
174173
}

crates/bindings-macro/src/table.rs

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,12 @@ fn superize_vis(vis: &syn::Visibility) -> Cow<'_, syn::Visibility> {
451451
}
452452
}
453453

454-
#[derive(Clone)]
454+
#[derive(Copy, Clone)]
455455
struct Column<'a> {
456456
index: u16,
457457
vis: &'a syn::Visibility,
458458
ident: &'a syn::Ident,
459459
ty: &'a syn::Type,
460-
default_value: Option<syn::Expr>,
461460
}
462461

463462
fn try_find_column<'a, 'b, T: ?Sized>(cols: &'a [Column<'b>], name: &T) -> Option<&'a Column<'b>>
@@ -476,7 +475,6 @@ enum ColumnAttr {
476475
AutoInc(Span),
477476
PrimaryKey(Span),
478477
Index(IndexArg),
479-
Default(syn::Expr, Span),
480478
}
481479

482480
impl ColumnAttr {
@@ -496,25 +494,12 @@ impl ColumnAttr {
496494
} else if ident == sym::primary_key {
497495
attr.meta.require_path_only()?;
498496
Some(ColumnAttr::PrimaryKey(ident.span()))
499-
} else if ident == sym::default {
500-
Some(parse_default_attr(attr, ident)?)
501497
} else {
502498
None
503499
})
504500
}
505501
}
506502

507-
fn parse_default_attr(attr: &syn::Attribute, ident: &Ident) -> syn::Result<ColumnAttr> {
508-
if let Ok(expr) = attr.parse_args::<syn::Expr>() {
509-
return Ok(ColumnAttr::Default(expr, ident.span()));
510-
}
511-
512-
Err(syn::Error::new_spanned(
513-
&attr.meta,
514-
"expected default value in format `#[default(CONSTANT_VALUE)]`",
515-
))
516-
}
517-
518503
pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::Result<TokenStream> {
519504
let vis = &item.vis;
520505
let sats_ty = sats::sats_type_from_derive(item, quote!(spacetimedb::spacetimedb_lib))?;
@@ -563,7 +548,6 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
563548
let mut unique = None;
564549
let mut auto_inc = None;
565550
let mut primary_key = None;
566-
let mut default_value = None;
567551
for attr in field.original_attrs {
568552
let Some(attr) = ColumnAttr::parse(attr, field_ident)? else {
569553
continue;
@@ -582,42 +566,28 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
582566
primary_key = Some(span);
583567
}
584568
ColumnAttr::Index(index_arg) => args.indices.push(index_arg),
585-
ColumnAttr::Default(expr, span) => {
586-
check_duplicate(&default_value, span)?;
587-
default_value = Some(expr);
588-
}
589569
}
590570
}
591571

592-
if let Some(default_value) = &default_value {
593-
if auto_inc.is_some() || primary_key.is_some() || unique.is_some() {
594-
return Err(syn::Error::new(
595-
default_value.span(),
596-
"invalid combination: auto_inc, unique index or primary key cannot have a default value",
597-
));
598-
};
599-
}
600-
601572
let column = Column {
602573
index: col_num,
603574
ident: field_ident,
604575
vis: field.vis,
605576
ty: field.ty,
606-
default_value,
607577
};
608578

609579
if unique.is_some() || primary_key.is_some() {
610-
unique_columns.push(column.clone());
580+
unique_columns.push(column);
611581
}
612582
if auto_inc.is_some() {
613-
sequenced_columns.push(column.clone());
583+
sequenced_columns.push(column);
614584
}
615585
if let Some(span) = primary_key {
616586
check_duplicate_msg(&primary_key_column, span, "can only have one primary key per table")?;
617-
primary_key_column = Some(column.clone());
587+
primary_key_column = Some(column);
618588
}
619589

620-
columns.push(column.clone());
590+
columns.push(column);
621591
}
622592

623593
let row_type = quote!(#original_struct_ident);
@@ -677,45 +647,9 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
677647

678648
let table_access = args.access.iter().map(|acc| acc.to_value());
679649
let unique_col_ids = unique_columns.iter().map(|col| col.index);
680-
let primary_col_id = primary_key_column.clone().into_iter().map(|col| col.index);
650+
let primary_col_id = primary_key_column.iter().map(|col| col.index);
681651
let sequence_col_ids = sequenced_columns.iter().map(|col| col.index);
682652

683-
let default_type_check: TokenStream = columns
684-
.iter()
685-
.filter_map(|col| {
686-
if let Some(val) = &col.default_value {
687-
let ty = &col.ty;
688-
let ident_span = col.ident.span();
689-
Some(quote_spanned! { ident_span =>
690-
// This closure enforces that `val` is of type `ty` at compile-time.
691-
let _check: #ty = #val;
692-
})
693-
} else {
694-
None
695-
}
696-
})
697-
.collect();
698-
699-
let col_defaults: Vec<TokenStream> = columns.iter().filter_map(|col| {
700-
if let Some(val) = &col.default_value {
701-
let col_id = col.index;
702-
Some(quote! {
703-
spacetimedb::table::ColumnDefault {
704-
col_id: #col_id,
705-
value: #val.serialize(spacetimedb::sats::algebraic_value::ser::ValueSerializer).expect("default value serialization failed"),
706-
}
707-
})
708-
} else {
709-
None
710-
}
711-
}).collect();
712-
713-
let default_fn: TokenStream = quote! {
714-
fn get_default_col_values() -> Vec<spacetimedb::table::ColumnDefault> {
715-
[#(#col_defaults)*].to_vec()
716-
}
717-
};
718-
719653
let (schedule, schedule_typecheck) = args
720654
.scheduled
721655
.as_ref()
@@ -785,7 +719,6 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
785719
let field_types = fields.iter().map(|f| f.ty).collect::<Vec<_>>();
786720

787721
let tabletype_impl = quote! {
788-
use spacetimedb::Serialize;
789722
impl spacetimedb::Table for #tablehandle_ident {
790723
type Row = #row_type;
791724

@@ -805,7 +738,6 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
805738
#(const SCHEDULE: Option<spacetimedb::table::ScheduleDesc<'static>> = Some(#schedule);)*
806739

807740
#table_id_from_name_func
808-
#default_fn
809741
}
810742
};
811743

@@ -841,7 +773,6 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
841773
const _: () = {
842774
#(let _ = <#field_types as spacetimedb::rt::TableColumn>::_ITEM;)*
843775
#schedule_typecheck
844-
#default_type_check
845776
};
846777

847778
#trait_def

crates/bindings/src/rt.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,6 @@ pub fn register_table<T: Table>() {
342342
table = table.with_schedule(schedule.reducer_name, schedule.scheduled_at_column);
343343
}
344344

345-
for col in T::get_default_col_values().iter_mut() {
346-
table = table.with_default_column_value(col.col_id, col.value.clone())
347-
}
348-
349345
table.finish();
350346
})
351347
}

crates/bindings/src/table.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ use core::borrow::Borrow;
33
use core::convert::Infallible;
44
use core::fmt;
55
use core::marker::PhantomData;
6+
use spacetimedb_lib::buffer::{BufReader, Cursor, DecodeError};
67
pub use spacetimedb_lib::db::raw_def::v9::TableAccess;
7-
use spacetimedb_lib::{
8-
buffer::{BufReader, Cursor, DecodeError},
9-
AlgebraicValue,
10-
};
118
use spacetimedb_lib::{FilterableValue, IndexScanRangeBoundsTerminator};
129
pub use spacetimedb_primitives::{ColId, IndexId};
1310

@@ -131,8 +128,6 @@ pub trait TableInternal: Sized {
131128

132129
/// Returns the ID of this table.
133130
fn table_id() -> TableId;
134-
135-
fn get_default_col_values() -> Vec<ColumnDefault>;
136131
}
137132

138133
/// Describe a named index with an index type over a set of columns identified by their IDs.
@@ -153,12 +148,6 @@ pub struct ScheduleDesc<'a> {
153148
pub scheduled_at_column: u16,
154149
}
155150

156-
#[derive(Debug, Clone)]
157-
pub struct ColumnDefault {
158-
pub col_id: u16,
159-
pub value: AlgebraicValue,
160-
}
161-
162151
/// A row operation was attempted that would violate a unique constraint.
163152
// TODO: add column name for better error message
164153
#[derive(Debug)]

modules/module-test/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ pub enum TestC {
4040
Bar,
4141
}
4242

43-
const DEFAULT_TEST_C: TestC = TestC::Foo;
4443
#[table(name = test_d, public)]
4544
pub struct TestD {
46-
#[default(Some(DEFAULT_TEST_C))]
4745
test_c: Option<TestC>,
4846
}
4947

0 commit comments

Comments
 (0)