peace_resources/resources.rs
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
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
use crate::resources::ts::{Empty, SetUp};
pub mod ts;
/// Map of all types at runtime. [`resman::Resources`] newtype.
///
/// This augments the any-map functionality of [`resman::Resources`] with type
/// state, so that it is impossible for developers to pass `Resources` to
/// functions that require particular data to have been inserted beforehand.
///
/// For example, `Resources` must be `setup` before any `TryFnSpec`,
/// `ApplyFns`, or `CleanOpSpec` may execute with it.
///
/// # Type Parameters
///
/// * `TS`: The type state of the `Resources` map.
///
/// [`ItemId`]: peace_cfg::ItemId
#[derive(Debug)]
pub struct Resources<TS> {
inner: resman::Resources,
marker: PhantomData<TS>,
}
impl Resources<Empty> {
/// Returns a new `Resources`.
pub fn new() -> Self {
Self {
inner: resman::Resources::new(),
marker: PhantomData,
}
}
}
impl<TS> Resources<TS> {
/// Returns the inner [`resman::Resources`].
pub fn into_inner(self) -> resman::Resources {
self.inner
}
}
impl Default for Resources<Empty> {
fn default() -> Self {
Self::new()
}
}
impl<TS> Deref for Resources<TS> {
type Target = resman::Resources;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<TS> DerefMut for Resources<TS> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
// For `ItemGraph` after resources have been set up.
impl From<Resources<Empty>> for Resources<SetUp> {
fn from(resources: Resources<Empty>) -> Self {
Self {
inner: resources.into_inner(),
marker: PhantomData,
}
}
}