Tool Library: A Module Owns Its Memory

Design problem and decision

  • Design problem A branch lends tools from a shelf. The count of tools on the shelf must never be wrong, whoever else joins the codebase later.
  • Viewer's decision Where does the count live, and which code is allowed to change it?
  • Outcome The viewer can describe a module as public functions, private memory, and the code between them. They can say what the seal on the memory guarantees, find the module in the file tree, and require that a refusal changes nothing.
  • Starting point The viewer knows canisters and public methods. Nothing from this course is on screen yet.
  • Handoff Lesson 2 puts five modules together.
  • Pacing Three minutes.

Worked design

Tool Library is an illustrative app made for this course. This film uses one of its modules, Shelf, for branch 3.

Saved in Shelf Value at the start
tool 17 (drill) owned 4, available 3
tool 22 (ladder) owned 1, available 0
Public function Result in this film
take(17) true. Available goes from 3 to 2.
take(22) false. Nothing changes.
stock(17) owned 4, available 2

The story in one pass

Show the feature. Draw the module that answers it as three layers. Find it in the file tree. Send a call through the public rim and watch the vault change. Aim a call at the vault from outside and watch it stop. Say what the seal means. Then send a call that must be refused and watch nothing move.

Views and visual objects

Use the shared visual language. This film introduces ModuleCapsule, the call pill, OutcomeCard and AppTree.

View Framing and anchor
capsule The Shelf ModuleCapsule centered, large enough to read the rim, the band and the vault table.
tree_and_capsule AppTree at left with shelf/ expanded. The capsule at right. Thin leaders join each file to a layer.
seal Close on the lower half of the capsule: the service band, the key badge and the vault edge.

Scene one_module — What a module is

Already on screen Light paper and the small title. Nothing else.

Beat request — The feature

View and focus capsule, empty stage.

Narration

A neighborhood library lends tools. Branch three has four drills, and three are on the shelf. Someone borrows one. We need a count that is always right.

Cue and animation On “four drills”, a plain note appears: tool 17: owned 4, available 3. On “borrows one”, the call pill take(17) enters from the left and waits.

Leaves with The note and the waiting pill.

Beat three_layers — Functions, code, memory

View and focus capsule. Focus moves top to bottom.

Narration

We give the shelf to one module. Its public functions are how other parts of the app use it. Its private memory holds the count. The module's code connects the two.

Cue and animation On “one module”, the capsule outline draws around the note. On “public functions”, the rim fills with take, put_back, stock, all_home. On “private memory”, the note settles into the vault as a table and the vault outline turns solid. On “connects the two”, the dashed service band draws, with the key badge inside it.

Leaves with The complete Shelf capsule, vault closed, values visible through it.

Beat in_the_tree — Where it lives

View and focus tree_and_capsule.

Show The AppTree with only shelf/ expanded:

shelf/
  mod.rs
  memory/
    mod.rs
    v1.rs

Narration

In the project, a module is a folder. The functions and the code are in mod dot r s. Everything the module saves is declared in its memory folder, in a numbered version file. This one is version one.

Cue and animation On “a folder”, shelf/ highlights. On “mod dot r s”, a leader joins shelf/mod.rs to the rim and band. On “memory folder”, a leader joins memory/v1.rs to the vault. On “version one”, the tab v1 appears on the vault.

Leaves with Tree and capsule joined by two leaders. The v1 tab stays on the vault for the rest of the course.

Scene the_seal — Who can touch the memory

Already on screen The Shelf capsule with its v1 tab, the tree at left, the pill take(17) waiting.

Beat through_the_rim — The only way in

View and focus capsule.

Narration

The call arrives at a public function. The module's code checks the shelf, then uses its key to open its own memory and write. Three becomes two.

Cue and animation On “public function”, the pill docks at take on the rim. On “checks the shelf”, a small tick appears beside available 3. On “uses its key”, the key badge turns and the vault opens. On “Three becomes two”, available changes from 3 to 2 in red with a -1 that rises slightly. An OutcomeCard reading true leaves from the rim.

Leaves with tool 17: owned 4, available 2. Vault closed again.

Beat straight_at_vault — Outside code stops at the seal

View and focus seal.

Show One snippet, tabbed shelf/memory/v1.rs — illustrative, modelled on Shop's inventory version file:

pub struct Data {
    pub(in crate::shelf) tools: Map<u64, Tool>,
}

#[cdk_rsx_store::memory(owner = crate::shelf)]
pub struct Memory;

Narration

Now some other code tries to set the count directly. It cannot. Outside the module, this memory is a sealed handle. Other code can hold the handle and pass it along. Only the module's own code holds the key.

Cue and animation On “some other code”, a grey pill tools[17] = 9 approaches the vault from the side. On “It cannot”, it stops at the vault edge and a small seal mark appears there. On “sealed handle”, the line pub struct Memory; highlights. On “holds the key”, the line pub(in crate::shelf) highlights and the key badge pulses inside the capsule.

Leaves with The grey pill gone. Seal mark on the vault edge.

Beat what_seal_means — What the seal promises

View and focus seal. PauseAndExplain pointing at the seal mark.

Hold The balloon reads:

The seal means
  every change to this count goes through Shelf's functions
  no other module's code can read or write these fields

The seal does not mean
  Shelf's own rules are correct
  the data is hidden from whoever controls the canister

Narration

So when the count is wrong, there is one place to look. The seal is a rule the compiler enforces between modules. It keeps the app's own code honest. The rules inside the module are still ours to get right.

Cue and animation All motion holds. The balloon opens on “one place to look”. Lines of the balloon appear with the matching sentence.

Leaves with Balloon closed. Capsule unchanged.

Scene refuse_first — A refusal changes nothing

Already on screen The Shelf capsule. tool 17: owned 4, available 2. The vault table now also shows tool 22: owned 1, available 0.

Beat none_left — The ladder is out

View and focus capsule.

Narration

Someone asks for the ladder. There is one, and it is out. The module checks, and answers no. Nothing in memory moved.

Cue and animation On “asks for the ladder”, the pill take(22) docks at take. On “checks”, a small cross appears beside available 0. On “answers no”, an OutcomeCard with a neutral outline reading false leaves from the rim. On “Nothing in memory moved”, the vault stays closed and both rows pulse once, unchanged.

Leaves with Both rows as before.

Beat check_then_write — The order matters

View and focus capsule. PauseAndExplain pointing at the service band.

Hold The balloon reads:

1  check every rule
2  then write
An error returned after a write does not undo the write.

Narration

This order is a design rule, and you should ask for it by name. Decide every refusal before the first write. If code writes and then returns an error, the write stays.

Cue and animation All motion holds. Lines one and two appear on “Decide every refusal”. The third line appears on “the write stays”.

Leaves with The Shelf capsule, with v1 tab and seal, values 17: 4/2 and 22: 1/0. Lesson 2 opens on this capsule and adds four more.

Brief to hand over

Illustrative. This is the kind of card the architect gives a developer or an AI for this module.

Module shelf. Saves, per tool: how many the branch owns and how many are available. Bound: at most 500 tools per shelf. Public functions: stock(tool), take(tool), put_back(tool), all_home(). No other module reads or writes these fields. Every function decides its refusal before it writes.

Acceptance test, expected and not run

Given Command Expect Then
17: owned 4, available 3 take(17) true 17: available 2
22: owned 1, available 0 take(22) false 22 unchanged
17: owned 4, available 4 put_back(17) false 17 unchanged
no tool 99 take(99) false nothing added
500 tools add a 501st refused still 500

After every row: for each tool, 0 ≤ available ≤ owned.

Production and source notes

  • Tool Library and the Shelf module are illustrative. The snippet is not in the checkout. Its shape follows Shop's inventory version file, which declares pub(in crate::inventory) items and pub struct Memory. Shop inventory version file, lines 9 to 16.
  • The folder layout is the documented convention. Conventions, lines 8 to 40.
  • What the seal guarantees and does not: mental-model review, claim 2.
  • Check before write, and no undo on a returned error: Store architecture, lines 148 to 152. Shop's sell returns false before it touches stock. Shop inventory, lines 38 to 48.
  • The bound of 500 is an app decision in this design. Shop sets 1,024 items in the same way. Shop inventory, lines 4 and 30 to 37.
  • Do not show the key as an object that travels. It is a badge that stays inside the capsule and turns.
  • Do not show what the memory attribute generates.