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
use crate::{prelude::*, storage::StorageGuard, StorageRef, StorageRefMut};
use owning_ref::{OwningRef, OwningRefMut};
pub trait Resource: 'static {
#[inline] fn register_resource(self) where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
World::register_resource(world, self);
World::unmark_borrow_mut();
}
#[inline] fn resource<'a>() -> OwningRef<StorageRef<'a, Self>, Self> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
let res = World::resource::<Self>(world);
World::unmark_borrow_mut();
res
}
#[inline] fn resource_mut<'a>() -> OwningRefMut<StorageGuard<'a, Self, StorageRefMut<'a, Self>>, Self> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
let res = World::resource_mut::<Self>(world);
World::unmark_borrow_mut();
res
}
#[inline] fn try_resource<'a>() -> Option<OwningRef<StorageRef<'a, Self>, Self>> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
let res = World::try_resource::<Self>(world);
World::unmark_borrow_mut();
res
}
#[inline] fn try_resource_mut<'a>() -> Option<OwningRefMut<StorageGuard<'a, Self, StorageRefMut<'a, Self>>, Self>> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
let res = World::try_resource_mut::<Self>(world);
World::unmark_borrow_mut();
res
}
}
impl<T: 'static + Sized> Resource for T {}
pub trait DefaultResource: Default + 'static {
#[inline] fn resource_or_default<'a>() -> OwningRef<StorageRef<'a, Self>, Self> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
if !World::resource_exists::<Self>(world) {
World::register_resource(world, Self::default());
}
let res = World::resource::<Self>(world);
World::unmark_borrow_mut();
res
}
#[inline] fn resource_mut_or_default<'a>() -> OwningRefMut<StorageGuard<'a, Self, StorageRefMut<'a, Self>>, Self> where Self: Sized {
World::mark_borrow_mut();
let world = unsafe { &mut *WORLD.get() as &mut World };
if !World::resource_exists::<Self>(world) {
World::register_resource(world, Self::default());
}
let res = World::resource_mut::<Self>(world);
World::unmark_borrow_mut();
res
}
}
impl<T: Default + 'static + Sized> DefaultResource for T {}