Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/codegen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl __sdk::InModule for {type_name} {{
let table_name = table.name.deref();
let table_name_pascalcase = table.accessor_name.deref().to_case(Case::Pascal);
let table_handle = table_name_pascalcase.clone() + "TableHandle";
let table_accessor = table_name_pascalcase.clone() + "TableAccessor";
let insert_callback_id = table_name_pascalcase.clone() + "InsertCallbackId";
let delete_callback_id = table_name_pascalcase.clone() + "DeleteCallbackId";
let accessor_trait = table_access_trait_name(&table.accessor_name);
Expand All @@ -148,6 +149,18 @@ pub struct {table_handle}<'ctx> {{
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}}

/// Lifetime-aware accessor marker for the table `{table_name}`.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why there are lifetimes on the currently generated code, it doesn't seem like they are necessary 🤔 If someone could give me some more info on that I'd appreciate it!

Nevertheless, I didn't want to introduce breaking changes so this is simply additive. If lifetimes aren't needed in the code, then I'd like to see them eventually removed in the next breaking release.

pub struct {table_accessor};

impl __sdk::TableAccessor<super::RemoteTables> for {table_accessor} {{
type Row = {row_type};
type Handle<'db> = {table_handle}<'db>;

fn get<'db>(db: &'db super::RemoteTables) -> Self::Handle<'db> {{
db.{accessor_method}()
}}
}}

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `{table_name}`.
///
Expand Down
4 changes: 2 additions & 2 deletions sdks/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use db_connection::DbConnectionBuilder;
pub use db_context::DbContext;
pub use error::{Error, Result};
pub use event::{Event, ReducerEvent, Status};
pub use table::{EventTable, Table, TableWithPrimaryKey};
pub use table::{EventTable, Table, TableAccessor, TableWithPrimaryKey};

pub use spacetime_module::SubscriptionHandle;
pub use spacetimedb_client_api_messages::websocket::v1::Compression;
Expand Down Expand Up @@ -62,7 +62,7 @@ pub mod __codegen {
pub use crate::subscription::{OnEndedCallback, SubscriptionBuilder, SubscriptionHandleImpl};
pub use crate::{
ConnectionId, DbConnectionBuilder, DbContext, Event, EventTable, Identity, ReducerEvent, ScheduleAt, Table,
TableWithPrimaryKey, TimeDuration, Timestamp, Uuid,
TableAccessor, TableWithPrimaryKey, TimeDuration, Timestamp, Uuid,
};
}

Expand Down
17 changes: 17 additions & 0 deletions sdks/rust/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
//! which mediate access to tables in the client cache.
//! Obtain a table handle by calling a method on `ctx.db`, where `ctx` is a `DbConnection` or `EventContext`.

/// Accesses a generated table handle from a database view.
///
/// This trait is implemented by generated marker types and preserves the
/// lifetime relationship between a database view and its table handles.
pub trait TableAccessor<DbView: ?Sized> {
/// The type of rows stored in this table.
type Row: 'static;

/// The generated table handle type for a database view borrow.
type Handle<'db>
where
DbView: 'db;

/// Returns the generated table handle for `db`.
fn get<'db>(db: &'db DbView) -> Self::Handle<'db>;
}

/// Trait implemented by table handles, which mediate access to tables in the client cache.
///
/// Obtain a table handle by calling a method on `ctx.db`, where `ctx` is a `DbConnection` or `EventContext`.
Expand Down