Resources

Struct Resources 

Source
pub struct Resources { /* private fields */ }
Expand description

Stores ‘singleton’ data values in the ECS.

A struct storing a hashmap of type id and value pairs. It is used as a resource storage in the ecs.

Implementations§

Source§

impl Resources

Source

pub fn new() -> Self

Creates and returns a new Resources struct using its Default Implementation.

use sceller::prelude::*;
    
struct Health(u8);
    
let mut resources = Resources::new();
Source

pub fn add<T: Any>(&mut self, res: T)

Inserts any value implementing the std::any::Any trait into the instance of the Resources struct provided.

use sceller::prelude::*;
    
struct Health(u8);
    
let mut resources = Resources::new();
resources.add(Health(10));
    
assert_eq!(
     resources.get_ref::<Health>().unwrap().0,
     10
);
Source

pub fn get_ref<T: Any>(&self) -> Result<Ref<'_, T>>

Gets and optionally returns an immutable reference any given resource from the Resources struct provided. Makes use of turbofish syntax (::()) as opposed to concrete variables.

Note: This function internally uses downcast_ref()

use sceller::prelude::*;

struct Health(f32);

let mut resources = Resources::new();
resources.add(Health(42.0));

let extracted_health = resources.get_ref::<Health>().unwrap();
assert_eq!(extracted_health.0, 42.0);
Source

pub fn get_mut<T: Any>(&self) -> Result<RefMut<'_, T>>

Optionally returns a mutable reference to a value of the given type.

use sceller::prelude::*;
    
#[derive(Debug, PartialEq)]
struct Health(i32);
    
let mut resources = Resources::new();
resources.add(Health(123));
    
{
    let mut hp = resources.get_mut::<Health>().unwrap();
    assert_eq!(hp.0, 123);
    hp.0 = 42;
}
    
let hp = resources.get_ref::<Health>().unwrap();
assert_eq!(hp.0, 42);
Source

pub fn delete<T: Any>(&mut self) -> Result<T>

Attempts to delete and return a resource.

use sceller::prelude::*;
    
#[derive(Debug, PartialEq)]
struct Health(i32);
    
let mut resources = Resources::new();
resources.add(Health(123));
    
{
    let hp = resources.get_ref::<Health>().unwrap();
    assert_eq!(hp.0, 123);
}
    
resources.delete::<Health>().unwrap();
assert!(resources.get_ref::<Health>().is_err());

This function tries to return the value that was stored in the Resources struct, and returns None if the type doesn’t exist;

use sceller::prelude::*;
    
#[derive(Debug, PartialEq)]
struct Health(i32);
    
struct No;
    
let mut resources = Resources::new();
resources.add(Health(123));
    
{
    let hp = resources.get_ref::<Health>().unwrap();
    assert_eq!(hp.0, 123);
}

{
    let res = resources.delete::<Health>();
    assert!(res.is_ok());
}

let res = resources.delete::<No>();
assert!(!res.is_ok());

Trait Implementations§

Source§

impl Debug for Resources

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Resources

Source§

fn default() -> Resources

Returns the “default value” for a type. Read more
Source§

impl Display for Resources

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.