Quickstart: Follow One Call Through the App

Audience task and outcome

  • Task Save a player's score and read it back in the smallest complete cdk-rsx app.
  • Observable outcome The viewer can open an unfamiliar app and point to its endpoints, its service methods, its saved data declaration and its root wiring. The Quickstart native test passes and the viewer can say what each of its assertions shows.
  • Starting point Rust and Internet Computer basics. This is the first film.
  • Handoff Lesson 2 builds a module like Scores from an empty folder.
  • Pacing About three minutes. Leave time to read each highlighted line.

The worked example

Quickstart is a complete canister with one root and one Scores module. Its public put, get, remove and count endpoints each make one call into the Scores service. The native test installs the app in a test Store, saves score 42 for player 9 and score 13 for player 17, reads Some(42), drops the app, opens it again on the same Store and counts 2. It then removes player 9 once with true, a second time with false, and still reads Some(13) for player 17. Quickstart · Endpoints · Scores service · Native test

The layout follows the documented owner-folder convention. lib.rs holds endpoints, each stateful feature has its own folder with its service in mod.rs, and its saved data lives in memory/v1.rs. Owner folders and versions

The story in one pass

Open the app's file tree and name the three kinds of file. Send put(9, 42) through the endpoint, into the Scores service, and watch one value appear in the saved data. Send get(9) down the read path and return Some(42). Show that a read cannot write. Finish with the native test, which throws the service objects away and rebuilds them over the same data.

Views and visual objects

Use the shared visual language. AppTree shows src/ with lib.rs, root/ and scores/, each feature folder holding mod.rs and memory/{mod.rs,v1.rs}. One ModuleCard for Scores lists get, count, put and remove on its top edge. Its drawer holds a StateCard with one table named scores. The App is a thin frame around the Scores card with a small root tab. It has no drawer of its own in this film.

View Framing and anchor
tree AppTree at left, empty stage at right. Anchor on src/lib.rs.
call_path Three EditorPane columns for lib.rs, scores/mod.rs and scores/memory/v1.rs, with the Scores ModuleCard beneath the middle and right columns.
scores_card The Scores card and its open StateCard, large enough to read two rows.
test TestPanel at left with src/tests.rs, the Scores card at right.

Scene open_app — Find the three kinds of file

Enters with A blank paper stage and the small title.

Beat show_tree — One app, three folders

View and focus tree. Reveal src/lib.rs, then root/, then scores/.

Show The real folder listing of tests/canisters/quickstart/src. Point at lib.rs, at scores/mod.rs and at scores/memory/v1.rs in that order.

Narration

This is Quickstart, a complete canister that saves scores. Its public methods are in lib dot r s. The scores feature has its own folder. The root folder wires the app together at startup.

Cue and animation On “public methods”, tint lib.rs. On “its own folder”, tint the scores/ folder and open it. On “wires the app together”, tint root/.

Leaves with The open tree with three tinted groups.

Beat name_roles — Service code and saved data sit apart

View and focus tree, zoomed on scores/.

Show scores/mod.rs and scores/memory/v1.rs side by side in the tree, with memory/mod.rs between them.

Narration

Inside a feature folder, mod dot r s holds the service, which is the code other parts of the app call. The memory folder declares the data that the feature saves. That declaration has a version in its file name.

Cue and animation On “holds the service”, slide the Scores ModuleCard out of scores/mod.rs with its four method names. On “the data that the feature saves”, open the card's drawer from memory/v1.rs and show an empty scores table. On “a version”, pulse the v1 badge on the drawer.

Leaves with The Scores card with an empty scores table and a v1 badge.

Scene follow_put — Save one score

Enters with The Scores card and the tree.

Beat endpoint_put — The endpoint is one line

View and focus call_path, left pane on src/lib.rs.

Show src/lib.rs lines 6 to 9. Point first at the attribute, then at root::with.

#[cdk_rsx::update(guard = cdk_rsx::guards::controller)]
fn put(player: u64, score: u32) {
    root::with(|app| app.scores().put(player, score));
}

Narration

A client calls put with player nine and score forty-two. The update attribute makes this function a public method. Its guard lets only a controller call it. The body asks the root for write access and calls the scores service.

Cue and animation On “A client calls put”, bring the pill put(9, 42) to the pane. On “only a controller”, band the guard argument. On “write access”, band root::with and draw a write scope outline around the Scores card. On “calls the scores service”, band app.scores().put and move the pill to the middle pane.

Leaves with An open write scope and the pill at the Scores service.

Beat service_put — Only Scores changes its data

View and focus call_path, middle and right panes.

Show src/scores/mod.rs lines 43 to 47, then src/scores/memory/v1.rs lines 5 to 7. Point at self.memory.write() and then at the scores field.

impl View<'_, Scope<'_>> {
    pub fn put(&self, player: u64, score: u32) -> Result<()> {
        self.memory.write()?.scores.insert(player, score);
        Ok(())
    }
pub struct Data {
    pub(in crate::scores) scores: Map<u64, u32>,
}

Narration

The service method opens its own saved data for writing and inserts the score into a map. The field is visible only inside the scores module. An endpoint cannot reach it, and neither can another feature. They call put.

Cue and animation On “opens its own saved data”, band self.memory.write() and slide the drawer open. On “inserts the score”, add the row 9 → 42 in green. On “visible only inside the scores module”, band pub(in crate::scores) in the right pane.

Hold or transition Hold everything from “An endpoint cannot reach it”. Point a dashed leader at pub(in crate::scores) with the balloon only code in scores/ can name this field. Clear it after “They call put.” Close the write scope as the call returns.

Leaves with scores showing 9 → 42. No scope is open.

Scene follow_get — Read it back

Enters with One saved row.

Beat endpoint_get — A query asks for read access

View and focus call_path, left pane then middle pane.

Show src/lib.rs lines 10 to 13, then src/scores/mod.rs lines 35 to 38.

#[cdk_rsx::query]
fn get(player: u64) -> Option<u32> {
    root::read(|app| app.scores().get(player))
}
impl<A: Access> View<'_, A> {
    pub fn get(&self, player: u64) -> Result<Option<u32>> {
        Ok(self.memory.read()?.scores.get(&player).copied())
    }

Narration

Reading works the same way with one difference. A query calls root read. The service method copies the value out and returns it. The client receives Some forty-two.

Cue and animation On “A query calls root read”, move get(9) in and draw a read scope. On “copies the value out”, band .copied() and lift 42 from the row without changing it. On “Some forty-two”, send a ReceiptCard Some(42) back to the client.

Leaves with The row unchanged and the receipt at the client.

Beat read_cannot_write — The compiler keeps reads read-only

View and focus call_path, middle pane, scrolled to the top of scores/mod.rs.

Show src/scores/mod.rs lines 7 to 11, the documentation test marked compile_fail.

//! ```compile_fail
//! use quickstart_canister::scores::View;
//! use cdk_rsx_store::ReadScope;
//! fn query(view: View<'_, ReadScope<'_>>) { view.put(1, 2).unwrap(); }
//! ```

Narration

Read methods are written for any kind of access. Write methods exist only under a write scope. Quickstart includes a test that tries to call put from a read scope, and it must fail to compile.

Cue and animation On “any kind of access”, band impl<A: Access> at line 35. On “only under a write scope”, band impl View<'_, Scope<'_>> at line 43. On “must fail to compile”, show the documentation test and stamp it does not compile.

Leaves with The two impl headers banded as the read group and the write group on the Scores card.

Scene run_test — Check it without a canister

Enters with The Scores card with its two method groups.

Beat native_test — Install the app in a test

View and focus test. TestPanel shows the first half of the test.

Show src/tests.rs lines 5 to 12.

    let store = Store::default();
    let app = App::install(&store).unwrap();
    app.with(|view| {
        view.scores().put(9, 42)?;
        view.scores().put(17, 13)
    })
    .unwrap();
    assert_eq!(app.read(|view| view.scores().get(9)).unwrap(), Some(42));

Narration

The same service runs in an ordinary Rust test. The test installs the app, saves two scores in one write scope, and reads the first one back.

Cue and animation On “installs the app”, band App::install. On “saves two scores”, add 9 → 42 and 17 → 13 to a fresh scores table inside one write outline. On “reads the first one back”, mark the assertion as passed.

Leaves with Two rows and one passed assertion.

Beat rebuild_services — Drop the app, keep the data

View and focus test, second half of the test.

Show src/tests.rs lines 13 to 18. Point at drop(app) and App::open(&store).

    drop(app);
    let app = App::open(&store).unwrap();
    assert_eq!(app.read(|view| view.scores().count()).unwrap(), 2);
    assert!(app.with(|view| view.scores().remove(9)).unwrap());
    assert!(!app.with(|view| view.scores().remove(9)).unwrap());
    assert_eq!(app.read(|view| view.scores().get(17)).unwrap(), Some(13));

Narration

Then the test drops the app. The service objects are gone, and the saved scores are still there. Opening the app again builds new services over the same data. The count is two. Removing player nine works once, and player seventeen still has thirteen.

Cue and animation On “drops the app”, fade the App frame and the Scores card's method edge while the drawer and both rows stay. On “builds new services”, redraw the frame and method edge. On “The count is two”, pass the third assertion. On “works once”, remove row 9 in red, pass the true assertion, then pass the false assertion with no change. On “still has thirteen”, pulse row 17.

Leaves with A rebuilt Scores card with one row, 17 → 13, and every assertion passed.

Beat handoff — Next, build a module

View and focus Return to tree with the Scores card beside it.

Show The scores/ folder, with root/ dimmed.

Narration

Every stateful feature in an app follows this shape. Next we add one to a larger app, starting from an empty folder.

Cue and animation On “this shape”, outline the scores/ folder and its card together. On “an empty folder”, show a dashed empty folder outline beside it.

Leaves with The Scores shape and an empty folder outline for lesson 2.

Try it

Verify From the newstack root, with the pinned environment prepared, run the Quickstart package tests. Testing

source tools/env.sh
cargo test --manifest-path tests/Cargo.toml --locked --offline -p quickstart-canister

Expected app-level result. scores_survive_service_rebind_and_root_open passes, and the documentation tests in scores/mod.rs report their compile_fail cases as passing.

Change, adapted exercise Add a has(player) -> bool query. Put has in the impl<A: Access> View block of scores/mod.rs using self.memory.read()?.scores.contains_key(&player), and add a #[cdk_rsx::query] endpoint in lib.rs that calls it through root::read. Extend the native test. Expected result is true for player 17 and false for player 9 after the removal. This method is not in the checkout.

Source and review notes

Item shown Status
#[cdk_rsx::update], #[cdk_rsx::query], cdk_rsx::guards::controller Shipped SDK API. Guards
#[cdk_rsx::service(memory = Mem, view = View)], Access, Scope Shipped SDK API, documented in the conventions. Bind services once
root::read, root::with Generated for the app by the root attribute in root/mod.rs. Keep the root declarative
App::install, App::open Functions written by the app in root/mod.rs. Lesson 2 writes them. Quickstart root
  • root::with and root::read return the closure's value directly. A storage error inside them traps the call. The film does not need to say this until lesson 5.
  • Keep #[repr(C)] and the derive line out of frame in service_put. Lesson 2 shows the full declaration.
  • The Quickstart README mentions lifecycle and admission terms. This film uses none of them. Lesson 6 covers the upgrade.
  • The native test proves service rebuilding in a test Store. It is not an upgrade test. Keep the narration at “drops the app” and “opening the app again”.