cve_rs/
references.rs

1//! Reimplementations of [`std::ptr::null()`] and [`std::ptr::null_mut()`], with safe code only.
2//! Relies on [`crate::transmute`] under the hood.
3
4/// Equivalent to [`std::ptr::null()`], but returns a null reference instead.
5pub fn null<'a, T: 'static>() -> &'a T {
6	crate::transmute(0usize)
7}
8/// Equivalent to [`std::ptr::null_mut()`], but returns a mutable null reference instead.
9pub fn null_mut<'a, T: 'static>() -> &'a mut T {
10	crate::transmute(0usize)
11}
12
13/// Not allocate an object. The returned reference is always invalid.
14///
15/// **Note:** It turns out that `null` is a valid memory address in WASM.
16/// So here we use the maximum address instead.
17pub fn not_alloc<'a, T: 'static>() -> &'a mut T {
18	crate::transmute(usize::MAX)
19}