diff --git a/crates/bindings/src/http.rs b/crates/bindings/src/http.rs index ec476bba46e..7cf60a5af39 100644 --- a/crates/bindings/src/http.rs +++ b/crates/bindings/src/http.rs @@ -13,7 +13,7 @@ use crate::StdbRng; #[cfg(feature = "unstable")] use crate::{try_with_tx, with_tx, Timestamp, TxContext}; use bytes::Bytes; -#[cfg(all(feature = "rand", feature = "unstable"))] +#[cfg(all(feature = "rand08", feature = "unstable"))] use rand08::RngCore; #[cfg(feature = "unstable")] use spacetimedb_lib::db::raw_def::v10::MethodOrAny; @@ -22,10 +22,10 @@ use spacetimedb_lib::http as st_http; use spacetimedb_lib::http::{character_is_acceptable_for_route_path, ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION}; #[cfg(feature = "unstable")] use spacetimedb_lib::Identity; -#[cfg(all(feature = "unstable", feature = "rand"))] +#[cfg(all(feature = "unstable", feature = "rand08"))] use spacetimedb_lib::Uuid; use spacetimedb_lib::{bsatn, TimeDuration}; -#[cfg(all(feature = "unstable", feature = "rand"))] +#[cfg(all(feature = "unstable", feature = "rand08"))] use std::cell::Cell; #[cfg(all(feature = "unstable", feature = "rand08"))] use std::cell::OnceCell; @@ -72,7 +72,7 @@ pub use spacetimedb_bindings_macro::http_handler as handler; /// Register a [`Router`](struct@Router) to route HTTP requests to handlers. /// -/// This should annotate a function of no arguments which returns a [`Router`](struct@router). +/// This should annotate a function of no arguments which returns a [`Router`](struct@Router). /// /// ```no_run /// # use spacetimedb::http::{handler, router, Request, Response, Body, HandlerContext, Router}; @@ -109,7 +109,7 @@ pub struct HandlerContext { /// A counter used for generating UUIDv7 values. /// **Note:** must be 0..=u32::MAX - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub(crate) counter_uuid: Cell, } @@ -121,7 +121,7 @@ impl HandlerContext { http: HttpClient {}, #[cfg(feature = "rand08")] rng: OnceCell::new(), - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell::new(0), } } @@ -142,7 +142,7 @@ impl HandlerContext { } /// Create a new random [`Uuid`] `v4` using the built-in RNG. - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v4(&self) -> anyhow::Result { let mut bytes = [0u8; 16]; self.rng().try_fill_bytes(&mut bytes)?; @@ -150,7 +150,7 @@ impl HandlerContext { } /// Create a new sortable [`Uuid`] `v7` using the built-in RNG, counter and timestamp. - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v7(&self) -> anyhow::Result { let mut random_bytes = [0u8; 4]; self.rng().try_fill_bytes(&mut random_bytes)?; diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 62b78e14be4..400d2d197d9 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1,9 +1,11 @@ #![doc = include_str!("../README.md")] // ^ if you are working on docs, go read the top comment of README.md please. -use core::cell::{Cell, LazyCell, OnceCell, RefCell}; +use core::cell::{LazyCell, OnceCell, RefCell}; use core::ops::Deref; use spacetimedb_lib::bsatn; +#[cfg(feature = "rand08")] +use std::cell::Cell; use std::rc::Rc; #[cfg(feature = "unstable")] @@ -24,9 +26,11 @@ pub use spacetimedb_query_builder as query_builder; #[cfg(feature = "unstable")] pub use client_visibility_filter::Filter; pub use log; -#[cfg(feature = "rand")] +#[cfg(feature = "rand08")] +use rand::distributions::{Distribution, Standard}; +#[cfg(feature = "rand08")] pub use rand08 as rand; -#[cfg(feature = "rand")] +#[cfg(feature = "rand08")] use rand08::RngCore; #[cfg(feature = "rand08")] pub use rng::StdbRng; @@ -42,6 +46,9 @@ pub use spacetimedb_lib::ser::Serialize; pub use spacetimedb_lib::AlgebraicValue; pub use spacetimedb_lib::ConnectionId; // `FilterableValue` re-exported purely for rustdoc. +#[cfg(feature = "unstable")] +use crate::http::HandlerContext; +use crate::http::HttpClient; pub use spacetimedb_lib::FilterableValue; pub use spacetimedb_lib::Identity; pub use spacetimedb_lib::ScheduleAt; @@ -1027,7 +1034,7 @@ pub struct ReducerContext { rng: std::cell::OnceCell, /// A counter used for generating UUIDv7 values. /// **Note:** must be 0..=u32::MAX - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell, } @@ -1042,7 +1049,7 @@ impl ReducerContext { sender_auth: AuthCtx::internal(), #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell::new(0), } } @@ -1057,7 +1064,7 @@ impl ReducerContext { sender_auth: AuthCtx::from_connection_id_opt(connection_id), #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell::new(0), } } @@ -1123,7 +1130,7 @@ impl ReducerContext { /// } /// # } /// ``` - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v4(&self) -> anyhow::Result { let mut bytes = [0u8; 16]; self.rng().try_fill_bytes(&mut bytes)?; @@ -1145,7 +1152,7 @@ impl ReducerContext { /// } /// # } /// ``` - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v7(&self) -> anyhow::Result { let mut random_bytes = [0u8; 4]; self.rng().try_fill_bytes(&mut random_bytes)?; @@ -1262,7 +1269,7 @@ pub struct ProcedureContext { /// A counter used for generating UUIDv7 values. /// **Note:** must be 0..=u32::MAX // Disabled when compiling without `rand`, as both v4 and v7 UUIDs have random components. - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell, } @@ -1275,7 +1282,7 @@ impl ProcedureContext { http: http::HttpClient {}, #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] counter_uuid: Cell::new(0), } } @@ -1409,7 +1416,7 @@ impl ProcedureContext { /// } /// # } /// ``` - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v4(&self) -> anyhow::Result { let mut bytes = [0u8; 16]; self.rng().try_fill_bytes(&mut bytes)?; @@ -1431,7 +1438,7 @@ impl ProcedureContext { /// } /// # } /// ``` - #[cfg(feature = "rand")] + #[cfg(feature = "rand08")] pub fn new_uuid_v7(&self) -> anyhow::Result { let mut random_bytes = [0u8; 4]; self.rng().try_fill_bytes(&mut random_bytes)?; @@ -1440,6 +1447,7 @@ impl ProcedureContext { } /// A handle on a database with a particular table schema. +#[deprecated(note = "Use the capability based traits (CtxDbRead, CtxDbWrite) instead!")] pub trait DbContext { /// A view into the tables of a database. /// @@ -1461,6 +1469,7 @@ pub trait DbContext { fn db_read_only(&self) -> &LocalReadOnly; } +#[allow(deprecated)] impl DbContext for AnonymousViewContext { type DbView = LocalReadOnly; @@ -1473,6 +1482,7 @@ impl DbContext for AnonymousViewContext { } } +#[allow(deprecated)] impl DbContext for ReducerContext { type DbView = Local; @@ -1485,6 +1495,7 @@ impl DbContext for ReducerContext { } } +#[allow(deprecated)] impl DbContext for TxContext { type DbView = Local; @@ -1497,6 +1508,7 @@ impl DbContext for TxContext { } } +#[allow(deprecated)] impl DbContext for ViewContext { type DbView = LocalReadOnly; @@ -1526,6 +1538,295 @@ impl Local { } } +/// Contexts which provide read access to the database. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from views, reducers, and transactions started by procedures and HTTP handlers. +/// +/// When operating on a concrete-typed [`ViewContext`], [`ReducerContext`] or [`TxContext`], +/// this trait is not necessary, as the context's `db` field provides the same (or greater, read-write) access. +pub trait CtxDbRead { + fn db_read_only(&self) -> &LocalReadOnly; +} + +impl CtxDbRead for TxContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ReducerContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ViewContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for AnonymousViewContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +/// Contexts which provide read-write access to the database. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from reducers and from transactions started by procedures and HTTP handlers. +/// +/// When operating on a concrete-typed [`ReducerContext`] or [`TxContext`], this trait is not necessary, +/// as the context's `db` field provides the same access. +pub trait CtxDbWrite: CtxDbRead { + fn db(&self) -> &Local; +} + +impl CtxDbWrite for TxContext { + fn db(&self) -> &Local { + &Local {} + } +} + +impl CtxDbWrite for ReducerContext { + fn db(&self) -> &Local { + &Local {} + } +} + +/// Contexts which can retrieve the sender [`Identity`]. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from views, reducers, and transactions started by procedures and HTTP handlers. +/// +/// When operating on a concrete-typed [`ViewContext`], [`ReducerContext`], [`ProcedureContext`] or [`TxContext`], +/// this trait is not necessary, as the context's inherent `sender` method provides the same access. +pub trait CtxWithSender { + fn sender(&self) -> Identity; +} + +impl CtxWithSender for ViewContext { + fn sender(&self) -> Identity { + self.sender + } +} + +impl CtxWithSender for ReducerContext { + fn sender(&self) -> Identity { + self.sender + } +} + +impl CtxWithSender for TxContext { + fn sender(&self) -> Identity { + self.0.sender + } +} + +impl CtxWithSender for ProcedureContext { + fn sender(&self) -> Identity { + self.sender + } +} + +/// Contexts which can retrieve the current [`Timestamp`]. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from reducers, procedures, HTTP handlers, +/// and transactions started by procedures and HTTP handlers. +/// +/// When operating on a concrete-typed [`ReducerContext`], [`ProcedureContext`] +#[cfg_attr(feature = "unstable", doc = ", [`HandlerContext`]")] +/// or [`TxContext`], +/// this trait is not necessary, as the context's `timestamp` field provides the same access. +pub trait CtxWithTimestamp { + fn timestamp(&self) -> Timestamp; +} + +impl CtxWithTimestamp for ReducerContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +impl CtxWithTimestamp for TxContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +impl CtxWithTimestamp for ProcedureContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +#[cfg(feature = "unstable")] +impl CtxWithTimestamp for HandlerContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +/// Contexts which can retrieve the current [`AuthCtx`]. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from reducers and procedures. +/// +/// When operating on a concrete-typed [`ReducerContext`], [`ProcedureContext`], [`TxContext`], +/// this trait is not necessary, as the context's sender_auth method provides the same access. +pub trait CtxWithSenderAuth { + fn sender_auth(&self) -> &AuthCtx; +} + +impl CtxWithSenderAuth for ReducerContext { + fn sender_auth(&self) -> &AuthCtx { + self.sender_auth() + } +} + +impl CtxWithSenderAuth for TxContext { + fn sender_auth(&self) -> &AuthCtx { + self.0.sender_auth() + } +} + +/// Contexts which can enter a start a transaction with a [`TxContext`] inside the function. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from reducers and procedures. +/// +/// When operating on a concrete-typed +#[cfg_attr(feature = "unstable", doc = "[`HandlerContext`] or")] +/// [`ProcedureContext`], +/// this trait is not necessary, as the context's methods provide the same access. +pub trait CtxWithTxManagement { + fn with_tx(&mut self, body: impl Fn(&TxContext) -> T) -> T; + fn try_with_tx(&mut self, body: impl Fn(&TxContext) -> Result) -> Result; +} + +impl CtxWithTxManagement for ProcedureContext { + fn with_tx(&mut self, body: impl Fn(&TxContext) -> T) -> T { + self.with_tx(body) + } + + fn try_with_tx(&mut self, body: impl Fn(&TxContext) -> Result) -> Result { + self.try_with_tx(body) + } +} + +#[cfg(feature = "unstable")] +impl CtxWithTxManagement for HandlerContext { + fn with_tx(&mut self, body: impl Fn(&TxContext) -> T) -> T { + self.with_tx(body) + } + + fn try_with_tx(&mut self, body: impl Fn(&TxContext) -> Result) -> Result { + self.try_with_tx(body) + } +} + +/// Contexts which can retrieve the current [`StdbRng`] state. +/// +/// This trait is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from reducers and procedures. +/// +/// When operating on a concrete-typed +#[cfg_attr(feature = "unstable", doc = "[`HandlerContext`],")] +/// [`ProcedureContext`], [`ReducerContext`], [`TxContext`] +/// this trait is not necessary, as the context's methods provide the same access. +#[cfg(feature = "rand08")] +pub trait CtxWithRng { + fn rng(&self) -> &StdbRng; + fn random(&self) -> T + where + Standard: Distribution; +} + +#[cfg(feature = "rand08")] +impl CtxWithRng for ProcedureContext { + fn rng(&self) -> &StdbRng { + self.rng() + } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } +} + +#[cfg(all(feature = "unstable", feature = "rand08"))] +impl CtxWithRng for HandlerContext { + fn rng(&self) -> &StdbRng { + self.rng() + } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } +} + +#[cfg(feature = "rand08")] +impl CtxWithRng for ReducerContext { + fn rng(&self) -> &StdbRng { + self.rng() + } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } +} + +#[cfg(feature = "rand08")] +impl CtxWithRng for TxContext { + fn rng(&self) -> &StdbRng { + self.0.rng() + } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.0.random() + } +} + +/// Contexts which can perform outgoing HTTP requests. +/// +/// This type is useful for writing reusable logic which is generic over the context type, +/// allowing it to be used from procedures and HTTP handlers. +/// +/// When operating on a concrete-typed [`ProcedureContext`] +#[cfg_attr(feature = "unstable", doc = "or [`HandlerContext`],")] +/// this trait is not necessary, +/// as the context's `http` field provides the same access. +pub trait CtxWithHttp { + fn http(&self) -> &HttpClient; +} + +#[cfg(feature = "unstable")] +impl CtxWithHttp for HandlerContext { + fn http(&self) -> &HttpClient { + &self.http + } +} + +impl CtxWithHttp for ProcedureContext { + fn http(&self) -> &HttpClient { + &self.http + } +} + /// The [JWT] of an [`AuthCtx`]. /// /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token diff --git a/modules/keynote-benchmarks/src/lib.rs b/modules/keynote-benchmarks/src/lib.rs index 3ba8394089c..70d68bc0152 100644 --- a/modules/keynote-benchmarks/src/lib.rs +++ b/modules/keynote-benchmarks/src/lib.rs @@ -1,5 +1,5 @@ use core::ops::AddAssign; -use spacetimedb::{log_stopwatch::LogStopwatch, rand::Rng, reducer, table, DbContext, ReducerContext, Table}; +use spacetimedb::{log_stopwatch::LogStopwatch, rand::Rng, reducer, table, ReducerContext, Table}; #[derive(Clone, Copy, Debug)] #[table(accessor = position, public)] @@ -39,7 +39,7 @@ fn init(ctx: &ReducerContext) { // Insert 10^6 randomized positions and velocities, // but with incrementing and corresponding ids. - let db = ctx.db(); + let db = &ctx.db; let mut rng = ctx.rng(); for id in 0..1_000_000 { let (x, y, z) = rng.r#gen(); @@ -71,7 +71,7 @@ fn update_positions_by_collect(ctx: &ReducerContext) { #[reducer] fn roundtrip(ctx: &ReducerContext) { // Warmup the index. - let id = ctx.db().velocity().id(); + let id = ctx.db.velocity().id(); for x in 0..10_000 { id.find(x); } diff --git a/modules/sdk-test-procedure-concurrency/src/lib.rs b/modules/sdk-test-procedure-concurrency/src/lib.rs index 4f99c3a422e..e8874309f8e 100644 --- a/modules/sdk-test-procedure-concurrency/src/lib.rs +++ b/modules/sdk-test-procedure-concurrency/src/lib.rs @@ -1,6 +1,4 @@ -use spacetimedb::{ - procedure, reducer, table, DbContext, ProcedureContext, ReducerContext, ScheduleAt, Table, TxContext, -}; +use spacetimedb::{procedure, reducer, table, ProcedureContext, ReducerContext, ScheduleAt, Table, TxContext}; use std::time::Duration; #[table(public, accessor = procedure_concurrency_row)] @@ -19,7 +17,7 @@ fn insert_procedure_concurrency_row(ctx: &TxContext, insertion_context: &str) { #[reducer] fn insert_reducer_row(ctx: &ReducerContext) { - ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow { + ctx.db.procedure_concurrency_row().insert(ProcedureConcurrencyRow { insertion_order: 0, insertion_context: "reducer".into(), }); @@ -82,7 +80,7 @@ struct ScheduledReducerRow { #[reducer] fn insert_scheduled_reducer(ctx: &ReducerContext, _schedule: ScheduledReducerRow) { - ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow { + ctx.db.procedure_concurrency_row().insert(ProcedureConcurrencyRow { insertion_order: 0, insertion_context: "scheduled_reducer".into(), }); @@ -130,11 +128,11 @@ fn scheduled_procedure_sleep_between_inserts(ctx: &mut ProcedureContext, _schedu #[reducer] fn schedule_procedure_then_reducer(ctx: &ReducerContext) { - ctx.db().scheduled_procedure_row().insert(ScheduledProcedureRow { + ctx.db.scheduled_procedure_row().insert(ScheduledProcedureRow { scheduled_id: 0, scheduled_at: ctx.timestamp.into(), }); - ctx.db().scheduled_reducer_row().insert(ScheduledReducerRow { + ctx.db.scheduled_reducer_row().insert(ScheduledReducerRow { scheduled_id: 0, scheduled_at: (ctx.timestamp + Duration::from_secs(2)).into(), }); diff --git a/modules/sdk-test-procedure/src/lib.rs b/modules/sdk-test-procedure/src/lib.rs index 95ae9b523b1..817be45ce61 100644 --- a/modules/sdk-test-procedure/src/lib.rs +++ b/modules/sdk-test-procedure/src/lib.rs @@ -1,6 +1,6 @@ use spacetimedb::{ - duration, procedure, reducer, table, DbContext, ProcedureContext, ReducerContext, ScheduleAt, SpacetimeType, Table, - Timestamp, TxContext, Uuid, + duration, procedure, reducer, table, ProcedureContext, ReducerContext, ScheduleAt, SpacetimeType, Table, Timestamp, + TxContext, Uuid, }; #[derive(SpacetimeType)] @@ -106,7 +106,7 @@ fn insert_with_tx_rollback(ctx: &mut ProcedureContext) { #[reducer] fn schedule_proc(ctx: &ReducerContext) { // Schedule the procedure to run in 1s. - ctx.db().scheduled_proc_table().insert(ScheduledProcTable { + ctx.db.scheduled_proc_table().insert(ScheduledProcTable { scheduled_id: 0, scheduled_at: duration!("1000ms").into(), // Store the timestamp at which this reducer was called. @@ -134,7 +134,7 @@ fn scheduled_proc(ctx: &mut ProcedureContext, data: ScheduledProcTable) { let ScheduledProcTable { reducer_ts, x, y, .. } = data; let procedure_ts = ctx.timestamp; ctx.with_tx(|ctx| { - ctx.db().proc_inserts_into().insert(ProcInsertsInto { + ctx.db.proc_inserts_into().insert(ProcInsertsInto { reducer_ts, procedure_ts, x,