From 5645288df7595cb872cd8ff0f9fd1b7e22b45c16 Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sun, 14 Jun 2026 14:10:57 +0200 Subject: [PATCH 01/11] impl --- crates/bindings/src/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 62b78e14be4..9d70a410c77 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1440,6 +1440,7 @@ impl ProcedureContext { } /// A handle on a database with a particular table schema. +#[deprecated(note = "Use the capability based traits (CtxDbRead, CtxDbWrite, CtxWithSender) instead!")] pub trait DbContext { /// A view into the tables of a database. /// @@ -1461,6 +1462,7 @@ pub trait DbContext { fn db_read_only(&self) -> &LocalReadOnly; } +#[allow(deprecated)] impl DbContext for AnonymousViewContext { type DbView = LocalReadOnly; @@ -1473,6 +1475,7 @@ impl DbContext for AnonymousViewContext { } } +#[allow(deprecated)] impl DbContext for ReducerContext { type DbView = Local; @@ -1485,6 +1488,7 @@ impl DbContext for ReducerContext { } } +#[allow(deprecated)] impl DbContext for TxContext { type DbView = Local; @@ -1497,6 +1501,7 @@ impl DbContext for TxContext { } } +#[allow(deprecated)] impl DbContext for ViewContext { type DbView = LocalReadOnly; @@ -1526,6 +1531,76 @@ impl Local { } } +/// This trait allows you to be generic over all contexts that allow you to read from the db. +/// including views, reducers and event procedures and http handlers through [TxContext]. +pub trait CtxDbRead { + fn db(&self) -> &LocalReadOnly; +} + +impl CtxDbRead for TxContext { + fn db(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ReducerContext { + fn db(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ViewContext { + fn db(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for AnonymousViewContext { + fn db(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +/// This trait allows you to be generic over all contexts that allow read/write access to the db. +pub trait CtxDbWrite { + fn db(&self) -> &Local; +} + +impl CtxDbWrite for TxContext { + fn db(&self) -> &Local { + &Local {} + } +} + +impl CtxDbWrite for ReducerContext { + fn db(&self) -> &Local { + &Local {} + } +} + +/// This trait allows you to be generic over all contexts that allow to retrieve the caller identity. +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 + } +} + /// The [JWT] of an [`AuthCtx`]. /// /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token From 0b0c5f99417e3f72748c0bcf953c7dba8ba8ea2e Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sun, 14 Jun 2026 15:43:55 +0200 Subject: [PATCH 02/11] finish the impl --- crates/bindings/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 9d70a410c77..f31de2c1237 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -912,6 +912,8 @@ pub use spacetimedb_bindings_macro::view; pub struct QueryBuilder {} pub use query_builder::{Query, RawQuery}; +use crate::http::{HandlerContext, HttpClient}; + /// One of two possible types that can be passed as the first argument to a `#[view]`. /// The other is [`ViewContext`]. /// Use this type if the view does not depend on the caller's identity. @@ -1532,7 +1534,8 @@ impl Local { } /// This trait allows you to be generic over all contexts that allow you to read from the db. -/// including views, reducers and event procedures and http handlers through [TxContext]. +/// including views, reducers and even procedures and http handlers through [TxContext]. +/// Useful when trying to encapsulate logic in reusable parts. pub trait CtxDbRead { fn db(&self) -> &LocalReadOnly; } @@ -1578,7 +1581,7 @@ impl CtxDbWrite for ReducerContext { } } -/// This trait allows you to be generic over all contexts that allow to retrieve the caller identity. +/// This trait allows you to be generic over all contexts that allow to retrieve the caller [Identity]. pub trait CtxWithSender { fn sender(&self) -> Identity; } @@ -1601,6 +1604,40 @@ impl CtxWithSender for TxContext { } } +/// This trait allows you to be generic over all contexts that allow to retrieve the [Timestamp] of calling them. +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 + } +} + +/// This trait allows you to be generic over all contexts that allow to use the [HttpClient]. +pub trait CtxWithHttp { + fn http(&self) -> &HttpClient; +} + +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 From 0bd8d52069d28534dd2616bfbfee3b76d72e652b Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sun, 14 Jun 2026 16:08:44 +0200 Subject: [PATCH 03/11] fix wrong deprecation message --- crates/bindings/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index f31de2c1237..146d85f4164 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1442,7 +1442,7 @@ impl ProcedureContext { } /// A handle on a database with a particular table schema. -#[deprecated(note = "Use the capability based traits (CtxDbRead, CtxDbWrite, CtxWithSender) instead!")] +#[deprecated(note = "Use the capability based traits (CtxDbRead, CtxDbWrite) instead!")] pub trait DbContext { /// A view into the tables of a database. /// From 43f789e05aad120917016a6c9fe9d6bdd7d2c320 Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sun, 14 Jun 2026 16:35:20 +0200 Subject: [PATCH 04/11] fix impl --- crates/bindings/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 146d85f4164..f079c9a99d4 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1537,35 +1537,35 @@ impl Local { /// including views, reducers and even procedures and http handlers through [TxContext]. /// Useful when trying to encapsulate logic in reusable parts. pub trait CtxDbRead { - fn db(&self) -> &LocalReadOnly; + fn db_read_only(&self) -> &LocalReadOnly; } impl CtxDbRead for TxContext { - fn db(&self) -> &LocalReadOnly { + fn db_read_only(&self) -> &LocalReadOnly { &LocalReadOnly {} } } impl CtxDbRead for ReducerContext { - fn db(&self) -> &LocalReadOnly { + fn db_read_only(&self) -> &LocalReadOnly { &LocalReadOnly {} } } impl CtxDbRead for ViewContext { - fn db(&self) -> &LocalReadOnly { + fn db_read_only(&self) -> &LocalReadOnly { &LocalReadOnly {} } } impl CtxDbRead for AnonymousViewContext { - fn db(&self) -> &LocalReadOnly { + fn db_read_only(&self) -> &LocalReadOnly { &LocalReadOnly {} } } /// This trait allows you to be generic over all contexts that allow read/write access to the db. -pub trait CtxDbWrite { +pub trait CtxDbWrite: CtxDbRead { fn db(&self) -> &Local; } From 1190071fa4ede8aa6eaa402b10d3b9ab9da89169 Mon Sep 17 00:00:00 2001 From: Kilian Strunz <93079615+kistz@users.noreply.github.com> Date: Sat, 27 Jun 2026 09:07:26 +0200 Subject: [PATCH 05/11] Update crates/bindings/src/lib.rs Co-authored-by: Phoebe Goldman Signed-off-by: Kilian Strunz <93079615+kistz@users.noreply.github.com> --- crates/bindings/src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index f079c9a99d4..7c81816e861 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1533,9 +1533,13 @@ impl Local { } } -/// This trait allows you to be generic over all contexts that allow you to read from the db. -/// including views, reducers and even procedures and http handlers through [TxContext]. -/// Useful when trying to encapsulate logic in reusable parts. +/// 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; } From 923939b74cfbeffdf0c1a2182fb8bffa411089d1 Mon Sep 17 00:00:00 2001 From: Kilian Strunz <93079615+kistz@users.noreply.github.com> Date: Sat, 27 Jun 2026 09:07:46 +0200 Subject: [PATCH 06/11] Update crates/bindings/src/lib.rs Co-authored-by: Phoebe Goldman Signed-off-by: Kilian Strunz <93079615+kistz@users.noreply.github.com> --- crates/bindings/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 7c81816e861..dde125413f6 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1568,7 +1568,13 @@ impl CtxDbRead for AnonymousViewContext { } } -/// This trait allows you to be generic over all contexts that allow read/write access to the db. +/// 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; } From faa6a9313a5c426809672e73f0792e1559bb75c5 Mon Sep 17 00:00:00 2001 From: Kilian Strunz <93079615+kistz@users.noreply.github.com> Date: Sat, 27 Jun 2026 09:07:58 +0200 Subject: [PATCH 07/11] Update crates/bindings/src/lib.rs Co-authored-by: Phoebe Goldman Signed-off-by: Kilian Strunz <93079615+kistz@users.noreply.github.com> --- crates/bindings/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index dde125413f6..1d448a5d96f 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1614,7 +1614,14 @@ impl CtxWithSender for TxContext { } } -/// This trait allows you to be generic over all contexts that allow to retrieve the [Timestamp] of calling them. +/// 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`], [`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; } From 228165534517fd411d1723af7c16d8af9fdd40e5 Mon Sep 17 00:00:00 2001 From: Kilian Strunz <93079615+kistz@users.noreply.github.com> Date: Sat, 27 Jun 2026 09:08:21 +0200 Subject: [PATCH 08/11] Update crates/bindings/src/lib.rs Co-authored-by: Phoebe Goldman Signed-off-by: Kilian Strunz <93079615+kistz@users.noreply.github.com> --- crates/bindings/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 1d448a5d96f..1a724fc9638 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1638,7 +1638,13 @@ impl CtxWithTimestamp for TxContext { } } -/// This trait allows you to be generic over all contexts that allow to use the [HttpClient]. +/// 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`] or [`HandlerContext`], this trait is not necessary, +/// as the context's `http` field provides the same access. pub trait CtxWithHttp { fn http(&self) -> &HttpClient; } From f532a7ec6897cd35a3868e049c7244001c4e646b Mon Sep 17 00:00:00 2001 From: Kilian Strunz <93079615+kistz@users.noreply.github.com> Date: Sat, 27 Jun 2026 09:08:33 +0200 Subject: [PATCH 09/11] Update crates/bindings/src/lib.rs Co-authored-by: Phoebe Goldman Signed-off-by: Kilian Strunz <93079615+kistz@users.noreply.github.com> --- crates/bindings/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 1a724fc9638..146aead18b9 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1591,7 +1591,13 @@ impl CtxDbWrite for ReducerContext { } } -/// This trait allows you to be generic over all contexts that allow to retrieve the caller [Identity]. +/// 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`] 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; } From 900b6488565da9c717cbe81aebe97a551ae2d947 Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sat, 27 Jun 2026 09:28:30 +0200 Subject: [PATCH 10/11] apply applicable suggestions --- crates/bindings/src/lib.rs | 110 ++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 146aead18b9..43e207267d1 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1596,7 +1596,7 @@ impl CtxDbWrite for ReducerContext { /// 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, +/// 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; @@ -1620,6 +1620,12 @@ impl CtxWithSender for TxContext { } } +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, @@ -1644,6 +1650,108 @@ impl CtxWithTimestamp for TxContext { } } +impl CtxWithTimestamp for ProcedureContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +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 [`HandlerContext`], [`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) + } +} + +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 [`HandlerContext`], [`ProcedureContext`], [`ReducerContext`], [`TxContext`] +/// this trait is not necessary, as the context's methods provide the same access. +pub trait CtxWithRng { + fn rng(&self) -> &StdbRng; +} + +impl CtxWithRng for ProcedureContext { + fn rng(&self) -> &StdbRng { + self.rng() + } +} + +impl CtxWithRng for HandlerContext { + fn rng(&self) -> &StdbRng { + self.rng() + } +} + +impl CtxWithRng for ReducerContext { + fn rng(&self) -> &StdbRng { + self.rng() + } +} + +impl CtxWithRng for TxContext { + fn rng(&self) -> &StdbRng { + self.0.rng() + } +} + /// Contexts which can perform outgoing HTTP requests. /// /// This type is useful for writing reusable logic which is generic over the context type, From 6d432277031bc4503b282843c9aada6f89c95b4d Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sat, 27 Jun 2026 09:37:55 +0200 Subject: [PATCH 11/11] add missing random accessor --- crates/bindings/src/lib.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 43e207267d1..3d826cc7cb1 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -24,11 +24,12 @@ pub use spacetimedb_query_builder as query_builder; #[cfg(feature = "unstable")] pub use client_visibility_filter::Filter; pub use log; +#[cfg(feature = "rand08")] +use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand")] pub use rand08 as rand; #[cfg(feature = "rand")] use rand08::RngCore; -#[cfg(feature = "rand08")] pub use rng::StdbRng; pub use sats::SpacetimeType; #[doc(hidden)] @@ -1726,30 +1727,61 @@ impl CtxWithTxManagement for HandlerContext { /// this trait is not necessary, as the context's methods provide the same access. pub trait CtxWithRng { fn rng(&self) -> &StdbRng; + fn random(&self) -> T + where + Standard: Distribution; } impl CtxWithRng for ProcedureContext { fn rng(&self) -> &StdbRng { self.rng() } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } } impl CtxWithRng for HandlerContext { fn rng(&self) -> &StdbRng { self.rng() } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } } impl CtxWithRng for ReducerContext { fn rng(&self) -> &StdbRng { self.rng() } + + fn random(&self) -> T + where + Standard: Distribution, + { + self.random() + } } 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.