Struct all_is_cubes::universe::Universe
source · pub struct Universe {
pub whence: Arc<dyn WhenceUniverse>,
/* private fields */
}
Expand description
A collection of named objects which can refer to each other via URef
,
and which are simulated at the same time steps.
Thread-safety caveat: See the documentation on avoiding deadlock.
A Universe
consists of:
- members of various types, which may be identified using
Name
s orURef
s. Behavior
s that modify theUniverse
itself.- A
time::Clock
defining how time is considered to pass in it. - A
WhenceUniverse
defining where its data is persisted, if anywhere. - A
UniverseId
unique within this process.
§Serialization stability warning
This type implements serde::Serialize
and serde::Deserialize
, but serialization
support is still experimental (as is the game data model in general). We do not guarantee that future versions of all-is-cubes
will be able to deserialize data which is serialized by this version.
Additionally, the serialization schema is designed with serde_json
in mind. It is not
guaranteed that using a different data format crate, which may use a different subset of
the information exposed via serde::Serialize
, will produce stable results.
Fields§
§whence: Arc<dyn WhenceUniverse>
Where the contents of `self came from, and where they might be able to be written back to.
For universes created by Universe::new()
, this is equal to Arc::new(())
.
Implementations§
source§impl Universe
impl Universe
sourcepub fn get_any(&self, name: &Name) -> Option<Box<dyn URefErased>>
pub fn get_any(&self, name: &Name) -> Option<Box<dyn URefErased>>
Returns a URef
for the object in this universe with the given name,
regardless of its type, or None
if there is none.
This is a dynamically-typed version of Universe::get()
.
sourcepub fn get_default_character(&self) -> Option<URef<Character>>
pub fn get_default_character(&self) -> Option<URef<Character>>
Returns the character named "character"
.
This is currently assumed to be the “player character” for this universe.
TODO: this is a temporary shortcut to be replaced with something with more nuance (e.g. we might have temporary characters for editing purposes, which are ‘current’ but not ‘primary’).
sourcepub fn universe_id(&self) -> UniverseId
pub fn universe_id(&self) -> UniverseId
sourcepub fn step<I: Instant>(
&mut self,
paused: bool,
deadline: Deadline<I>
) -> UniverseStepInfo
pub fn step<I: Instant>( &mut self, paused: bool, deadline: Deadline<I> ) -> UniverseStepInfo
Advance time for all members.
deadline
is when to stop computing flexible things such as light transport.
sourcepub fn clock(&self) -> Clock
pub fn clock(&self) -> Clock
Returns the time::Clock
that is used to advance time when step()
is called.
sourcepub fn insert_anonymous<T>(&mut self, value: T) -> URef<T>where
Self: UniverseOps<T>,
T: UniverseMember,
pub fn insert_anonymous<T>(&mut self, value: T) -> URef<T>where
Self: UniverseOps<T>,
T: UniverseMember,
Inserts a new object without giving it a specific name, and returns a reference to it.
sourcepub fn get<T>(&self, name: &Name) -> Option<URef<T>>where
Self: UniverseOps<T>,
T: UniverseMember,
pub fn get<T>(&self, name: &Name) -> Option<URef<T>>where
Self: UniverseOps<T>,
T: UniverseMember,
sourcepub fn insert<T>(
&mut self,
name: Name,
value: T
) -> Result<URef<T>, InsertError>where
Self: UniverseOps<T>,
T: UniverseMember,
pub fn insert<T>(
&mut self,
name: Name,
value: T
) -> Result<URef<T>, InsertError>where
Self: UniverseOps<T>,
T: UniverseMember,
Inserts a new object with a specific name.
Returns an error if the name is already in use.
sourcepub fn iter_by_type<T>(&self) -> UniverseIter<'_, T> ⓘwhere
Self: UniverseOps<T>,
T: UniverseMember,
pub fn iter_by_type<T>(&self) -> UniverseIter<'_, T> ⓘwhere
Self: UniverseOps<T>,
T: UniverseMember,
Iterate over all of the objects of type T
.
Note that this includes anonymous objects.
use all_is_cubes::block::{Block, BlockDef};
use all_is_cubes::content::make_some_blocks;
use all_is_cubes::universe::{Name, Universe, URef};
let mut universe = Universe::new();
let [block_1, block_2] = make_some_blocks();
universe.insert(Name::from("b1"), BlockDef::new(block_1.clone()));
universe.insert(Name::from("b2"), BlockDef::new(block_2.clone()));
let mut found_blocks = universe.iter_by_type()
.map(|(name, value): (Name, URef<BlockDef>)| {
(name, value.read().unwrap().block().clone())
})
.collect::<Vec<_>>();
found_blocks.sort_by_key(|(name, _)| name.to_string());
assert_eq!(
found_blocks,
vec![Name::from("b1"), Name::from("b2")].into_iter()
.zip(vec![block_1, block_2])
.collect::<Vec<_>>(),
);
Trait Implementations§
source§impl BehaviorHost for Universe
impl BehaviorHost for Universe
§type Attachment = ()
type Attachment = ()
source§impl<'de> Deserialize<'de> for Universe
impl<'de> Deserialize<'de> for Universe
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl Transaction<Universe> for UniverseTransaction
impl Transaction<Universe> for UniverseTransaction
§type CommitCheck = UniverseCommitCheck
type CommitCheck = UniverseCommitCheck
Transaction::check
to Transaction::commit
.
This may be used to pass precalculated values to speed up the commit phase,
or even lock guards or similar, but also makes it slightly harder to accidentally
call commit
without check
.§type Output = Infallible
type Output = Infallible
Transaction::commit()
or Transaction::execute()
.
Each commit may produce any number of these messages. Read moresource§fn check(
&self,
target: &Universe
) -> Result<Self::CommitCheck, PreconditionFailed>
fn check( &self, target: &Universe ) -> Result<Self::CommitCheck, PreconditionFailed>
source§fn commit(
&self,
target: &mut Universe,
checks: Self::CommitCheck,
outputs: &mut dyn FnMut(Self::Output)
) -> Result<(), CommitError>
fn commit( &self, target: &mut Universe, checks: Self::CommitCheck, outputs: &mut dyn FnMut(Self::Output) ) -> Result<(), CommitError>
check
value should have
been created by a prior call to Transaction::commit
. Read moresource§impl Transactional for Universe
impl Transactional for Universe
§type Transaction = UniverseTransaction
type Transaction = UniverseTransaction
Self
.Auto Trait Implementations§
impl !RefUnwindSafe for Universe
impl Send for Universe
impl Sync for Universe
impl Unpin for Universe
impl !UnwindSafe for Universe
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.