makepad/libs/relax/compiler/tests/n01_closure_object_default.rs
Admin b22a714840 makepad run: the Rust JIT (libs/relax) and makepad-remote
The JIT that runs makepad apps from source: lexer, front end, type checker,
lazy borrow checker (Gandalf; `--check-first` opts in), codegen, session and
warm/cold caches keyed by compiler stamp, provided crates bound late,
startup images, live patching. User-facing name is `makepad run` (binary
`makepad`, MAKEPAD_RUN_* env, ~/.makepad-run); libs/relax stays the internal
library name. `makepad check` reports rustc-style diagnostics with rustc
error codes and checks a whole workspace (`--workspace`). Runs on macOS,
Linux, Windows and Android. makepad-remote: one tool schema for apps and the
WM (app/1): grabs, captures, traces, frame profiles and tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-13 12:26:21 +02:00

93 lines
3.3 KiB
Rust

// A closure's annotated return type `-> Result<Box<dyn Executor>, String>`
// has the object type's default lifetime `'static` (Box<T> has no lifetime
// bound: rustc_hir_analysis collect/resolve_bound_vars.rs object lifetime
// defaults), also when the closure is called through a blanket
// `impl<F: Fn(..) -> ..> Factory for F` instance (makepad flow
// ExecutorRegistry::register / ExecutorFactory::create<closure>).
use std::collections::BTreeMap;
use std::sync::Arc;
pub struct Node {
pub type_name: String,
pub weight: u32,
}
pub struct Registry {
pub scale: u32,
}
pub trait Executor {
fn run(&mut self) -> u32;
}
pub trait ExecutorFactory: Send + Sync {
fn create(&self, node: &Node, registry: &Registry) -> Result<Box<dyn Executor>, String>;
}
impl<F> ExecutorFactory for F
where
F: Fn(&Node, &Registry) -> Result<Box<dyn Executor>, String> + Send + Sync,
{
fn create(&self, node: &Node, registry: &Registry) -> Result<Box<dyn Executor>, String> {
self(node, registry)
}
}
#[derive(Default)]
pub struct ExecutorRegistry {
factories: BTreeMap<String, Arc<dyn ExecutorFactory>>,
}
impl ExecutorRegistry {
pub fn register(&mut self, owner: impl Into<String>, factory: impl ExecutorFactory + 'static) -> Result<(), String> {
let owner = owner.into();
if self.factories.contains_key(&owner) {
return Err(format!("executor owner `{owner}` is already registered"));
}
self.factories.insert(owner, Arc::new(factory));
Ok(())
}
pub fn create(&self, owner: &str, node: &Node, registry: &Registry) -> Result<Box<dyn Executor>, String> {
self.factories
.get(owner)
.ok_or_else(|| format!("node `{}` needs unregistered job owner `{owner}`", node.type_name))?
.create(node, registry)
}
}
struct Check {
value: u32,
}
impl Executor for Check {
fn run(&mut self) -> u32 {
self.value
}
}
fn register(executors: &mut ExecutorRegistry) -> Result<(), String> {
executors.register("gen.validate", |node: &Node, _: &Registry| -> Result<Box<dyn Executor>, String> {
match node.type_name.as_str() {
"Validate" | "ValidateBrief" => Ok(Box::new(Check { value: node.weight })),
other => Err(format!("unsupported check node `{other}`")),
}
})?;
executors.register("stage.text", |node: &Node, registry: &Registry| -> Result<Box<dyn Executor>, String> {
Ok(Box::new(Check { value: node.weight * registry.scale }))
})
}
#[test]
fn closure_factories_with_object_returns() {
let mut executors = ExecutorRegistry::default();
register(&mut executors).unwrap();
let registry = Registry { scale: 3 };
let node = Node { type_name: "Validate".into(), weight: 5 };
assert_eq!(executors.create("gen.validate", &node, &registry).unwrap().run(), 5);
assert_eq!(executors.create("stage.text", &node, &registry).unwrap().run(), 15);
let other = Node { type_name: "Other".into(), weight: 1 };
assert_eq!(executors.create("gen.validate", &other, &registry).err().unwrap(), "unsupported check node `Other`");
assert!(executors.create("missing", &node, &registry).is_err());
assert!(executors.register("stage.text", |_: &Node, _: &Registry| -> Result<Box<dyn Executor>, String> { Err(String::new()) }).is_err());
}