pub struct Persistent<R: Resource + Serialize + DeserializeOwned> { /* private fields */ }
Expand description

A persistent resource.

Persistent resources are Bevy resources which are automatically synchronized with the disk.

They require a name for logging, a path to be saved to and loaded from, a storage format, and a default resource in case the persistent resource is created for the first time.

They are synchronized with the disk from the moment of their creation.

Implementations§

source§

impl<R: Resource + Serialize + DeserializeOwned> Persistent<R>

source

pub fn builder() -> PersistentBuilder<R>

Creates a persistent resource builder.

source

pub fn new( name: impl ToString, format: StorageFormat, storage: Storage, loaded: bool, default: R, revertible: bool, revert_to_default_on_deserialization_errors: bool ) -> Result<Persistent<R>, PersistenceError>

Creates a persistent resource.

§Panics

Panics if revert_to_default_on_deserialization_errors is set to true but revertible is set to false.

source§

impl<R: Resource + Serialize + DeserializeOwned> Persistent<R>

source

pub fn name(&self) -> &str

Gets the name of the resource.

source

pub fn format(&self) -> StorageFormat

Gets the storage format of the resource.

source

pub fn storage(&self) -> &Storage

Gets the storage of the resource.

source

pub fn is_revertible(&self) -> bool

Gets if the resource is revertible.

source

pub fn is_loaded(&self) -> bool

Gets if the resource is loaded.

source

pub fn is_unloaded(&self) -> bool

Gets if the resource is unloaded.

source

pub fn get(&self) -> &R

Gets the resource.

§Panics

Panics if the resource is unloaded.

source

pub fn get_mut(&mut self) -> &mut R

Gets the resource mutably.

§Panics

Panics if the resource is unloaded.

source

pub fn try_get(&self) -> Option<&R>

Tries to get the resource.

source

pub fn try_get_mut(&mut self) -> Option<&mut R>

Tries to get the resource mutably.

source§

impl<R: Resource + Serialize + DeserializeOwned> Persistent<R>

source

pub fn set(&mut self, new_resource: R) -> Result<(), PersistenceError>

Sets the resource.

Changes are synchronized with the underlying storage immediately.

source

pub fn update( &mut self, updater: impl Fn(&mut R) ) -> Result<(), PersistenceError>

Updates the resource.

Changes are synchronized with the underlying storage immediately.

§Panics

Panics if the resource is unloaded.

source

pub fn unload(&mut self) -> Result<(), PersistenceError>

Unloads the resource from memory.

Changes are synchronized with the underlying storage before unloading.

§Panics

Panics if the resource is unloaded.

source

pub fn unload_without_persisting(&mut self)

Unloads the resource from memory immediately.

Changes are not synchronized with the underlying storage before unloading.

source

pub fn reload(&mut self) -> Result<(), PersistenceError>

Reloads the resource from the underlying storage.

If reloading fails, the underlying resource is kept untouched.

source

pub fn revert_to_default(&mut self) -> Result<(), PersistenceError>

Reverts the resource to it’s default value.

Loaded status is kept upon reloading.

§Panics

Panics if the resource is not revertible.

source

pub fn revert_to_default_in_memory(&mut self) -> Result<(), PersistenceError>

Reverts the resource to it’s default value only in memory, not persistent storage.

§Panics

Panics if the resource is not revertible.

source§

impl<R: Resource + Serialize + DeserializeOwned> Persistent<R>

source

pub fn persist(&self) -> Result<(), PersistenceError>

Writes the resource to the underlying storage.

§Panics

Panics if the resource is unloaded.

Trait Implementations§

source§

impl<R: Resource + Serialize + DeserializeOwned> Component for Persistent<R>
where Self: Send + Sync + 'static,

§

type Storage = TableStorage

A marker type indicating the storage type used for this component. This must be either [TableStorage] or [SparseStorage].
source§

impl<R: Debug + Resource + Serialize + DeserializeOwned> Debug for Persistent<R>

source§

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

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

impl<R: Resource + Serialize + DeserializeOwned> Deref for Persistent<R>

§

type Target = R

The resulting type after dereferencing.
source§

fn deref(&self) -> &R

Dereferences the value.
source§

impl<R: Resource + Serialize + DeserializeOwned> DerefMut for Persistent<R>

source§

fn deref_mut(&mut self) -> &mut R

Mutably dereferences the value.
source§

impl<R: Resource + Serialize + DeserializeOwned> Resource for Persistent<R>
where Self: Send + Sync + 'static,

Auto Trait Implementations§

§

impl<R> RefUnwindSafe for Persistent<R>
where R: RefUnwindSafe,

§

impl<R> Send for Persistent<R>

§

impl<R> Sync for Persistent<R>

§

impl<R> Unpin for Persistent<R>
where R: Unpin,

§

impl<R> UnwindSafe for Persistent<R>
where R: UnwindSafe,

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
§

impl<C> Bundle for C
where C: Component,

§

fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId) )

§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,

§

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

§

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

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

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

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

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

§

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

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<C> DynamicBundle for C
where C: Component,

§

fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more