Path: blob/main/crates/bevy_feathers/src/handle_or_path.rs
6595 views
//! Provides a way to specify assets either by handle or by path.1use bevy_asset::{Asset, Handle};2use bevy_reflect::Reflect;34/// Enum that represents a reference to an asset as either a [`Handle`] or a [`String`] path.5///6/// This is useful for when you want to specify an asset, but don't always have convenient7/// access to an asset server reference.8#[derive(Clone, Debug, Reflect)]9pub enum HandleOrPath<T: Asset> {10/// Specify the asset reference as a handle.11Handle(Handle<T>),12/// Specify the asset reference as a [`String`].13Path(String),14}1516impl<T: Asset> Default for HandleOrPath<T> {17fn default() -> Self {18Self::Path("".to_string())19}20}2122// Necessary because we don't want to require T: PartialEq23impl<T: Asset> PartialEq for HandleOrPath<T> {24fn eq(&self, other: &Self) -> bool {25match (self, other) {26(HandleOrPath::Handle(h1), HandleOrPath::Handle(h2)) => h1 == h2,27(HandleOrPath::Path(p1), HandleOrPath::Path(p2)) => p1 == p2,28_ => false,29}30}31}3233impl<T: Asset> From<Handle<T>> for HandleOrPath<T> {34fn from(h: Handle<T>) -> Self {35HandleOrPath::Handle(h)36}37}3839impl<T: Asset> From<&str> for HandleOrPath<T> {40fn from(p: &str) -> Self {41HandleOrPath::Path(p.to_string())42}43}4445impl<T: Asset> From<String> for HandleOrPath<T> {46fn from(p: String) -> Self {47HandleOrPath::Path(p.clone())48}49}5051impl<T: Asset> From<&String> for HandleOrPath<T> {52fn from(p: &String) -> Self {53HandleOrPath::Path(p.to_string())54}55}5657impl<T: Asset + Clone> From<&HandleOrPath<T>> for HandleOrPath<T> {58fn from(p: &HandleOrPath<T>) -> Self {59p.to_owned()60}61}626364