-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathlib.rs
More file actions
106 lines (89 loc) · 2.22 KB
/
lib.rs
File metadata and controls
106 lines (89 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#[macro_use]
mod utils;
#[macro_use]
pub mod component;
pub mod block;
pub mod components;
pub mod entities;
pub mod entity;
pub mod entity_init;
pub mod events;
pub mod tablist;
use std::marker::PhantomData;
use bytemuck::{Pod, Zeroable};
pub use component::{Component, HostComponent};
pub use entity::EntityId;
/// Wrapper type that enforces 64-bit pointers
/// for all targets. Needed for ABI compatibility
/// between WASM-compiled and native-compiled plugins.
#[derive(Debug, PartialEq, Eq, Zeroable)]
#[repr(transparent)]
pub struct Pointer<T> {
ptr: u64,
_marker: PhantomData<*const T>,
}
impl<T> Clone for Pointer<T> {
fn clone(&self) -> Self {
Self {
ptr: self.ptr,
_marker: self._marker,
}
}
}
impl<T> Copy for Pointer<T> {}
impl<T> Pointer<T> {
pub fn new(ptr: *const T) -> Self {
Self {
ptr: ptr as usize as u64,
_marker: PhantomData,
}
}
pub fn as_ptr(self) -> *const T {
self.ptr as usize as *const T
}
}
impl<T> From<*const T> for Pointer<T> {
fn from(ptr: *const T) -> Self {
Self::new(ptr)
}
}
// SAFETY: Pointer<T> contains a u64 regardless
// of T. bytemuck won't derive Pod for generic
// types because it cannot guarantee this.
unsafe impl<T: 'static> Pod for Pointer<T> {}
/// Wrapper type that enforces 64-bit pointers
/// for all targets. Needed for ABI compatibility
/// between WASM-compiled and native-compiled plugins.
#[derive(Debug, PartialEq, Eq, Zeroable)]
#[repr(transparent)]
pub struct PointerMut<T> {
ptr: u64,
_marker: PhantomData<*mut T>,
}
impl<T> Clone for PointerMut<T> {
fn clone(&self) -> Self {
Self {
ptr: self.ptr,
_marker: self._marker,
}
}
}
impl<T> Copy for PointerMut<T> {}
impl<T> PointerMut<T> {
pub fn new(ptr: *mut T) -> Self {
Self {
ptr: ptr as usize as u64,
_marker: PhantomData,
}
}
pub fn as_mut_ptr(self) -> *mut T {
self.ptr as usize as *mut T
}
}
impl<T> From<*mut T> for PointerMut<T> {
fn from(ptr: *mut T) -> Self {
Self::new(ptr)
}
}
// SAFETY: see impl Pod for Pointer.
unsafe impl<T: 'static> Pod for PointerMut<T> {}