Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rust module documentation #2050

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open

Conversation

kazimuth
Copy link
Contributor

Description of Changes

While I'm rewriting the reference, I figured I'd do this as well.

Expected complexity level and risk

0

Testing

Added doctests

@kazimuth kazimuth force-pushed the jgilles/rust-docs-update branch from 6137dc2 to 1d89177 Compare December 17, 2024 21:55
@kazimuth kazimuth force-pushed the jgilles/rust-docs-update branch 2 times, most recently from 68c8c19 to 0f1c3df Compare January 10, 2025 19:26
More docs

One more patch

More table docs

More docs

Denser sats comments

Move comments

More doc rewriting

Substantial rewrite of main doc slug

More links and emphasis

Hmm

Document macros inline in the bindings crate to make links better

Fix some errors in rust module bindings docs + work on schedule documentation

Table macro docs polish

A day of writing
@kazimuth kazimuth force-pushed the jgilles/rust-docs-update branch from 09b3a03 to 1256125 Compare January 15, 2025 18:10
@kazimuth
Copy link
Contributor Author

There are still a number of doctest failures that need to be fixed, and some warnings in upstream crates of bindings as well, but I'm opening this up for textual review now.

@kazimuth kazimuth marked this pull request as ready for review January 15, 2025 18:11
@kazimuth kazimuth removed the request for review from joshua-spacetime January 15, 2025 19:39
@@ -0,0 +1,517 @@
# SpacetimeDB Rust Module Library
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend reviewing starting here

Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great stuff, thanks!

but *are* resolved when compiled by Rustdoc.
-->

[SpacetimeDB](https://spacetimedb.com/) allows using the Rust language to write server-side applications called **modules**. Modules run **inside** a SQL database. They have direct access to database tables, and expose public functions called **reducers** that can be invoked over the network. Clients connect directly to the database to read data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[SpacetimeDB](https://spacetimedb.com/) allows using the Rust language to write server-side applications called **modules**. Modules run **inside** a SQL database. They have direct access to database tables, and expose public functions called **reducers** that can be invoked over the network. Clients connect directly to the database to read data.
[SpacetimeDB](https://spacetimedb.com/) allows using the Rust language to write server-side applications called **modules**. Modules run inside a relational database. They have direct access to database tables, and expose public functions called **reducers** that can be invoked over the network. Clients connect directly to the database to read data.

The defining feature of our DBs are not their SQL handling; modules do not interact with their DBs via SQL.

"inside" is not a vocab word here, and so should not use the same formatting as "modules" and "reducers." I also don't think it needs to be emphasized.


Rust modules are written with the the Rust Module Library (this crate). They are built using [cargo](https://doc.rust-lang.org/cargo/) and deployed using the [`spacetime` CLI tool](https://spacetimedb.com/install). Rust modules can import any Rust [crate](https://crates.io/) that supports being compiled to WebAssembly.

(Note: Rust can also be used to write **clients** of SpacetimeDB databases, but this requires using a completely different library, the SpacetimeDB Rust Client SDK. See the documentation on [clients] for more information.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this link work? There's no header or anchor in this file for it to go to, but when I click on it in the GitHub Markdown viewer, somehow it sends me to https://spacetimedb.com/docs/#client .

Created new database with identity: <hex string>
```

This hex string is the [`Identity`] of the created database. It distinguishes the created database from the other databases running on the SpacetimeDB network. It is used when administering the module, for example using the [`spacetime logs <DATABASE_IDENTITY>`](#the-log-crate) command. You should save it to a text file so that you can remember it. <!-- TODO: is there a CLI command that says "list all Identities running this version of a module?" -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a CLI command that says "list all Identities running this version of a module?

Nope.

Comment on lines +155 to +165
spacetime publish
```

When you publish your module, a database will be created with the requested tables, and the module will be installed inside it.

The output of `spacetime publish` will end with a line:
```text
Created new database with identity: <hex string>
```

This hex string is the [`Identity`] of the created database. It distinguishes the created database from the other databases running on the SpacetimeDB network. It is used when administering the module, for example using the [`spacetime logs <DATABASE_IDENTITY>`](#the-log-crate) command. You should save it to a text file so that you can remember it. <!-- TODO: is there a CLI command that says "list all Identities running this version of a module?" -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend telling readers to name their DBs. "Make sure to write down this hex string!" is pretty terrible compared to "give it a fun name you'll remember! Also, it has this hex string. Anywhere you use the name, you can use the hex string too."


Under the hood, SpacetimeDB modules are WebAssembly modules that import a [specific WebAssembly ABI](https://spacetimedb.com/docs/webassembly-abi) and export a small number of special functions. This is automatically configured when you add the `spacetime` crate as a dependency of your application.

The SpacetimeDB host is an application that hosts SpacetimeDB databases. It is [source available](https://github.com/clockworklabs/SpacetimeDB). You can run your own host, or you can upload your module to the public SpacetimeDB network. <!-- TODO: want a link to some dashboard for the public network. --> The network will create a database for you and install your module in it to serve client requests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The SpacetimeDB host is an application that hosts SpacetimeDB databases. It is [source available](https://github.com/clockworklabs/SpacetimeDB). You can run your own host, or you can upload your module to the public SpacetimeDB network. <!-- TODO: want a link to some dashboard for the public network. --> The network will create a database for you and install your module in it to serve client requests.
The SpacetimeDB host is an application that hosts SpacetimeDB databases. [Its source code is available](https://github.com/clockworklabs/SpacetimeDB) under [the Business Source License with an Additional Use Grant](https://github.com/clockworklabs/SpacetimeDB/blob/master/LICENSE.txt). You can run your own host, or you can upload your module to the public SpacetimeDB network. <!-- TODO: want a link to some dashboard for the public network. --> The network will create a database for you and install your module in it to serve client requests.

@@ -542,7 +542,7 @@ pub trait DeserializeSeed<'de> {
use crate::de::impls::BorrowedSliceVisitor;
pub use spacetimedb_bindings_macro::Deserialize;

/// A **datastructure** that can be deserialized from any data format supported by SATS.
/// A **data structure** that can be deserialized from any data format supported by the SpacetimeDB Algebraic Type System.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emphasis is weird and ugly here.

Comment on lines +214 to +216
/// Do not manually implement this trait unless you know what you are doing.
/// Incorrect implementations are safe, but can result in data loss.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note about this warning.

///
/// Deriving this trait also derives [`Serialize`](crate::ser::Serialize), [`Deserialize`](crate::de::Deserialize),
/// and [`Debug`](std::fmt::Debug). (There are currently no trait bounds on `SpacetimeType` documenting this fact.)
/// These traits are used to convert Rust data structures to other formats, suitable for storing on disk or passing over the network. `Debug` is simply for debugging convenience.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// These traits are used to convert Rust data structures to other formats, suitable for storing on disk or passing over the network. `Debug` is simply for debugging convenience.
/// `Serialize` and `Deserialize` are used to convert Rust data structures to other formats, suitable for storing on disk or passing over the network. `Debug` is simply for debugging convenience.

/// and [`Debug`](std::fmt::Debug). (There are currently no trait bounds on `SpacetimeType` documenting this fact.)
/// These traits are used to convert Rust data structures to other formats, suitable for storing on disk or passing over the network. `Debug` is simply for debugging convenience.
///
/// Any Rust type implementing `SpacetimeType` can be used in table and reducer declarations. A derive macro is provided, and can be used on both structs and enums:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be more specific? I believe we don't allow tuple structs. I'm not sure whether or not we allow unit structs. I think on enums we only allow unit variants and single-element tuple variants, not struct variants or n-tuple variants. I could be wrong though; you should check.

///
/// (Storing collections in rows of a database table is a form of [denormalization](https://en.wikipedia.org/wiki/Denormalization).)
///
/// Do not manually implement this trait unless you are VERY sure you know what you're doing. Incorrect implementations can result in data loss!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about the warning here.

@bfops bfops added release-any To be landed in any release window no runtime change This change does not affect the final binaries labels Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no runtime change This change does not affect the final binaries release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants