Skip to content

Commit c5f4546

Browse files
JMLX42alice-i-cecilemockersf
authored andcommitted
Add From<Uuid> implementation for Handle<A> (bevyengine#21394)
## Objective Closes bevyengine#21349 Provides an ergonomic way to create a `Handle<A>` from a `Uuid` at runtime. ## Solution Implements the `From<Uuid>` trait for `Handle<A>`, allowing users to write: ```rust let uuid = Uuid::new_v4(); let handle: Handle<Image> = uuid.into(); ``` Instead of the current verbose approach: ```rust let handle = Handle::<Image>::Uuid(uuid, PhantomData); ``` ## Testing - Added comprehensive test `from_uuid` that verifies: - Conversion from `Uuid` to `Handle<A>` works correctly - The resulting handle is a UUID variant - The handle ID matches the input UUID - Both `.into()` and explicit `From::from()` work - All existing handle tests continue to pass - Clippy and formatting checks pass --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: François Mockers <[email protected]>
1 parent 7a4b80b commit c5f4546

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

crates/bevy_asset/src/handle.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ impl<A: Asset> From<&mut Handle<A>> for UntypedAssetId {
268268
}
269269
}
270270

271+
impl<A: Asset> From<Uuid> for Handle<A> {
272+
#[inline]
273+
fn from(uuid: Uuid) -> Self {
274+
Handle::Uuid(uuid, PhantomData)
275+
}
276+
}
277+
271278
/// An untyped variant of [`Handle`], which internally stores the [`Asset`] type information at runtime
272279
/// as a [`TypeId`] instead of encoding it in the compile-time type. This allows handles across [`Asset`] types
273280
/// to be stored together and compared.
@@ -626,6 +633,15 @@ mod tests {
626633
assert_eq!(UntypedHandle::from(typed.clone()), untyped);
627634
}
628635

636+
#[test]
637+
fn from_uuid() {
638+
let uuid = UUID_1;
639+
let handle: Handle<TestAsset> = uuid.into();
640+
641+
assert!(handle.is_uuid());
642+
assert_eq!(handle.id(), AssetId::Uuid { uuid });
643+
}
644+
629645
/// `PartialReflect::reflect_clone`/`PartialReflect::to_dynamic` should increase the strong count of a strong handle
630646
#[test]
631647
fn strong_handle_reflect_clone() {

0 commit comments

Comments
 (0)