Repository: bevyengine/bevy
File: crates/bevy_asset/src/lib.rs (Rust)

Code before:
                let untyped_handle: UntypedHandle = load_context
                    .loader()
                    .with_unknown_type()
                    .load(asset_path)
                    .into();

                Ok(TestAssetUD(untyped_handle))
            }

            fn extensions(&self) -> &[&str] {
                &["ron"]
            }
        }

        app.init_asset::<TestAssetUD>()
            .register_asset_loader(ImmediateSelfLoader);

        let handle: Handle<TestAssetUD> = asset_server.load("abc.cool.ron");

        run_app_until(&mut app, |world| match asset_server.load_state(&handle) {
            LoadState::Loading => None,
            LoadState::Loaded => {
                let test_assets = world.resource::<Assets<TestAssetUD>>();
                let asset = test_assets.get(&handle).unwrap();
                assert_eq!(handle.id(), asset.0.id().typed_unchecked::<TestAssetUD>());
                // This one fails.
                // assert_eq!(handle.id(), asset.0.id().typed::<TestAssetUD>());
                Some(())
            }
            state => panic!("Unexpected asset state: {state:?}"),
        });

        run_app_until(&mut app, |world| {
            let messages = collect_asset_events(world);
            if messages.is_empty() {
                return None;
            }
            assert_eq!(
                messages,
                [
                    AssetEvent::LoadedWithDependencies { id: handle.id() },
                    AssetEvent::Added { id: handle.id() }
                ]
            );
            Some(())
        });

        // Sending an asset event should result in the asset being reloaded - resulting in a
        // "Modified" message.
        source_events
            .send_blocking(AssetSourceEvent::ModifiedAsset(PathBuf::from(

Review comment (bug):
I don't understand this. Why does this fail? The handle inside the asset should produce the same handle, so the asset type should be the same? Documenting that here would be useful.

Code after:
            LoadState::Loading => None,
            LoadState::Loaded => {
                let asset_id = {
                    let test_assets = world.resource::<Assets<TestAssetUD>>();
                    let asset = test_assets.get(&handle).unwrap();
                    asset.0.id()
                };
                let untyped = world.resource::<Assets<crate::LoadedUntypedAsset>>();
                if let Some(untyped_handle) = untyped.get(asset_id) {
                    assert_eq!(handle.id(), untyped_handle.handle.id());
                    Some(())
                } else {
                    None
                }
            }
            state => panic!("Unexpected asset state: {state:?}"),
        });

        run_app_until(&mut app, |world| {
            let messages = collect_asset_events(world);
            if messages.is_empty() {
                return None;
            }
            assert_eq!(
                messages,
                [
                    AssetEvent::LoadedWithDependencies { id: handle.id() },
                    AssetEvent::Added { id: handle.id() }
                ]
            );
            Some(())
        });

        // Sending an asset event should result in the asset being reloaded - resulting in a
        // "Modified" message.
        source_events
            .send_blocking(AssetSourceEvent::ModifiedAsset(PathBuf::from("abc.rsp")))
            .unwrap();

        run_app_until(&mut app, |world| {
            let messages = collect_asset_events(world);
            if messages.is_empty() {
                return None;
            }
            assert_eq!(
                messages,
                [
                    AssetEvent::LoadedWithDependencies { id: handle.id() },
                    AssetEvent::Modified { id: handle.id() }
                ]
            );