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

40 lines
1.2 KiB
Rust

// A reference-to-raw cast keeps an existing trait object's data and vtable.
struct ErasedFn(*const (dyn Fn(usize) -> usize + Sync));
fn erase(f: &'static (dyn Fn(usize) -> usize + Sync)) -> ErasedFn {
ErasedFn(f as *const _)
}
#[test]
fn inferred_dyn_raw_cast() {
let f: &'static (dyn Fn(usize) -> usize + Sync) = &|x| x + 3;
let p = erase(f);
assert_eq!(unsafe { (*p.0)(4) }, 7);
let explicit = f as *const (dyn Fn(usize) -> usize + Sync);
assert_eq!(unsafe { (*explicit)(5) }, 8);
}
trait Value {
fn value(&self) -> usize;
}
impl Value for usize {
fn value(&self) -> usize { *self }
}
#[test]
fn inferred_thin_pointer_still_unsizes() {
let value = 9usize;
let ptr: *const dyn Value = &value as *const _;
assert_eq!(unsafe { (*ptr).value() }, 9);
}
#[test]
fn mutable_dyn_pointer_and_auto_trait_erasure() {
let mut value = 11usize;
let object: &mut (dyn Value + Send) = &mut value;
let ptr: *mut (dyn Value + Send) = object as *mut _;
assert_eq!(unsafe { (*ptr).value() }, 11);
let object: &(dyn Value + Send) = &value;
let ptr: *const dyn Value = object as *const _;
assert_eq!(unsafe { (*ptr).value() }, 11);
}