Skip to content

Commit

Permalink
Allow String and &String as Id for AssetServer.get_handle(id) (
Browse files Browse the repository at this point in the history
…#3280)

# Objective

Make it easier to build and use an asset path with `format!()`. This can be useful for accessing assets in a loop.

Enabled by this PR:
```rust
let monkey_handle = asset_server.get_handle(&format!("models/monkey/Monkey.gltf#Mesh0/Primitive0"));
let monkey_handle = asset_server.get_handle(format!("models/monkey/Monkey.gltf#Mesh0/Primitive0"));
```

Before this PR:
```rust
let monkey_handle = asset_server.get_handle(format!("models/monkey/Monkey.gltf#Mesh0/Primitive0").as_str());
```

It's just a tiny improvement in ergonomics, but i ran into it and was wondering why the function does not accept a `String` and Bevy is all about simplicity/ergonomics, right? 😄😉

## Solution

Implement `Into<HandleId>` for `String` and `&String`.
  • Loading branch information
Weasy666 committed Dec 9, 2021
1 parent 82c04f9 commit a2f0fe2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ impl From<&str> for HandleId {
}
}

impl From<&String> for HandleId {
fn from(value: &String) -> Self {
AssetPathId::from(value).into()
}
}

impl From<String> for HandleId {
fn from(value: String) -> Self {
AssetPathId::from(&value).into()
}
}

impl<T: Asset> From<&Handle<T>> for HandleId {
fn from(value: &Handle<T>) -> Self {
value.id
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_asset/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ impl<'a> From<&'a str> for AssetPath<'a> {
}
}

impl<'a> From<&'a String> for AssetPath<'a> {
fn from(asset_path: &'a String) -> Self {
asset_path.as_str().into()
}
}

impl<'a> From<&'a Path> for AssetPath<'a> {
fn from(path: &'a Path) -> Self {
AssetPath {
Expand Down

0 comments on commit a2f0fe2

Please sign in to comment.