SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Consider adding `#[must_use]` to `#[table]` rows in Rust modules

Open kazimuth opened this issue 10 months ago • 2 comments

Upsides

This would catch anywhere the user updates a row struct but forgets to commit it. This is likely to be particularly helpful to new users who don't understand the data model, but it would help experienced users as well.

E.g.

fn update_username(ctx: &ReducerContext, id: UID, name: String) {
   let mut user = ctx.db.user().id().find(id);
   user.name = name;
   // whoops! this does nothing!!
}

Downsides

You have to explicitly discard rows you don't need, e.g.

fn update_username(ctx: &ReducerContext, id: UID, name: String) {
   let mut user = ctx.db.user().id().find(id);
   user.name = name;
   // now you need this `let _ =` to avoid a warning, because the `update` method returns the updated row.
   let _ = ctx.db.user().id().update(user);
}

This makes using Row structs as plain-old-structs slightly less convenient. I still think it's worth it.

kazimuth avatar Feb 13 '25 21:02 kazimuth

unused_must_use is warn by default, right? Not a hard error?

gefjon avatar Feb 14 '25 16:02 gefjon

Assigning to dev ex team. They will prioritize as they see fit based on user reports (or lack thereof).

gefjon avatar Mar 12 '25 20:03 gefjon