rustubs/
black_magic.rs

1//! collection of hacks
2use core::slice;
3
4pub unsafe fn make_static(r: &[u8]) -> &'static [u8] {
5	return slice::from_raw_parts(&r[0] as *const u8, r.len());
6}
7
8// an empty struct
9pub struct Void;
10impl Void {
11	pub const fn new() -> Self { Self {} }
12}
13
14/// flush a volatile variable: rust doesn't have a volatile keyword. When a
15/// "const static" variable is expected to be written externally the optimized
16/// code may go wrong.
17pub fn flush<T>(thing: &T) -> T {
18	unsafe { core::ptr::read_volatile(thing as *const T) }
19}
20
21/// macro to declare a external symbol: cast its address into e.g. u64.
22/// useful for getting the address of a tag in asm.
23/// ```
24/// tag:
25///     // something
26/// ```
27///
28/// EXTERN_SYM_PTR!(_TAG, tag)
29///
30/// becomes
31///
32/// ```rust
33/// extern "C" {
34///    fn tag();
35/// }
36/// const TAG: *const () = tag as *const ();
37/// ```
38#[macro_export]
39macro_rules! EXTERN_SYM_PTR {
40	($vis:vis $name:ident ,  $ext:ident) => {
41		extern "C" {
42			fn $ext();
43		}
44		$vis const $name:*const () = $ext as *const ();
45	};
46
47	($vis:vis $name:ident : $ty:ty = $ext:ident) => {
48		extern "C" {
49			fn $ext();
50		}
51		$vis const $name:*const () = $ext as *const () as $ty;
52	};
53
54}