makepad/libs/relax/compiler/tests/dyn_fn_place.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

31 lines
1.1 KiB
Rust

// F36: calls on any place of type dyn Fn* (through a raw-pointer deref too) go through the
// vtable; fn pointers are Clone (builtin impls)
struct S { k: u32 }
impl S {
fn run(&self, g: *const dyn Fn(&S, u32) -> u32, x: u32) -> u32 { unsafe { (*g)(self, x) } }
}
#[test]
fn raw_dyn_fn_call() {
let add = 5u32;
let f: Box<dyn Fn(&S, u32) -> u32> = Box::new(move |s: &S, x: u32| s.k * x + add);
let p: *const dyn Fn(&S, u32) -> u32 = &*f;
let s = S { k: 3 };
assert_eq!(s.run(p, 2), 11);
assert_eq!((*f)(&s, 1), 8);
let r: &dyn Fn(&S, u32) -> u32 = &*f;
assert_eq!((*r)(&s, 0), 5);
let bm: Box<dyn FnMut(u32) -> u32> = Box::new(move |x: u32| x + add);
let m: *mut dyn FnMut(u32) -> u32 = Box::into_raw(bm);
assert_eq!(unsafe { (*m)(1) }, 6);
unsafe { drop(Box::from_raw(m)) };
}
fn tw(x: u32) -> u32 { x * 2 }
#[test]
fn fn_ptr_clone() {
let f: fn(u32) -> u32 = tw;
let g = f.clone();
let v = vec![f, g];
let mut w: Vec<fn(u32) -> u32> = Vec::new();
w.extend_from_slice(&v);
assert_eq!(w[1](4), 8);
}