Struct DataHandle

Source
pub struct DataHandle<T: DataObject + 'static> { /* private fields */ }
Expand description

A path to a data resource, resolvable to a cached DataId.

This object serves three purposes:

  • Conveniently resolving a path to an ID but only when resolution is necessary
  • Serializing data that needs to reference a DataObject
  • Keeping a package open so it doesn’t get auto-unloaded.

If placing inside a DataObject struct, take care not to have a self-referencing type (i.e. a DataHandle<T> inside a T) or a type cycle (i.e. a DataHandle<T> inside a U and a DataHandle<U> inside a T). Otherwise the auto-unload mechanism may not be able to unload the package the object is from.

Implementations§

Source§

impl<T: DataObject + 'static> DataHandle<T>

Source

pub fn null() -> DataHandle<T>

Creates a new DataHandle that points to nothing. Resolving this data handle will fail until it is given a valid path, such as through set_path().

Source

pub fn with_path(package: &str, path: &str) -> DataHandle<T>

Creates a new DataHandle with the given resource path. The data ID will be unresolved. You will need to call resolve() for the data ID to point to a valid resource.

Source

pub fn clone_without_path(&self) -> DataHandle<T>

Clones the given DataHandle but only retains the ID, erasing the path. This is useful for instances where you want to store a lot of handles but don’t need the path, to cut down on memory usage.

Source

pub fn set_path(&mut self, package: &str, path: &str)

Changes the resource path. The data ID will reset to be unresolved. You will need to call resolve() for the data ID to point to a valid resource.

Source

pub fn package(&self) -> &String

Returns the package name part of the resource path

Source

pub fn path(&self) -> &String

Returns the filepath to the resource

Source

pub fn id(&self) -> DataId<T>

Returns the data ID. You will need to call resolve() beforehand for the data ID to point to a valid resource.

Source

pub fn resolved(&self) -> bool

Returns true if the path has been resolved via a call to resolve().

Source

pub fn resolve( &self, data_res: &mut DataStore<T>, load_error: LoadErrorMode, ) -> DataId<T>

Resolves the data ID using the given DataStore. Returns the resolved data ID, which is cached inside the object. If load_error is Fatal, this function will panic if the load fails. Otherwise it will simply return a null data ID.

Source

pub fn get<'a, 'b: 'a>( &self, data_res: &'b mut DataStore<T>, load_error: LoadErrorMode, ) -> Option<&'a T>

Returns a reference to data object in the store, if it exists. If the data object is not resolved, then resolve() will be called, and then the data object reference returned, if available.

Source

pub fn get_mut<'a, 'b: 'a>( &self, data_res: &'b mut DataStore<T>, load_error: LoadErrorMode, ) -> Option<&'a mut T>

Returns a mutable reference to data object in the store, if it exists. If the data object is not resolved, then resolve() will be called, and then the data object reference returned, if available.

Source

pub fn get_if_resolved<'a, 'b: 'a>( &self, data_res: &'b DataStore<T>, ) -> Option<&'a T>

Returns a reference to data object in the store, if it exists. If the data object is not resolved, returns None instead of attempting to resolve.

Source

pub fn get_mut_if_resolved<'a, 'b: 'a>( &self, data_res: &'b mut DataStore<T>, ) -> Option<&'a mut T>

Returns a mutable reference to data object in the store, if it exists. If the data object is not resolved, returns None instead of attempting to resolve.

Source

pub fn resolve_if_loaded(&self, data_res: &DataStore<T>) -> DataId<T>

Resolves the data ID using the given DataStore. Returns the resolved data ID, which is cached inside the object. If the data is not yet loaded, returns a null ID.

Trait Implementations§

Source§

impl<T: DataObject + 'static> Clone for DataHandle<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: DataObject + 'static> Debug for DataHandle<T>

Source§

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

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

impl<T: DataObject + 'static> Default for DataHandle<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for DataHandle<T>

§

impl<T> !RefUnwindSafe for DataHandle<T>

§

impl<T> !Send for DataHandle<T>

§

impl<T> !Sync for DataHandle<T>

§

impl<T> Unpin for DataHandle<T>
where T: Unpin,

§

impl<T> UnwindSafe for DataHandle<T>
where T: 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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.