MCPcopy Index your code
hub / github.com/RustPython/RustPython / zst_ref_out_of_thin_air

Function zst_ref_out_of_thin_air

crates/vm/src/function/builtin.rs:53–66  ·  view source on GitHub ↗
(x: T)

Source from the content-addressed store, hash-verified

51}
52
53const fn zst_ref_out_of_thin_air<T: 'static>(x: T) -> &'static T {
54 // if T is zero-sized, there's no issue forgetting it - even if it does have a Drop impl, it
55 // would never get called anyway if we consider this semantically a Box::leak(Box::new(x))-type
56 // operation. if T isn't zero-sized, we don't have to worry about it because we'll fail to compile.
57 core::mem::forget(x);
58 const {
59 if core::mem::size_of::<T>() != 0 {
60 panic!("can't use a non-zero-sized type here")
61 }
62 // SAFETY: we just confirmed that T is zero-sized, so we can
63 // pull a value of it out of thin air.
64 unsafe { core::ptr::NonNull::<T>::dangling().as_ref() }
65 }
66}
67
68/// Get the STATIC_FUNC of the passed function. The same
69/// requirements of zero-sized-ness apply, see that documentation for details.

Callers 2

static_funcFunction · 0.85
static_raw_funcFunction · 0.85

Calls 2

forgetFunction · 0.85
as_refMethod · 0.45

Tested by

no test coverage detected