-
I've recently been attempting to setup a custom asset using serde to parse a YAML file similar to what was done in this example, but I've been running into quite a few issues getting the lifetimes correct and I'm hoping it is just my relative inexperience with Rust. The YAML files I'm attempting to load bring in arbitrary String data, so I need my asset to have a borrowed #[derive(Default, Debug, TypeUuid, Deserialize)]
#[uuid = "594838dc-63d6-4718-a223-2f76dc347a0c"]
pub struct GameConfig<'a> {
pub window_name: &'a str,
}
#[derive(Default)]
pub struct YamlLoader<T: DeserializeOwned> {
pub yamls: T,
}
impl<T> AssetLoader for YamlLoader<T>
where
T: 'static + Send + Sync + Asset + DeserializeOwned + Debug,
{
fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, anyhow::Result<()>> {
Box::pin(async move {
serde_yaml::from_slice::<T>(bytes)
.map_err(Error::msg)
.map(|yaml_file| {
load_context.set_default_asset(LoadedAsset::new(yaml_file));
})
})
}
fn extensions(&self) -> &[&str] {
&["yml", "yaml"]
}
}
impl Plugin for YamlLoaderPlugin {
fn build(&self, app: &mut bevy::prelude::AppBuilder) {
app
.add_asset::<GameConfig<'static>()
.init_asset_loader::<YamlLoader<GameConfig<'static>>>();
}
} I've been aiming for this to be a generic YAML loader so any arbitrary struct can be loaded, but while attempting to attach lifetimes to my
I had a few questions around this:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
quick workaround: replace your |
Beta Was this translation helpful? Give feedback.
quick workaround: replace your
&'a str
by aString