mc_core/util/
mod.rs

1use std::mem::ManuallyDrop;
2
3mod version;
4mod packed;
5mod palette;
6mod cache;
7mod sync;
8mod nbt_ext;
9mod geom;
10
11pub use cache::*;
12pub use packed::*;
13pub use palette::*;
14pub use sync::*;
15pub use version::*;
16pub use nbt_ext::NbtExt;
17pub use geom::*;
18
19
20// pub type Rect<T, const X: usize, const Z: usize> = [[T; Z]; X];
21// pub type Cube<T, const X: usize, const Y: usize, const Z: usize> = [[[T; Z]; Y]; X];
22
23
24pub unsafe fn cast_vec<Src, Dst>(src: Vec<Src>) -> Vec<Dst> {
25    debug_assert_eq!(std::mem::size_of::<Src>(), std::mem::size_of::<Dst>());
26    debug_assert_eq!(std::mem::align_of::<Src>(), std::mem::align_of::<Dst>());
27    let mut src = ManuallyDrop::new(src);
28    let (ptr, len, cap) = (src.as_mut_ptr(), src.len(), src.capacity());
29    Vec::from_raw_parts(ptr as *mut Dst, len, cap)
30}
31
32pub fn cast_vec_ref_to_ptr<T>(src: Vec<&'static T>) -> Vec<*const T> {
33    unsafe { cast_vec(src) }
34}
35
36
37/// A macro used internally to MinecraftRS to count the number of tokens you give
38/// to the macro and return the count as usize.
39#[macro_export]
40macro_rules! count {
41    () => (0usize);
42    ($x0:tt $x1:tt $x2:tt $x3:tt $x4:tt $x5:tt $x6:tt $x7:tt
43     $x8:tt $x9:tt $x10:tt $x11:tt $x12:tt $x13:tt $x14:tt $x15:tt $($xs:tt)*) => (16usize + $crate::count!($($xs)*));
44    ($x0:tt $x1:tt $x2:tt $x3:tt $x4:tt $x5:tt $x6:tt $x7:tt $($xs:tt)*) => (8usize + $crate::count!($($xs)*));
45    ($x0:tt $x1:tt $x2:tt $x3:tt $($xs:tt)*) => (4usize + $crate::count!($($xs)*));
46    ($x0:tt $x1:tt $($xs:tt)*) => (2usize + $crate::count!($($xs)*));
47    ($x0:tt $($xs:tt)*) => (1usize + $crate::count!($($xs)*));
48}
49
50
51#[macro_export]
52macro_rules! debug {
53    ($($arg:tt)*) => {
54        #[cfg(debug_assertions)]
55        {
56            print!("[{}:{}] ", file!(), line!());
57            println!($($arg)*);
58        }
59        #[cfg(not(debug_assertions))]
60        {}
61    };
62}