From f86d575b724972ac1ab1cdf1c0a36c80f1c92864 Mon Sep 17 00:00:00 2001 From: Jean-Marc Le Roux Date: Sun, 5 Oct 2025 11:41:56 +0200 Subject: [PATCH 1/3] Add `From` implementation for `Handle` Implements conversion from Uuid to Handle for easier handle creation. This provides a more ergonomic API when working with dynamic asset UUIDs. Closes #21349 --- crates/bevy_asset/src/handle.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 8924bf87de850..08289768d36a2 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -268,6 +268,13 @@ impl From<&mut Handle> for UntypedAssetId { } } +impl From for Handle { + #[inline] + fn from(uuid: Uuid) -> Self { + Handle::Uuid(uuid, PhantomData) + } +} + /// An untyped variant of [`Handle`], which internally stores the [`Asset`] type information at runtime /// as a [`TypeId`] instead of encoding it in the compile-time type. This allows handles across [`Asset`] types /// to be stored together and compared. @@ -626,6 +633,21 @@ mod tests { assert_eq!(UntypedHandle::from(typed.clone()), untyped); } + /// `Handle` should be created from a `Uuid` via `From` trait + #[test] + fn from_uuid() { + let uuid = UUID_1; + let handle: Handle = uuid.into(); + + assert!(handle.is_uuid()); + assert_eq!(handle.id(), AssetId::Uuid { uuid }); + + // Should also work with explicit From::from + let handle2 = Handle::::from(UUID_2); + assert!(handle2.is_uuid()); + assert_eq!(handle2.id(), AssetId::Uuid { uuid: UUID_2 }); + } + /// `PartialReflect::reflect_clone`/`PartialReflect::to_dynamic` should increase the strong count of a strong handle #[test] fn strong_handle_reflect_clone() { From 019da1a3a1e04fdf89205ccabccaa373f3767d8b Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sun, 5 Oct 2025 10:14:29 -0400 Subject: [PATCH 2/3] Remove unhelpful comment --- crates/bevy_asset/src/handle.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 08289768d36a2..7fa4c537ae46e 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -633,7 +633,6 @@ mod tests { assert_eq!(UntypedHandle::from(typed.clone()), untyped); } - /// `Handle` should be created from a `Uuid` via `From` trait #[test] fn from_uuid() { let uuid = UUID_1; From 274edd5a42e19e6d116e498ac60e7cd29c3197e6 Mon Sep 17 00:00:00 2001 From: Jean-Marc Le Roux Date: Sun, 5 Oct 2025 17:31:45 +0200 Subject: [PATCH 3/3] Update crates/bevy_asset/src/handle.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François Mockers --- crates/bevy_asset/src/handle.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 7fa4c537ae46e..77cb6bddfa7e9 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -640,11 +640,6 @@ mod tests { assert!(handle.is_uuid()); assert_eq!(handle.id(), AssetId::Uuid { uuid }); - - // Should also work with explicit From::from - let handle2 = Handle::::from(UUID_2); - assert!(handle2.is_uuid()); - assert_eq!(handle2.id(), AssetId::Uuid { uuid: UUID_2 }); } /// `PartialReflect::reflect_clone`/`PartialReflect::to_dynamic` should increase the strong count of a strong handle