leaderboard.rs
cdk-rsx/crates/cdk-rsx-ashroot/examples/leaderboard.rs
1//! Native, runnable example: `cargo run --example leaderboard` in the pinned env.2//!3//! The application owns one opaque database child. Ashroot owns all row/index4//! memory; callers cannot mutate a stored Player or change its indexes directly.5#![feature(allocator_ext, core_intrinsics, rustc_attrs, type_info)]6#![allow(internal_features)]78use cdk_rsx_ashroot::{9 Database, Desc, Descending, I0, Text,10 backend::{Nyxus, Resources},11};12use cdk_rsx_engine_nyxus::NyxusData;13use cdk_rsx_store::Store;14use core::ops::Bound::Unbounded;1516const POLICY: [u8; 32] = cdk_rsx_source_policy::source_policy!();1718#[derive(NyxusData)]19struct Player {20 name: Text<24>,21 score: u32,22}23cdk_rsx_ashroot::projection! {24 ByScore for Players[I0] -> u32 {25 dependencies: [POLICY], project: |player| Ok(Some(player.score)),26 }27}28// SAFETY: rows have no mutable aliases and projection is a pure field read.29cdk_rsx_ashroot::table! { unsafe Players {30 key: u64, row: Player, backend: Nyxus, position: I0,31 indexes: cdk_rsx_ashroot::list![Descending<ByScore>], policy: POLICY,32} }33cdk_rsx_ashroot::database! { Leaderboard { key: u64, tables: [Players], policy: POLICY } }3435mod app {36 use super::*;37 use cdk_rsx_store::Actor;3839 cdk_rsx_store::module_memory! {40 owner = crate::app,41 data = (),42 dependencies = [POLICY];43 pub(crate) struct Memory;44 pub struct Children { board: cdk_rsx_ashroot::Memory<Leaderboard> }45 }4647 pub fn install() -> (Actor<Memory>, Database<Leaderboard>) {48 let actor = Store::default()49 .install(&KEY, |scope| {50 let root = scope.create(&KEY, ())?;51 root.reference().children(scope, &KEY)?.board.attach(52 scope,53 Database::<Leaderboard>::create(scope, &Resources::default()).unwrap(),54 )?;55 Ok(root)56 })57 .unwrap();58 let board = actor59 .read(|scope, root| {60 let reference = root.children(scope, &KEY)?.board.reference(scope)?.unwrap();61 Ok(Database::bind(scope, reference, Resources::default()).unwrap())62 })63 .unwrap();64 (actor, board)65 }66}6768fn main() {69 let (actor, board) = app::install();70 actor71 .with(|scope, _| {72 board73 .insert::<Players>(74 scope,75 1,76 Player {77 name: Text::new("Ada").unwrap(),78 score: 70,79 },80 )81 .unwrap();82 board83 .insert::<Players>(84 scope,85 2,86 Player {87 name: Text::new("Grace").unwrap(),88 score: 90,89 },90 )91 .unwrap();92 // Replacement maintains the descending score index automatically.93 board94 .replace::<Players>(95 scope,96 1,97 Player {98 name: Text::new("Ada").unwrap(),99 score: 100,100 },101 )102 .unwrap();103 Ok(())104 })105 .unwrap();106107 actor108 .read(|scope, _| {109 board110 .read::<Players, _>(scope, |table| {111 let mut leaders = [(Desc(0), 0); 100];112 let count = table.index::<ByScore>().scan(113 (Unbounded, Unbounded),114 None,115 &mut leaders,116 )?;117 for (rank, (_, id)) in leaders[..count].iter().enumerate() {118 table.with_row(*id, |player| {119 let player = player.unwrap();120 println!("{}. {}: {}", rank + 1, player.name.as_str(), player.score);121 })?;122 }123 Ok(())124 })125 .unwrap();126 Ok(())127 })128 .unwrap();129}