Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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}`.
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
72 changes: 72 additions & 0 deletions crates/codegen/tests/snapshots/codegen__codegen_rust.snap
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,18 @@ pub struct LoggedOutPlayerTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `logged_out_player`.
pub struct LoggedOutPlayerTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for LoggedOutPlayerTableAccessor {
type Row = Player;
type Handle<'db> = LoggedOutPlayerTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `logged_out_player`.
///
Expand Down Expand Up @@ -2091,6 +2103,18 @@ pub struct MyPlayerTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `my_player`.
pub struct MyPlayerTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for MyPlayerTableAccessor {
type Row = Player;
type Handle<'db> = MyPlayerTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `my_player`.
///
Expand Down Expand Up @@ -2374,6 +2398,18 @@ pub struct PersonTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `person`.
pub struct PersonTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for PersonTableAccessor {
type Row = Person;
type Handle<'db> = PersonTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `person`.
///
Expand Down Expand Up @@ -2729,6 +2765,18 @@ pub struct PlayerTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `player`.
pub struct PlayerTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for PlayerTableAccessor {
type Row = Player;
type Handle<'db> = PlayerTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `player`.
///
Expand Down Expand Up @@ -3752,6 +3800,18 @@ pub struct TestDTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `test_d`.
pub struct TestDTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for TestDTableAccessor {
type Row = TestD;
type Handle<'db> = TestDTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `test_d`.
///
Expand Down Expand Up @@ -4039,6 +4099,18 @@ pub struct TestFTableHandle<'ctx> {
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}

/// Lifetime-aware accessor marker for the table `test_f`.
pub struct TestFTableAccessor;

impl __sdk::TableAccessor<super::RemoteTables> for TestFTableAccessor {
type Row = TestFoobar;
type Handle<'db> = TestFTableHandle<'db>;

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

#[allow(non_camel_case_types)]
/// Extension trait for access to the table `test_f`.
///
Expand Down
6 changes: 5 additions & 1 deletion crates/dst/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
[package]
name = "spacetimedb-dst"
name = "spacetimedb-dst-lib"
version.workspace = true
edition.workspace = true
rust-version.workspace = true

[features]
default = ["fallocate"]
fallocate = ["spacetimedb-commitlog/fallocate"]

[dependencies]
anyhow.workspace = true
clap.workspace = true
Expand Down
20 changes: 0 additions & 20 deletions crates/dst/README.md

This file was deleted.

5 changes: 3 additions & 2 deletions crates/dst/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ impl EngineTarget {
impl TargetDriver<Interaction> for EngineTarget {
type Observation = Observation;

fn execute(&mut self, interaction: &Interaction) -> Result<Self::Observation, anyhow::Error> {
async fn execute<'a>(&'a mut self, interaction: &'a Interaction) -> Result<Self::Observation, anyhow::Error> {
EngineTarget::execute(self, interaction)
}
}

pub struct EngineTest;

impl TestSuite for EngineTest {
Expand All @@ -289,7 +290,7 @@ impl TestSuite for EngineTest {

type Properties = EngineProperties;

fn build(&self, rng: Rng) -> Result<(Self::Interactions, Self::Target, Self::Properties), anyhow::Error> {
async fn build(&self, rng: Rng) -> Result<(Self::Interactions, Self::Target, Self::Properties), anyhow::Error> {
let schema = default_schema(rng.clone());
let runtime_seed = rng.next_u64();
let target = EngineTarget::init(schema.clone(), runtime_seed)?;
Expand Down
6 changes: 6 additions & 0 deletions crates/dst/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod engine;
pub mod schema;
pub mod sim;
pub mod traits;

pub use traits::{Properties, TargetDriver, TestSuite, TestSuiteParts};
91 changes: 0 additions & 91 deletions crates/dst/src/main.rs

This file was deleted.

10 changes: 10 additions & 0 deletions crates/dst/src/sim/commitlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub struct InMemoryCommitlog {
options: Options,
}

impl Default for InMemoryCommitlog {
fn default() -> Self {
Self::new()
}
}

impl InMemoryCommitlog {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -394,6 +400,10 @@ impl FileLike for Segment {

Ok(())
}

fn fallocate(&mut self, _size: u64) -> io::Result<()> {
Ok(())
}
}

pub struct ReadOnlySegment {
Expand Down
34 changes: 20 additions & 14 deletions crates/dst/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use spacetimedb_runtime::sim::Rng;
pub trait TargetDriver<I> {
type Observation;

fn execute(&mut self, interaction: &I) -> Result<Self::Observation, Error>;
fn execute<'a>(
&'a mut self,
interaction: &'a I,
) -> impl std::future::Future<Output = Result<Self::Observation, Error>> + 'a;
}

/// Ensures if Output of `TargetDrive` is expected for the input
Expand All @@ -20,32 +23,35 @@ pub type TestSuiteParts<S> = (
);

pub trait TestSuite {
type Interaction;
type Interaction: std::fmt::Debug;
type Interactions: Iterator<Item = Self::Interaction> + std::fmt::Debug;
type Target: TargetDriver<Self::Interaction>;
type Properties: Properties<Self::Interaction, <Self::Target as TargetDriver<Self::Interaction>>::Observation>;

fn build(&self, rng: Rng) -> Result<TestSuiteParts<Self>, Error>
fn build(&self, rng: Rng) -> impl std::future::Future<Output = Result<TestSuiteParts<Self>, Error>> + '_
where
Self: Sized;

fn run(&self, rng: Rng, max_interactions: Option<usize>) -> Result<(), Error>
fn run(&self, rng: Rng, max_interactions: usize) -> impl std::future::Future<Output = Result<(), Error>> + '_
where
Self: Sized,
{
let (mut interactions, mut target, mut properties) = self.build(rng)?;
async move {
let (mut interactions, mut target, mut properties) = self.build(rng).await?;

let result = (|| {
for interaction in interactions.by_ref().take(max_interactions.unwrap_or(usize::MAX)) {
let observation = target.execute(&interaction)?;
properties.observe(&interaction, &observation)?;
}
let result = async {
for interaction in interactions.by_ref().take(max_interactions) {
let observation = target.execute(&interaction).await?;
properties.observe(&interaction, &observation)?;
}

Ok(())
})();
Ok(())
}
.await;

tracing::info!(interaction_counts = ?interactions, "final interaction counts");
tracing::info!(interaction_counts = ?interactions, "final interaction counts");

result
result
}
}
}
Loading
Loading