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:

§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

source

pub fn new() -> Self

Constructs an empty Universe.

source

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().

source

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’).

source

pub fn universe_id(&self) -> UniverseId

Returns a unique identifier for this particular Universe (within this memory space).

It may be used to determine whether a given URef belongs to this universe or not.

source

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.
source

pub fn clock(&self) -> Clock

Returns the time::Clock that is used to advance time when step() is called.

source

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.

source

pub fn get<T>(&self, name: &Name) -> Option<URef<T>>
where Self: UniverseOps<T>, T: UniverseMember,

Translates a name for an object of type T into a URef for it, which allows borrowing the actual object.

Returns None if no object exists for the name.

source

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.

source

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<_>>(),
);
source

pub fn gc(&mut self)

Delete all anonymous members which have no references to them.

This may happen at any time during operations of the universe; calling this method merely ensures that it happens now and not earlier.

Trait Implementations§

source§

impl BehaviorHost for Universe

§

type Attachment = ()

Additional data about “where” the behavior is attached to the host; what part of the host should be affected by the behavior.
source§

impl Debug for Universe

source§

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

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

impl Default for Universe

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Universe

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Universe

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl Transaction<Universe> for UniverseTransaction

§

type CommitCheck = UniverseCommitCheck

Type of a value passed from 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

The results of a Transaction::commit() or Transaction::execute(). Each commit may produce any number of these messages. Read more
source§

fn check( &self, target: &Universe ) -> Result<Self::CommitCheck, PreconditionFailed>

Checks whether the target’s current state meets the preconditions and returns Err if it does not. (TODO: Informative error return type.) Read more
source§

fn commit( &self, target: &mut Universe, checks: Self::CommitCheck, outputs: &mut dyn FnMut(Self::Output) ) -> Result<(), CommitError>

Perform the mutations specified by this transaction. The check value should have been created by a prior call to Transaction::commit. Read more
source§

fn execute( &self, target: &mut T, outputs: &mut dyn FnMut(Self::Output) ) -> Result<(), ExecuteError>

Convenience method to execute a transaction in one step. Implementations should not need to override this. Equivalent to: Read more
source§

impl Transactional for Universe

§

type Transaction = UniverseTransaction

The type of transaction which should be used with Self.

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> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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.

§

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

§

type EqTo = T

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

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

§

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>,

§

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.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,