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
102 changes: 69 additions & 33 deletions crates/bindings/src/rt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::query_builder::Query;
use crate::table::IndexAlgo;
use crate::{sys, AnonymousViewContext, IterBuf, ReducerContext, ReducerResult, SpacetimeType, Table, ViewContext};
use spacetimedb_lib::bsatn::EncodeError;
pub use spacetimedb_lib::db::raw_def::v9::Lifecycle as LifecycleReducer;
use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, RawModuleDefV9Builder, TableType};
use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, RawModuleDefV9Builder, TableType, ViewResultHeader};
use spacetimedb_lib::de::{self, Deserialize, DeserializeOwned, Error as _, SeqProductAccess};
use spacetimedb_lib::sats::typespace::TypespaceBuilder;
use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, ProductTypeElement};
Expand Down Expand Up @@ -80,16 +82,16 @@ pub trait Reducer<'de, A: Args<'de>> {

/// Invoke a caller-specific view.
/// Returns a BSATN encoded `Vec` of rows.
pub fn invoke_view<'a, A: Args<'a>, T: SpacetimeType + Serialize>(
pub fn invoke_view<'a, A: Args<'a>, T: ViewReturn>(
view: impl View<'a, A, T>,
ctx: ViewContext,
args: &'a [u8],
) -> Vec<u8> {
// Deserialize the arguments from a bsatn encoding.
let SerDeArgs(args) = bsatn::from_slice(args).expect("unable to decode args");
let rows: Vec<T> = view.invoke(&ctx, args);
let retn = view.invoke(&ctx, args);
let mut buf = IterBuf::take();
buf.serialize_into(&rows).expect("unable to encode rows");
retn.to_writer(&mut buf).expect("unable to encode view return value");
std::mem::take(&mut *buf)
}
/// A trait for types representing the execution logic of a caller-specific view.
Expand All @@ -102,22 +104,22 @@ pub fn invoke_view<'a, A: Args<'a>, T: SpacetimeType + Serialize>(
note = "where each `Ti` implements `SpacetimeType`.",
note = ""
)]
pub trait View<'de, A: Args<'de>, T: SpacetimeType + Serialize> {
fn invoke(&self, ctx: &ViewContext, args: A) -> Vec<T>;
pub trait View<'de, A: Args<'de>, T: ViewReturn> {
fn invoke(&self, ctx: &ViewContext, args: A) -> T;
}

/// Invoke an anonymous view.
/// Returns a BSATN encoded `Vec` of rows.
pub fn invoke_anonymous_view<'a, A: Args<'a>, T: SpacetimeType + Serialize>(
pub fn invoke_anonymous_view<'a, A: Args<'a>, T: ViewReturn>(
view: impl AnonymousView<'a, A, T>,
ctx: AnonymousViewContext,
args: &'a [u8],
) -> Vec<u8> {
// Deserialize the arguments from a bsatn encoding.
let SerDeArgs(args) = bsatn::from_slice(args).expect("unable to decode args");
let rows: Vec<T> = view.invoke(&ctx, args);
let retn = view.invoke(&ctx, args);
let mut buf = IterBuf::take();
buf.serialize_into(&rows).expect("unable to encode rows");
retn.to_writer(&mut buf).expect("unable to encode view return value");
std::mem::take(&mut *buf)
}
/// A trait for types representing the execution logic of an anonymous view.
Expand All @@ -130,8 +132,8 @@ pub fn invoke_anonymous_view<'a, A: Args<'a>, T: SpacetimeType + Serialize>(
note = "where each `Ti` implements `SpacetimeType`.",
note = ""
)]
pub trait AnonymousView<'de, A: Args<'de>, T: SpacetimeType + Serialize> {
fn invoke(&self, ctx: &AnonymousViewContext, args: A) -> Vec<T>;
pub trait AnonymousView<'de, A: Args<'de>, T: ViewReturn> {
fn invoke(&self, ctx: &AnonymousViewContext, args: A) -> T;
}

/// A trait for types that can *describe* a callable function such as a reducer or view.
Expand Down Expand Up @@ -305,9 +307,29 @@ impl<T: SpacetimeType> ViewArg for T {}
pub trait ViewReturn {
#[doc(hidden)]
const _ITEM: () = ();

fn to_writer(self, w: &mut Vec<u8>) -> Result<(), EncodeError>;
}

impl<T: SpacetimeType + Serialize> ViewReturn for Vec<T> {
fn to_writer(self, buf: &mut Vec<u8>) -> Result<(), EncodeError> {
bsatn::to_writer(buf, &ViewResultHeader::RowData)?;
bsatn::to_writer(buf, &self)
}
}

impl<T: SpacetimeType + Serialize> ViewReturn for Option<T> {
fn to_writer(self, buf: &mut Vec<u8>) -> Result<(), EncodeError> {
bsatn::to_writer(buf, &ViewResultHeader::RowData)?;
bsatn::to_writer(buf, self.as_slice())
}
}

impl<T: SpacetimeType + Serialize> ViewReturn for Query<T> {
fn to_writer(self, buf: &mut Vec<u8>) -> Result<(), EncodeError> {
bsatn::to_writer(buf, &ViewResultHeader::RawSql(self.sql))
}
}
impl<T: SpacetimeType> ViewReturn for Vec<T> {}
impl<T: SpacetimeType> ViewReturn for Option<T> {}

/// Map the correct dispatcher based on the `Ctx` type
pub struct ViewKind<Ctx> {
Expand Down Expand Up @@ -336,7 +358,7 @@ impl ViewDispatcher<ViewContext> {
pub fn invoke<'a, A, T, V>(view: V, ctx: ViewContext, args: &'a [u8]) -> Vec<u8>
where
A: Args<'a>,
T: SpacetimeType + Serialize,
T: ViewReturn,
V: View<'a, A, T>,
{
invoke_view(view, ctx, args)
Expand All @@ -348,7 +370,7 @@ impl ViewDispatcher<AnonymousViewContext> {
pub fn invoke<'a, A, T, V>(view: V, ctx: AnonymousViewContext, args: &'a [u8]) -> Vec<u8>
where
A: Args<'a>,
T: SpacetimeType + Serialize,
T: ViewReturn,
V: AnonymousView<'a, A, T>,
{
invoke_anonymous_view(view, ctx, args)
Expand All @@ -365,7 +387,7 @@ impl ViewRegistrar<ViewContext> {
pub fn register<'a, A, I, T, V>(view: V)
where
A: Args<'a>,
T: SpacetimeType + Serialize,
T: ViewReturn,
I: FnInfo<Invoke = ViewFn>,
V: View<'a, A, T>,
{
Expand All @@ -378,7 +400,7 @@ impl ViewRegistrar<AnonymousViewContext> {
pub fn register<'a, A, I, T, V>(view: V)
where
A: Args<'a>,
T: SpacetimeType + Serialize,
T: ViewReturn,
I: FnInfo<Invoke = AnonymousFn>,
V: AnonymousView<'a, A, T>,
{
Expand Down Expand Up @@ -596,34 +618,32 @@ macro_rules! impl_reducer_procedure_view {
}

// Implement `View<..., ViewContext>` for the tuple type `($($T,)*)`.
impl<'de, Func, Elem, Retn, $($T),*>
View<'de, ($($T,)*), Elem> for Func
impl<'de, Func, Retn, $($T),*>
View<'de, ($($T,)*), Retn> for Func
where
$($T: SpacetimeType + Deserialize<'de> + Serialize,)*
Func: Fn(&ViewContext, $($T),*) -> Retn,
Retn: IntoVec<Elem>,
Elem: SpacetimeType + Serialize,
Retn: ViewReturn,
{
#[allow(non_snake_case)]
fn invoke(&self, ctx: &ViewContext, args: ($($T,)*)) -> Vec<Elem> {
fn invoke(&self, ctx: &ViewContext, args: ($($T,)*)) -> Retn {
let ($($T,)*) = args;
self(ctx, $($T),*).into_vec()
self(ctx, $($T),*)
}
}

// Implement `View<..., AnonymousViewContext>` for the tuple type `($($T,)*)`.
impl<'de, Func, Elem, Retn, $($T),*>
AnonymousView<'de, ($($T,)*), Elem> for Func
impl<'de, Func, Retn, $($T),*>
AnonymousView<'de, ($($T,)*), Retn> for Func
where
$($T: SpacetimeType + Deserialize<'de> + Serialize,)*
Func: Fn(&AnonymousViewContext, $($T),*) -> Retn,
Retn: IntoVec<Elem>,
Elem: SpacetimeType + Serialize,
Retn: ViewReturn,
{
#[allow(non_snake_case)]
fn invoke(&self, ctx: &AnonymousViewContext, args: ($($T,)*)) -> Vec<Elem> {
fn invoke(&self, ctx: &AnonymousViewContext, args: ($($T,)*)) -> Retn {
let ($($T,)*) = args;
self(ctx, $($T),*).into_vec()
self(ctx, $($T),*)
}
}
};
Expand Down Expand Up @@ -754,7 +774,7 @@ pub fn register_view<'a, A, I, T>(_: impl View<'a, A, T>)
where
A: Args<'a>,
I: FnInfo<Invoke = ViewFn>,
T: SpacetimeType + Serialize,
T: ViewReturn,
{
register_describer(|module| {
let params = A::schema::<I>(&mut module.inner);
Expand All @@ -771,7 +791,7 @@ pub fn register_anonymous_view<'a, A, I, T>(_: impl AnonymousView<'a, A, T>)
where
A: Args<'a>,
I: FnInfo<Invoke = AnonymousFn>,
T: SpacetimeType + Serialize,
T: ViewReturn,
{
register_describer(|module| {
let params = A::schema::<I>(&mut module.inner);
Expand Down Expand Up @@ -1046,14 +1066,22 @@ extern "C" fn __call_procedure__(
///
/// The output of the view is written to a `BytesSink`,
/// registered on the host side, with `bytes_sink_write`.
///
/// Note, a previous version of the abi used a different return format for views.
/// We used to write the return rows of the view directly to the sink.
/// However the current version first writes a [`ViewResultHeader`].
/// This is to distinguish between views that return rows vs ones that return queries.
///
/// The current abi is identified by a return code of 2.
/// The previous abi, which we still support, is identified by a return code of 0.
#[no_mangle]
extern "C" fn __call_view_anon__(id: usize, args: BytesSource, sink: BytesSink) -> i16 {
let views = ANONYMOUS_VIEWS.get().unwrap();
write_to_sink(
sink,
&with_read_args(args, |args| views[id](AnonymousViewContext::default(), args)),
);
0
2
}

/// Called by the host to execute a view when the `sender` calls the view identified by `id` with `args`.
Expand All @@ -1066,6 +1094,14 @@ extern "C" fn __call_view_anon__(id: usize, args: BytesSource, sink: BytesSink)
///
/// The output of the view is written to a `BytesSink`,
/// registered on the host side, with `bytes_sink_write`.
///
/// Note, a previous version of the abi used a different return format for views.
/// We used to write the return rows of the view directly to the sink.
/// However the current version first writes a [`ViewResultHeader`].
/// This is to distinguish between views that return rows vs ones that return queries.
///
/// The current abi is identified by a return code of 2.
/// The previous abi, which we still support, is identified by a return code of 0.
#[no_mangle]
extern "C" fn __call_view__(
id: usize,
Expand All @@ -1087,7 +1123,7 @@ extern "C" fn __call_view__(
sink,
&with_read_args(args, |args| views[id](ViewContext::new(sender), args)),
);
0
2
}

/// Run `logic` with `args` read from the host into a `&[u8]`.
Expand Down
99 changes: 26 additions & 73 deletions crates/bindings/tests/ui/views.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ error[E0277]: Views must return `Vec<T>` or `Option<T>` where `T` is a `Spacetim
|
= help: the following other types implement trait `ViewReturn`:
Option<T>
Query<T>
Vec<T>

error[E0277]: invalid view signature
Expand All @@ -261,51 +262,18 @@ note: required by a bound in `ViewDispatcher::<ViewContext>::invoke`
| ^^^^^^^^^^^^^^ required by this bound in `ViewDispatcher::<ViewContext>::invoke`
= note: this error originates in the attribute macro `view` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotSpacetimeType: SpacetimeType` is not satisfied
--> tests/ui/views.rs:136:1
|
136 | #[view(name = view_def_returns_not_a_spacetime_type, public)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SpacetimeType` is not implemented for `NotSpacetimeType`
|
= note: if you own the type, try adding `#[derive(SpacetimeType)]` to its definition
= help: the following other types implement trait `SpacetimeType`:
&T
()
AlgebraicType
AlgebraicTypeRef
Arc<T>
ArrayType
Box<T>
ColId
and $N others
= note: required for `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}` to implement `AnonymousView<'_, (), NotSpacetimeType>`
note: required by a bound in `ViewRegistrar::<AnonymousViewContext>::register`
--> src/rt.rs
|
| pub fn register<'a, A, I, T, V>(view: V)
| -------- required by a bound in this associated function
...
| V: AnonymousView<'a, A, T>,
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ViewRegistrar::<AnonymousViewContext>::register`
= note: this error originates in the attribute macro `view` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotSpacetimeType: Serialize` is not satisfied
error[E0277]: invalid anonymous view signature
--> tests/ui/views.rs:136:1
|
136 | #[view(name = view_def_returns_not_a_spacetime_type, public)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `NotSpacetimeType`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this view signature is not valid
|
= help: the following other types implement trait `Serialize`:
&T
()
(T0, T1)
(T0, T1, T2)
(T0, T1, T2, T3)
(T0, T1, T2, T3, T4)
(T0, T1, T2, T3, T4, T5)
(T0, T1, T2, T3, T4, T5, T6)
and $N others
= note: required for `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}` to implement `AnonymousView<'_, (), NotSpacetimeType>`
= help: the trait `AnonymousView<'_, _, _>` is not implemented for fn item `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}`
= note:
= note: anonymous view signatures must match:
= note: `Fn(&AnonymousViewContext, [T1, ...]) -> Vec<Tn> | Option<Tn>`
= note: where each `Ti` implements `SpacetimeType`.
= note:
note: required by a bound in `ViewRegistrar::<AnonymousViewContext>::register`
--> src/rt.rs
|
Expand Down Expand Up @@ -335,39 +303,11 @@ error[E0277]: the trait bound `NotSpacetimeType: SpacetimeType` is not satisfied
and $N others
= note: required for `Option<NotSpacetimeType>` to implement `ViewReturn`

error[E0277]: the trait bound `NotSpacetimeType: SpacetimeType` is not satisfied
--> tests/ui/views.rs:136:1
|
136 | #[view(name = view_def_returns_not_a_spacetime_type, public)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SpacetimeType` is not implemented for `NotSpacetimeType`
|
= note: if you own the type, try adding `#[derive(SpacetimeType)]` to its definition
= help: the following other types implement trait `SpacetimeType`:
&T
()
AlgebraicType
AlgebraicTypeRef
Arc<T>
ArrayType
Box<T>
ColId
and $N others
= note: required for `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}` to implement `AnonymousView<'_, (), NotSpacetimeType>`
note: required by a bound in `ViewDispatcher::<AnonymousViewContext>::invoke`
--> src/rt.rs
|
| pub fn invoke<'a, A, T, V>(view: V, ctx: AnonymousViewContext, args: &'a [u8]) -> Vec<u8>
| ------ required by a bound in this associated function
...
| V: AnonymousView<'a, A, T>,
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ViewDispatcher::<AnonymousViewContext>::invoke`
= note: this error originates in the attribute macro `view` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotSpacetimeType: Serialize` is not satisfied
--> tests/ui/views.rs:136:1
--> tests/ui/views.rs:137:71
|
136 | #[view(name = view_def_returns_not_a_spacetime_type, public)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `NotSpacetimeType`
137 | fn view_def_returns_not_a_spacetime_type(_: &AnonymousViewContext) -> Option<NotSpacetimeType> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `NotSpacetimeType`
|
= help: the following other types implement trait `Serialize`:
&T
Expand All @@ -379,7 +319,20 @@ error[E0277]: the trait bound `NotSpacetimeType: Serialize` is not satisfied
(T0, T1, T2, T3, T4, T5)
(T0, T1, T2, T3, T4, T5, T6)
and $N others
= note: required for `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}` to implement `AnonymousView<'_, (), NotSpacetimeType>`
= note: required for `Option<NotSpacetimeType>` to implement `ViewReturn`

error[E0277]: invalid anonymous view signature
--> tests/ui/views.rs:136:1
|
136 | #[view(name = view_def_returns_not_a_spacetime_type, public)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this view signature is not valid
|
= help: the trait `AnonymousView<'_, _, _>` is not implemented for fn item `for<'a> fn(&'a AnonymousViewContext) -> Option<NotSpacetimeType> {view_def_returns_not_a_spacetime_type}`
= note:
= note: anonymous view signatures must match:
= note: `Fn(&AnonymousViewContext, [T1, ...]) -> Vec<Tn> | Option<Tn>`
= note: where each `Ti` implements `SpacetimeType`.
= note:
note: required by a bound in `ViewDispatcher::<AnonymousViewContext>::invoke`
--> src/rt.rs
|
Expand Down