#[non_exhaustive]pub struct ReducerContext {
pub timestamp: Timestamp,
pub db: Local,
/* private fields */
}Expand description
The context that any reducer is provided with.
This must be the first argument of the reducer. Clients of the module will
only see arguments after the ReducerContext.
Includes information about the client calling the reducer and the time of invocation, as well as a view into the module’s database.
If the crate was compiled with the rand feature, also includes faculties for random
number generation.
Implements the DbContext trait for accessing views into a database.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.timestamp: TimestampThe time at which the reducer was started.
db: LocalAllows accessing the local database attached to a module.
This slightly strange type appears to have no methods, but that is misleading.
The #[table] macro uses the trait system to add table accessors to this type.
These are generated methods that allow you to access specific tables.
For a table named table, use ctx.db.{table}() to get a handle.
For example:
use spacetimedb::{table, reducer, ReducerContext};
#[table(accessor = book)]
#[derive(Debug)]
struct Book {
#[primary_key]
id: u64,
isbn: String,
name: String,
#[index(btree)]
author: String
}
#[reducer]
fn find_books_by(ctx: &ReducerContext, author: String) {
let book: &book__TableHandle = ctx.db.book();
log::debug!("looking up books by {author}...");
for book in book.author().filter(&author) {
log::debug!("- {book:?}");
}
}See the #[table] macro for more information.
Implementations§
Source§impl ReducerContext
impl ReducerContext
Sourcepub fn random<T>(&self) -> Twhere
Standard: Distribution<T>,
pub fn random<T>(&self) -> Twhere
Standard: Distribution<T>,
Generates a random value.
Similar to rand::random(), but using StdbRng instead.
See also ReducerContext::rng().
Sourcepub fn rng(&self) -> &StdbRng
pub fn rng(&self) -> &StdbRng
Retrieve the random number generator for this reducer transaction, seeded by the timestamp of the reducer call.
If you only need a single random value, you can use ReducerContext::random().
§Examples
use spacetimedb::{reducer, ReducerContext};
use rand::Rng;
#[spacetimedb::reducer]
fn rng_demo(ctx: &spacetimedb::ReducerContext) {
// Can be used in method chaining style:
let digit = ctx.rng().gen_range(0..=9);
// Or, cache locally for reuse:
let mut rng = ctx.rng();
let floats: Vec<f32> = rng.sample_iter(rand::distributions::Standard).collect();
}Source§impl ReducerContext
impl ReducerContext
Sourcepub fn connection_id(&self) -> Option<ConnectionId>
pub fn connection_id(&self) -> Option<ConnectionId>
The ConnectionId of the client that invoked the reducer.
Will be None for certain reducers invoked automatically by the host,
including init and scheduled reducers.
Sourcepub fn sender_auth(&self) -> &AuthCtx
pub fn sender_auth(&self) -> &AuthCtx
Returns the authorization information for the caller of this reducer.
Sourcepub fn as_anonymous_read_only(&self) -> AnonymousViewContext
pub fn as_anonymous_read_only(&self) -> AnonymousViewContext
Create an anonymous (no sender) read-only view context
Sourcepub fn as_read_only(&self) -> ViewContext
pub fn as_read_only(&self) -> ViewContext
Create a sender-bound read-only view context using this reducer’s caller.
Sourcepub fn new_uuid_v4(&self) -> Result<Uuid>
pub fn new_uuid_v4(&self) -> Result<Uuid>
Sourcepub fn new_uuid_v7(&self) -> Result<Uuid>
pub fn new_uuid_v7(&self) -> Result<Uuid>
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ReducerContext
impl !RefUnwindSafe for ReducerContext
impl !Send for ReducerContext
impl !Sync for ReducerContext
impl Unpin for ReducerContext
impl UnsafeUnpin for ReducerContext
impl !UnwindSafe for ReducerContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more