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 the AssetSource to load the asset from. This is optional. If one is not set the default source will be used (which is the assets 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>

source

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

source

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.

source

pub fn from_path(path: &'a Path) -> AssetPath<'a>

Creates a new AssetPath from a Path.

source

pub fn source(&self) -> &AssetSourceId<'_>

Gets the “asset source”, if one was defined. If none was defined, the default source will be used.

source

pub fn label(&self) -> Option<&str>

Gets the “sub-asset label”.

source

pub fn label_cow(&self) -> Option<CowArc<'a, str>>

Gets the “sub-asset label”.

source

pub fn path(&self) -> &Path

Gets the path to the asset in the “virtual filesystem”.

source

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

source

pub fn remove_label(&mut self)

Removes a “sub-asset label” from this AssetPath, if one was set.

source

pub fn take_label(&mut self) -> Option<CowArc<'a, str>>

Takes the “sub-asset label” from this AssetPath, if one was set.

source

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.

source

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.

source

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.

source

pub fn into_owned(self) -> AssetPath<'static>

Converts this into an “owned” value. If internally a value is borrowed, it will be cloned into an “owned Arc”. If internally a value is a static reference, the static reference will be used unchanged. If internally a value is an “owned Arc”, it will remain unchanged.

source

pub fn clone_owned(&self) -> AssetPath<'static>

Clones this into an “owned” value. If internally a value is borrowed, it will be cloned into an “owned Arc”. If internally a value is a static reference, the static reference will be used unchanged. If internally a value is an “owned Arc”, the Arc will be cloned.

source

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.

source

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 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")));
source

pub fn get_full_extension(&self) -> Option<String>

Returns the full extension (including multiple ‘.’ values). Ex: Returns "config.ron" for "my_asset.config.ron"

Trait Implementations§

source§

impl<'a> Clone for AssetPath<'a>

source§

fn clone(&self) -> AssetPath<'a>

Returns a copy 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<'a> Debug for AssetPath<'a>

source§

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

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

impl<'a> Default for AssetPath<'a>

source§

fn default() -> AssetPath<'a>

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

impl<'de> Deserialize<'de> for AssetPath<'static>

source§

fn deserialize<D>( deserializer: D ) -> Result<AssetPath<'static>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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

impl<'a> Display for AssetPath<'a>

source§

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

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

impl<'a, 'b> From<&'a AssetPath<'b>> for AssetPath<'b>

source§

fn from(value: &'a AssetPath<'b>) -> AssetPath<'b>

Converts to this type from the input type.
source§

impl From<&'static Path> for AssetPath<'static>

source§

fn from(path: &'static Path) -> AssetPath<'static>

Converts to this type from the input type.
source§

impl<'a> From<&'a String> for AssetPath<'a>

source§

fn from(asset_path: &'a String) -> AssetPath<'a>

Converts to this type from the input type.
source§

impl From<&'static str> for AssetPath<'static>

source§

fn from(asset_path: &'static str) -> AssetPath<'static>

Converts to this type from the input type.
source§

impl<'a> From<AssetPath<'a>> for PathBuf

source§

fn from(value: AssetPath<'a>) -> PathBuf

Converts to this type from the input type.
source§

impl From<AssetPath<'static>> for ShaderRef

source§

fn from(path: AssetPath<'static>) -> ShaderRef

Converts to this type from the input type.
source§

impl From<PathBuf> for AssetPath<'static>

source§

fn from(path: PathBuf) -> AssetPath<'static>

Converts to this type from the input type.
source§

impl From<String> for AssetPath<'static>

source§

fn from(asset_path: String) -> AssetPath<'static>

Converts to this type from the input type.
source§

impl FromReflect for AssetPath<'static>

source§

fn from_reflect(reflect: &(dyn Reflect + 'static)) -> Option<AssetPath<'static>>

Constructs a concrete instance of Self from a reflected value.
source§

fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>

Attempts to downcast the given value to Self using, constructing the value using from_reflect if that fails. Read more
source§

impl GetTypeRegistration for AssetPath<'static>

source§

impl<'a> Hash for AssetPath<'a>

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> PartialEq for AssetPath<'a>

source§

fn eq(&self, other: &AssetPath<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Reflect for AssetPath<'static>

source§

fn get_represented_type_info(&self) -> Option<&'static TypeInfo>

Returns the TypeInfo of the type represented by this value. Read more
source§

fn into_any(self: Box<AssetPath<'static>>) -> Box<dyn Any>

Returns the value as a Box<dyn Any>.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns the value as a &dyn Any.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Returns the value as a &mut dyn Any.
source§

fn into_reflect(self: Box<AssetPath<'static>>) -> Box<dyn Reflect>

Casts this type to a boxed reflected value.
source§

fn as_reflect(&self) -> &(dyn Reflect + 'static)

Casts this type to a reflected value.
source§

fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)

Casts this type to a mutable reflected value.
source§

fn apply(&mut self, value: &(dyn Reflect + 'static))

Applies a reflected value to this value. Read more
source§

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>

Performs a type-checked assignment of a reflected value to this value. Read more
source§

fn reflect_kind(&self) -> ReflectKind

Returns a zero-sized enumeration of “kinds” of type. Read more
source§

fn reflect_ref(&self) -> ReflectRef<'_>

Returns an immutable enumeration of “kinds” of type. Read more
source§

fn reflect_mut(&mut self) -> ReflectMut<'_>

Returns a mutable enumeration of “kinds” of type. Read more
source§

fn reflect_owned(self: Box<AssetPath<'static>>) -> ReflectOwned

Returns an owned enumeration of “kinds” of type. Read more
source§

fn clone_value(&self) -> Box<dyn Reflect>

Clones the value as a Reflect trait object. Read more
source§

fn reflect_hash(&self) -> Option<u64>

Returns a hash of the value (which includes the type). Read more
source§

fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>

Returns a “partial equality” comparison result. Read more
source§

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

Debug formatter for the value. Read more
source§

fn serializable(&self) -> Option<Serializable<'_>>

Returns a serializable version of the value. Read more
source§

fn is_dynamic(&self) -> bool

Indicates whether or not this type is a dynamic type. Read more
source§

impl<'a> Serialize for AssetPath<'a>

source§

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

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

impl TypePath for AssetPath<'static>

source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
source§

fn short_type_path() -> &'static str

Returns a short, pretty-print enabled path to the type. Read more
source§

fn type_ident() -> Option<&'static str>

Returns the name of the type, or None if it is anonymous. Read more
source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more
source§

impl Typed for AssetPath<'static>

source§

fn type_info() -> &'static TypeInfo

Returns the compile-time info for the underlying type.
source§

impl<'a> Eq for AssetPath<'a>

source§

impl<'a> StructuralEq for AssetPath<'a>

source§

impl<'a> StructuralPartialEq for AssetPath<'a>

Auto Trait Implementations§

§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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> Downcast<T> for T

source§

fn downcast(&self) -> &T

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> DynEq for T
where T: Any + Eq,

source§

fn as_any(&self) -> &(dyn Any + 'static)

Casts the type to dyn Any.
source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

This method tests for self and other values to be equal. Read more
source§

impl<T> DynHash for T
where T: DynEq + Hash,

source§

fn as_dyn_eq(&self) -> &(dyn DynEq + 'static)

Casts the type to dyn Any.
source§

fn dyn_hash(&self, state: &mut dyn Hasher)

Feeds this value into the given Hasher.
source§

impl<T> DynamicTypePath for T
where T: TypePath,

source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

source§

impl<T> FromWorld for T
where T: Default,

source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
source§

impl<T> GetPath for T
where T: Reflect + ?Sized,

source§

fn reflect_path<'p>( &self, path: impl ReflectPath<'p> ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>

Returns a reference to the value specified by path. Read more
source§

fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>

Returns a mutable reference to the value specified by path. Read more
source§

fn path<'p, T>( &self, path: impl ReflectPath<'p> ) -> Result<&T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed reference to the value specified by path. Read more
source§

fn path_mut<'p, T>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed mutable reference to the value specified by path. Read more
source§

impl<T> Instrument for T

source§

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

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

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> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer ) -> Result<(), ErrorImpl>

source§

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

§

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> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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> TypeData for T
where T: 'static + Send + Sync + Clone,

source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

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

source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

source§

impl<T> Settings for T
where T: 'static + Send + Sync,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,