Skip to content

Commit 8455c37

Browse files
committed
replace uuid by a u64
1 parent ad4bb64 commit 8455c37

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

crates/bevy_asset/src/asset_server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use crate::{
77
use anyhow::Result;
88
use bevy_ecs::system::{Res, ResMut};
99
use bevy_log::warn;
10+
use bevy_reflect::UniqueAssetId;
1011
use bevy_tasks::TaskPool;
11-
use bevy_utils::{Entry, HashMap, Uuid};
12+
use bevy_utils::{Entry, HashMap};
1213
use crossbeam_channel::TryRecvError;
1314
use parking_lot::{Mutex, RwLock};
1415
use std::{path::Path, sync::Arc};
@@ -52,7 +53,7 @@ pub struct AssetServerInternal {
5253
pub(crate) asset_io: Box<dyn AssetIo>,
5354
pub(crate) asset_ref_counter: AssetRefCounter,
5455
pub(crate) asset_sources: Arc<RwLock<HashMap<SourcePathId, SourceInfo>>>,
55-
pub(crate) asset_lifecycles: Arc<RwLock<HashMap<Uuid, Box<dyn AssetLifecycle>>>>,
56+
pub(crate) asset_lifecycles: Arc<RwLock<HashMap<UniqueAssetId, Box<dyn AssetLifecycle>>>>,
5657
loaders: RwLock<Vec<Arc<dyn AssetLoader>>>,
5758
extension_to_loader_index: RwLock<HashMap<String, usize>>,
5859
handle_to_path: Arc<RwLock<HashMap<HandleId, AssetPath<'static>>>>,
@@ -880,7 +881,7 @@ mod test {
880881
assert!(server.get_handle_path(&handle).is_none());
881882

882883
// invalid HandleId
883-
let invalid_id = HandleId::new(Uuid::new_v4(), 42);
884+
let invalid_id = HandleId::new(0, 42);
884885
assert!(server.get_handle_path(invalid_id).is_none());
885886

886887
// invalid AssetPath

crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{Asset, Assets};
22
use bevy_app::prelude::*;
33
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics, MAX_DIAGNOSTIC_NAME_WIDTH};
44
use bevy_ecs::system::{Res, ResMut};
5+
use bevy_reflect::Uuid;
56

67
/// Adds "asset count" diagnostic to an App
78
pub struct AssetCountDiagnosticsPlugin<T: Asset> {
@@ -25,7 +26,7 @@ impl<T: Asset> Plugin for AssetCountDiagnosticsPlugin<T> {
2526

2627
impl<T: Asset> AssetCountDiagnosticsPlugin<T> {
2728
pub fn diagnostic_id() -> DiagnosticId {
28-
DiagnosticId(T::TYPE_UUID)
29+
DiagnosticId(Uuid::from_u128(T::TYPE_UUID as u128))
2930
}
3031

3132
pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {

crates/bevy_asset/src/handle.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use crate::{
1010
Asset, Assets,
1111
};
1212
use bevy_ecs::{component::Component, reflect::ReflectComponent};
13-
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize};
14-
use bevy_utils::Uuid;
13+
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, UniqueAssetId};
1514
use crossbeam_channel::{Receiver, Sender};
1615
use serde::{Deserialize, Serialize};
1716

@@ -32,7 +31,7 @@ use serde::{Deserialize, Serialize};
3231
)]
3332
#[reflect_value(Serialize, Deserialize, PartialEq, Hash)]
3433
pub enum HandleId {
35-
Id(Uuid, u64),
34+
Id(UniqueAssetId, u64),
3635
AssetPathId(AssetPathId),
3736
}
3837

@@ -60,7 +59,7 @@ impl HandleId {
6059
}
6160

6261
#[inline]
63-
pub const fn new(type_uuid: Uuid, id: u64) -> Self {
62+
pub const fn new(type_uuid: UniqueAssetId, id: u64) -> Self {
6463
HandleId::Id(type_uuid, id)
6564
}
6665
}
@@ -306,7 +305,7 @@ pub struct HandleUntyped {
306305
}
307306

308307
impl HandleUntyped {
309-
pub const fn weak_from_u64(uuid: Uuid, id: u64) -> Self {
308+
pub const fn weak_from_u64(uuid: UniqueAssetId, id: u64) -> Self {
310309
Self {
311310
id: HandleId::new(uuid, id),
312311
handle_type: HandleType::Weak,

crates/bevy_asset/src/info.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{path::AssetPath, LabelId};
2-
use bevy_utils::{HashMap, HashSet, Uuid};
2+
use bevy_reflect::UniqueAssetId;
3+
use bevy_utils::{HashMap, HashSet};
34
use serde::{Deserialize, Serialize};
45
use std::path::PathBuf;
56

@@ -12,15 +13,15 @@ pub struct SourceMeta {
1213
pub struct AssetMeta {
1314
pub label: Option<String>,
1415
pub dependencies: Vec<AssetPath<'static>>,
15-
pub type_uuid: Uuid,
16+
pub type_uuid: UniqueAssetId,
1617
}
1718

1819
/// Info about a specific asset, such as its path and its current load state
1920
#[derive(Clone, Debug)]
2021
pub struct SourceInfo {
2122
pub meta: Option<SourceMeta>,
2223
pub path: PathBuf,
23-
pub asset_types: HashMap<LabelId, Uuid>,
24+
pub asset_types: HashMap<LabelId, UniqueAssetId>,
2425
pub load_state: LoadState,
2526
pub committed_assets: HashSet<LabelId>,
2627
pub version: usize,
@@ -33,7 +34,7 @@ impl SourceInfo {
3334
})
3435
}
3536

36-
pub fn get_asset_type(&self, label_id: LabelId) -> Option<Uuid> {
37+
pub fn get_asset_type(&self, label_id: LabelId) -> Option<UniqueAssetId> {
3738
self.asset_types.get(&label_id).cloned()
3839
}
3940
}

crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate proc_macro;
22

3+
use std::{collections::hash_map::DefaultHasher, hash::Hasher};
4+
35
use bevy_macro_utils::BevyManifest;
46
use quote::{quote, ToTokens};
57
use syn::{parse::*, *};
@@ -50,17 +52,14 @@ pub fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
5052

5153
let uuid =
5254
uuid.expect("No `#[uuid = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"` attribute found.");
53-
let bytes = uuid
54-
.as_bytes()
55-
.iter()
56-
.map(|byte| format!("{:#X}", byte))
57-
.map(|byte_str| syn::parse_str::<LitInt>(&byte_str).unwrap());
55+
56+
let mut s = DefaultHasher::new();
57+
std::hash::Hash::hash(&uuid, &mut s);
58+
let unique_id = s.finish();
5859

5960
let gen = quote! {
6061
impl #bevy_reflect_path::TypeUuid for #name {
61-
const TYPE_UUID: #bevy_reflect_path::Uuid = #bevy_reflect_path::Uuid::from_bytes([
62-
#( #bytes ),*
63-
]);
62+
const TYPE_UUID: #bevy_reflect_path::UniqueAssetId = #unique_id;
6463
}
6564
};
6665
gen.into()

crates/bevy_reflect/src/type_uuid.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
pub use bevy_reflect_derive::TypeUuid;
22
pub use bevy_utils::Uuid;
33

4+
pub type UniqueAssetId = u64;
5+
46
/// A trait for types with a statically associated UUID.
57
pub trait TypeUuid {
6-
const TYPE_UUID: Uuid;
8+
const TYPE_UUID: UniqueAssetId;
79
}
810

911
/// A trait for types with an associated UUID.
1012
pub trait TypeUuidDynamic {
11-
fn type_uuid(&self) -> Uuid;
13+
fn type_uuid(&self) -> UniqueAssetId;
1214
fn type_name(&self) -> &'static str;
1315
}
1416

@@ -17,7 +19,7 @@ where
1719
T: TypeUuid,
1820
{
1921
/// Returns the UUID associated with this value's type.
20-
fn type_uuid(&self) -> Uuid {
22+
fn type_uuid(&self) -> UniqueAssetId {
2123
Self::TYPE_UUID
2224
}
2325

crates/bevy_sprite/src/render/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use bevy_ecs::{
1212
system::{lifetimeless::*, SystemParamItem},
1313
};
1414
use bevy_math::{const_vec2, Vec2};
15-
use bevy_reflect::Uuid;
1615
use bevy_render::{
1716
color::Color,
1817
render_asset::RenderAssets,
@@ -416,7 +415,7 @@ pub fn queue_sprites(
416415

417416
// Impossible starting values that will be replaced on the first iteration
418417
let mut current_batch = SpriteBatch {
419-
image_handle_id: HandleId::Id(Uuid::nil(), u64::MAX),
418+
image_handle_id: HandleId::Id(u64::MAX, u64::MAX),
420419
colored: false,
421420
};
422421
let mut current_batch_entity = Entity::from_raw(u32::MAX);

0 commit comments

Comments
 (0)