Struct bevy_asset::AssetPath
source · pub struct AssetPath<'a> { /* private fields */ }
Expand description
Represents a path to an asset in a “virtual filesystem”.
Asset paths consist of three main parts:
AssetPath::source
: The name of theAssetSource
to load the asset from. This is optional. If one is not set the default source will be used (which is theassets
folder by default).AssetPath::path
: The “virtual filesystem path” pointing to an asset source file.AssetPath::label
: An optional “named sub asset”. When assets are loaded, they are allowed to load “sub assets” of any type, which are identified by a named “label”.
Asset paths are generally constructed (and visualized) as strings:
// This loads the `my_scene.scn` base asset from the default asset source.
let scene: Handle<Scene> = asset_server.load("my_scene.scn");
// This loads the `PlayerMesh` labeled asset from the `my_scene.scn` base asset in the default asset source.
let mesh: Handle<Mesh> = asset_server.load("my_scene.scn#PlayerMesh");
// This loads the `my_scene.scn` base asset from a custom 'remote' asset source.
let scene: Handle<Scene> = asset_server.load("remote://my_scene.scn");
AssetPath
implements From
for &'static str
, &'static Path
, and &'a String
,
which allows us to optimize the static cases.
This means that the common case of asset_server.load("my_scene.scn")
when it creates and
clones internal owned AssetPaths
.
This also means that you should use AssetPath::parse
in cases where &str
is the explicit type.
Implementations§
source§impl<'a> AssetPath<'a>
impl<'a> AssetPath<'a>
sourcepub fn parse(asset_path: &'a str) -> AssetPath<'a>
pub fn parse(asset_path: &'a str) -> AssetPath<'a>
Creates a new AssetPath
from a string in the asset path format:
- An asset at the root:
"scene.gltf"
- An asset nested in some folders:
"some/path/scene.gltf"
- An asset with a “label”:
"some/path/scene.gltf#Mesh0"
- An asset with a custom “source”:
"custom://some/path/scene.gltf#Mesh0"
Prefer [From<'static str>
] for static strings, as this will prevent allocations
and reference counting for AssetPath::into_owned
.
§Panics
Panics if the asset path is in an invalid format. Use AssetPath::try_parse
for a fallible variant
sourcepub fn try_parse(
asset_path: &'a str
) -> Result<AssetPath<'a>, ParseAssetPathError>
pub fn try_parse( asset_path: &'a str ) -> Result<AssetPath<'a>, ParseAssetPathError>
Creates a new AssetPath
from a string in the asset path format:
- An asset at the root:
"scene.gltf"
- An asset nested in some folders:
"some/path/scene.gltf"
- An asset with a “label”:
"some/path/scene.gltf#Mesh0"
- An asset with a custom “source”:
"custom://some/path/scene.gltf#Mesh0"
Prefer [From<'static str>
] for static strings, as this will prevent allocations
and reference counting for AssetPath::into_owned
.
This will return a ParseAssetPathError
if asset_path
is in an invalid format.
sourcepub fn source(&self) -> &AssetSourceId<'_>
pub fn source(&self) -> &AssetSourceId<'_>
Gets the “asset source”, if one was defined. If none was defined, the default source will be used.
sourcepub fn without_label(&self) -> AssetPath<'_>
pub fn without_label(&self) -> AssetPath<'_>
Gets the path to the asset in the “virtual filesystem” without a label (if a label is currently set).
sourcepub fn remove_label(&mut self)
pub fn remove_label(&mut self)
Removes a “sub-asset label” from this AssetPath
, if one was set.
sourcepub fn take_label(&mut self) -> Option<CowArc<'a, str>>
pub fn take_label(&mut self) -> Option<CowArc<'a, str>>
Takes the “sub-asset label” from this AssetPath
, if one was set.
sourcepub fn with_label(self, label: impl Into<CowArc<'a, str>>) -> AssetPath<'a>
pub fn with_label(self, label: impl Into<CowArc<'a, str>>) -> AssetPath<'a>
Returns this asset path with the given label. This will replace the previous label if it exists.
sourcepub fn with_source(self, source: impl Into<AssetSourceId<'a>>) -> AssetPath<'a>
pub fn with_source(self, source: impl Into<AssetSourceId<'a>>) -> AssetPath<'a>
Returns this asset path with the given asset source. This will replace the previous asset source if it exists.
sourcepub fn parent(&self) -> Option<AssetPath<'a>>
pub fn parent(&self) -> Option<AssetPath<'a>>
Returns an AssetPath
for the parent folder of this path, if there is a parent folder in the path.
sourcepub fn into_owned(self) -> AssetPath<'static>
pub fn into_owned(self) -> AssetPath<'static>
sourcepub fn clone_owned(&self) -> AssetPath<'static>
pub fn clone_owned(&self) -> AssetPath<'static>
sourcepub fn resolve(
&self,
path: &str
) -> Result<AssetPath<'static>, ParseAssetPathError>
pub fn resolve( &self, path: &str ) -> Result<AssetPath<'static>, ParseAssetPathError>
Resolves a relative asset path via concatenation. The result will be an AssetPath
which
is resolved relative to this “base” path.
assert_eq!(AssetPath::parse("a/b").resolve("c"), Ok(AssetPath::parse("a/b/c")));
assert_eq!(AssetPath::parse("a/b").resolve("./c"), Ok(AssetPath::parse("a/b/c")));
assert_eq!(AssetPath::parse("a/b").resolve("../c"), Ok(AssetPath::parse("a/c")));
assert_eq!(AssetPath::parse("a/b").resolve("c.png"), Ok(AssetPath::parse("a/b/c.png")));
assert_eq!(AssetPath::parse("a/b").resolve("/c"), Ok(AssetPath::parse("c")));
assert_eq!(AssetPath::parse("a/b.png").resolve("#c"), Ok(AssetPath::parse("a/b.png#c")));
assert_eq!(AssetPath::parse("a/b.png#c").resolve("#d"), Ok(AssetPath::parse("a/b.png#d")));
There are several cases:
If the path
argument begins with #
, then it is considered an asset label, in which case
the result is the base path with the label portion replaced.
If the path argument begins with ‘/’, then it is considered a ‘full’ path, in which
case the result is a new AssetPath
consisting of the base path asset source
(if there is one) with the path and label portions of the relative path. Note that a ‘full’
asset path is still relative to the asset source root, and not necessarily an absolute
filesystem path.
If the path
argument begins with an asset source (ex: http://
) then the entire base
path is replaced - the result is the source, path and label (if any) of the path
argument.
Otherwise, the path
argument is considered a relative path. The result is concatenated
using the following algorithm:
- The base path and the
path
argument are concatenated. - Path elements consisting of “/.” or “<name>/..” are removed.
If there are insufficient segments in the base path to match the “..” segments, then any left-over “..” segments are left as-is.
sourcepub fn resolve_embed(
&self,
path: &str
) -> Result<AssetPath<'static>, ParseAssetPathError>
pub fn resolve_embed( &self, path: &str ) -> Result<AssetPath<'static>, ParseAssetPathError>
Resolves an embedded asset path via concatenation. The result will be an AssetPath
which
is resolved relative to this path. This is similar in operation to resolve
, except that
the ‘file’ portion of the base path (that is, any characters after the last ‘/’)
is removed before concatenation, in accordance with the behavior specified in
IETF RFC 1808 “Relative URIs”.
The reason for this behavior is that embedded URIs which start with “./” or “../” are
relative to the directory containing the asset, not the asset file. This is consistent
with the behavior of URIs in JavaScript
, CSS, HTML and other web file formats. The
primary use case for this method is resolving relative paths embedded within asset files,
which are relative to the asset in which they are contained.
assert_eq!(AssetPath::parse("a/b").resolve_embed("c"), Ok(AssetPath::parse("a/c")));
assert_eq!(AssetPath::parse("a/b").resolve_embed("./c"), Ok(AssetPath::parse("a/c")));
assert_eq!(AssetPath::parse("a/b").resolve_embed("../c"), Ok(AssetPath::parse("c")));
assert_eq!(AssetPath::parse("a/b").resolve_embed("c.png"), Ok(AssetPath::parse("a/c.png")));
assert_eq!(AssetPath::parse("a/b").resolve_embed("/c"), Ok(AssetPath::parse("c")));
assert_eq!(AssetPath::parse("a/b.png").resolve_embed("#c"), Ok(AssetPath::parse("a/b.png#c")));
assert_eq!(AssetPath::parse("a/b.png#c").resolve_embed("#d"), Ok(AssetPath::parse("a/b.png#d")));
sourcepub fn get_full_extension(&self) -> Option<String>
pub fn get_full_extension(&self) -> Option<String>
Returns the full extension (including multiple ‘.’ values).
Ex: Returns "config.ron"
for "my_asset.config.ron"
Also strips out anything following a ?
to handle query parameters in URIs
Trait Implementations§
source§impl<'de> Deserialize<'de> for AssetPath<'static>
impl<'de> Deserialize<'de> for AssetPath<'static>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<'a> FromReflect for AssetPath<'a>
impl<'a> FromReflect for AssetPath<'a>
source§fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Self
from a reflected value.source§fn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moresource§impl<'a> GetTypeRegistration for AssetPath<'a>
impl<'a> GetTypeRegistration for AssetPath<'a>
source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration
for this type.source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
source§impl<'a> PartialEq for AssetPath<'a>
impl<'a> PartialEq for AssetPath<'a>
source§impl<'a> Reflect for AssetPath<'a>
impl<'a> Reflect for AssetPath<'a>
source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
.source§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moresource§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
source§fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
source§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
source§impl<'a> TypePath for AssetPath<'a>
impl<'a> TypePath for AssetPath<'a>
source§fn type_path() -> &'static str
fn type_path() -> &'static str
source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
impl<'a> Eq for AssetPath<'a>
impl<'a> StructuralPartialEq for AssetPath<'a>
Auto Trait Implementations§
impl<'a> Freeze for AssetPath<'a>
impl<'a> RefUnwindSafe for AssetPath<'a>
impl<'a> Send for AssetPath<'a>
impl<'a> Sync for AssetPath<'a>
impl<'a> Unpin for AssetPath<'a>
impl<'a> UnwindSafe for AssetPath<'a>
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> 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> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.source§impl<T> GetPath for T
impl<T> GetPath for T
source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p> ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moresource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more