Struct dungeon_cell::DungeonCore
source · pub struct DungeonCore<const SIZE: usize, const ALIGN: u8 = 3>where
Alignment<ALIGN>: ValidAlignment,{ /* private fields */ }Expand description
Core of a dungeon primitive.
Any static type with size less than or equal to SIZE bytes and alignment
requirements less than or equal to 2^ALIGN bytes can be stored in the core.
By default, this type uses a 8 byte alignment as that
is the most common alignment for x64 machines.
Implementations§
source§impl<const SIZE: usize, const ALIGN: u8> DungeonCore<SIZE, ALIGN>where
Alignment<ALIGN>: ValidAlignment,
impl<const SIZE: usize, const ALIGN: u8> DungeonCore<SIZE, ALIGN>where
Alignment<ALIGN>: ValidAlignment,
sourcepub fn new() -> Self
pub fn new() -> Self
Create a empty dungeon core.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
let mut x = DungeonCore::<24>::new();
x.store(1234_i32);
dbg!(x.take::<i32>());
x.store(1.234_f32);
dbg!(x.take::<f32>());
x.store(1234_i32);
dbg!(x.take::<f32>());
x.store("hello");
dbg!(x.take::<&'static str>());
x.store("world".to_owned());
dbg!(x.take::<String>());
x.store("a".to_owned());
x.store("b".to_owned());
dbg!(x.take::<String>());
}sourcepub fn store<T: 'static>(&mut self, value: T)
pub fn store<T: 'static>(&mut self, value: T)
Store a value of type T.
If T can not be stored by this dungeon core do to
size or alignment requirements a compiler error or
runtime panic will be generated. This behavior is
controlled by the assert_runtime feature.
If the dungeon core contains a value already it will be dropped.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
let mut x = DungeonCore::<24>::new();
x.store(1234_i32);
dbg!(x.take::<i32>());
x.store(1.234_f32);
dbg!(x.take::<f32>());
x.store(1234_i32);
dbg!(x.take::<f32>());
x.store("hello");
dbg!(x.take::<&'static str>());
x.store("world".to_owned());
dbg!(x.take::<String>());
x.store("a".to_owned());
x.store("b".to_owned());
dbg!(x.take::<String>());
}sourcepub fn take<T: 'static>(&mut self) -> Option<T>
pub fn take<T: 'static>(&mut self) -> Option<T>
Gain ownership of the value inside the dungeon core.
This will leave the dungeon core empty if the value is of
type T. If the dungeon core is empty or has another type’s
value stored in it then a None will be returned.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
let mut x = DungeonCore::<24>::new();
x.store(1234_i32);
dbg!(x.take::<i32>());
x.store(1.234_f32);
dbg!(x.take::<f32>());
x.store(1234_i32);
dbg!(x.take::<f32>());
x.store("hello");
dbg!(x.take::<&'static str>());
x.store("world".to_owned());
dbg!(x.take::<String>());
x.store("a".to_owned());
x.store("b".to_owned());
dbg!(x.take::<String>());
}sourcepub fn replace<T: 'static, U: 'static>(&mut self, value: T) -> Option<U>
pub fn replace<T: 'static, U: 'static>(&mut self, value: T) -> Option<U>
Replace the current value with a T.
The current value is returned if the dungeon core is not empty,
and the current stored value is of type U.
If T can not be stored by this dungeon core do to
size or alignment requirements a compiler error or
runtime panic will be generated. This behavior is
controlled by the assert_runtime feature.
This is slightly more efficient than a take() followed be
a `store()
sourcepub fn try_borrow<T: 'static>(&self) -> Option<&T>
pub fn try_borrow<T: 'static>(&self) -> Option<&T>
Try to borrow the stored value.
If the dungeon core is empty or the stored value is
not of type T then None will be returned.
sourcepub fn try_borrow_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn try_borrow_mut<T: 'static>(&mut self) -> Option<&mut T>
Try to mutably borrow the stored value.
If the dungeon core is empty or the stored value is
not of type T then None will be returned.
source§impl<const SIZE: usize, const ALIGN: u8> DungeonCore<SIZE, ALIGN>where
Alignment<ALIGN>: ValidAlignment,
impl<const SIZE: usize, const ALIGN: u8> DungeonCore<SIZE, ALIGN>where
Alignment<ALIGN>: ValidAlignment,
Unsafe methods.
sourcepub unsafe fn store_unchecked<T: 'static>(&mut self, value: T)
pub unsafe fn store_unchecked<T: 'static>(&mut self, value: T)
Unchecked form of store.
If the dungeon core is not empty, then the stored value will be forgotten.
Safety
T must be storable in SIZE bytes with alignment of 2^ALIGN bytes.