pub struct ResourceManager { /* private fields */ }
Expand description
Resource manager controls loading and lifetime of resource in the engine. Resource manager can hold resources of arbitrary types via type erasure mechanism.
§Built-in Resources
Built-in resources are special kinds of resources, whose data is packed in the executable (i.e. via
include_bytes
macro). Such resources reference the data that cannot be “loaded” from external
source. To support such kind of resource the manager provides built_in_resources
hash map where
you can register your own built-in resource and access existing ones.
§Internals
It is a simple wrapper over ResourceManagerState
that can be shared (cloned). In other words,
it is just a strong reference to the inner state.
Implementations§
Source§impl ResourceManager
impl ResourceManager
Sourcepub fn new(task_pool: Arc<TaskPool>) -> ResourceManager
pub fn new(task_pool: Arc<TaskPool>) -> ResourceManager
Creates a resource manager with default settings and loaders.
Sourcepub fn state(&self) -> MutexGuard<'_, RawMutex, ResourceManagerState>
pub fn state(&self) -> MutexGuard<'_, RawMutex, ResourceManagerState>
Returns a guarded reference to internal state of resource manager.
Sourcepub fn resource_io(&self) -> Arc<dyn ResourceIo>
pub fn resource_io(&self) -> Arc<dyn ResourceIo>
Returns the ResourceIo used by this resource manager
Sourcepub fn request<T>(&self, path: impl AsRef<Path>) -> Resource<T> ⓘwhere
T: TypedResourceData,
pub fn request<T>(&self, path: impl AsRef<Path>) -> Resource<T> ⓘwhere
T: TypedResourceData,
Requests a resource of the given type located at the given path. This method is non-blocking, instead it immediately returns the typed resource wrapper. Loading of the resource is managed automatically in a separate thread (or thread pool) on PC, and JS micro-task (the same thread) on WebAssembly.
§Sharing
If the resource at the given path is already was requested (no matter in which state the actual resource is), this method will return the existing instance. This way the resource manager guarantees that the actual resource data will be loaded once, and it can be shared.
§Waiting
If you need to wait until the resource is loaded, use .await
on the result of the method. Every resource
implements Future
trait and can be used in async
contexts.
§Resource state
Keep in mind, that the resource itself is a small state machine. It could be in three main states:
ResourceState::Pending
- a resource is in the queue to load or still loading.ResourceState::LoadError
- a resource is failed to load.ResourceState::Ok
- a resource is successfully loaded.
Actual resource state can be fetched by Resource::state
method. If you know for sure that the resource
is already loaded, then you can use Resource::data_ref
to obtain a reference to the actual resource data.
Keep in mind, that this method will panic if the resource non in Ok
state.
§Panic
This method will panic, if type UUID of T
does not match the actual type UUID of the resource. If this
is undesirable, use Self::try_request
instead.
Sourcepub fn try_request<T>(&self, path: impl AsRef<Path>) -> Option<Resource<T>>where
T: TypedResourceData,
pub fn try_request<T>(&self, path: impl AsRef<Path>) -> Option<Resource<T>>where
T: TypedResourceData,
The same as Self::request
, but returns None
if type UUID of T
does not match the actual type UUID
of the resource.
§Panic
This method does not panic.
Sourcepub fn request_untyped<P>(&self, path: P) -> UntypedResource ⓘ
pub fn request_untyped<P>(&self, path: P) -> UntypedResource ⓘ
Same as Self::request
, but returns untyped resource.
Sourcepub fn register<P, F>(
&self,
resource: UntypedResource,
path: P,
on_register: F,
) -> Result<(), ResourceRegistrationError>
pub fn register<P, F>( &self, resource: UntypedResource, path: P, on_register: F, ) -> Result<(), ResourceRegistrationError>
Saves given resources in the specified path and registers it in resource manager, so it will be accessible through it later.
Sourcepub async fn move_resource(
&self,
resource: UntypedResource,
new_path: impl AsRef<Path>,
working_directory: impl AsRef<Path>,
filter: impl FnMut(&UntypedResource) -> bool,
) -> Result<(), FileLoadError>
pub async fn move_resource( &self, resource: UntypedResource, new_path: impl AsRef<Path>, working_directory: impl AsRef<Path>, filter: impl FnMut(&UntypedResource) -> bool, ) -> Result<(), FileLoadError>
Attempts to move a resource from its current location to the new path.
Sourcepub async fn reload_resources(&self)
pub async fn reload_resources(&self)
Reloads all loaded resources. Normally it should never be called, because it is very heavy method! This method is asynchronous, it uses all available CPU power to reload resources as fast as possible.
Trait Implementations§
Source§impl Clone for ResourceManager
impl Clone for ResourceManager
Source§fn clone(&self) -> ResourceManager
fn clone(&self) -> ResourceManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for ResourceManager
impl !RefUnwindSafe for ResourceManager
impl Send for ResourceManager
impl Sync for ResourceManager
impl Unpin for ResourceManager
impl !UnwindSafe for ResourceManager
Blanket Implementations§
Source§impl<T> AsyncTaskResult for T
impl<T> AsyncTaskResult for T
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Any
. Could be used to downcast a trait object
to a particular type.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Any
. Could be used to downcast a trait object
to a particular type.fn into_any(self: Box<T>) -> Box<dyn Any>
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FieldValue for Twhere
T: 'static,
impl<T> FieldValue for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> ScriptMessagePayload for T
impl<T> ScriptMessagePayload for T
Source§fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_ref(&self) -> &(dyn Any + 'static)
self
as &dyn Any
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
self
as &dyn Any
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.