From 5645288df7595cb872cd8ff0f9fd1b7e22b45c16 Mon Sep 17 00:00:00 2001 From: "kistz (Kilian Strunz)" Date: Sun, 14 Jun 2026 14:10:57 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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; }