Skip to content

Commit

Permalink
replace uuid by a u64
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Feb 22, 2022
1 parent ad4bb64 commit 8455c37
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
7 changes: 4 additions & 3 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
use anyhow::Result;
use bevy_ecs::system::{Res, ResMut};
use bevy_log::warn;
use bevy_reflect::UniqueAssetId;
use bevy_tasks::TaskPool;
use bevy_utils::{Entry, HashMap, Uuid};
use bevy_utils::{Entry, HashMap};
use crossbeam_channel::TryRecvError;
use parking_lot::{Mutex, RwLock};
use std::{path::Path, sync::Arc};
Expand Down Expand Up @@ -52,7 +53,7 @@ pub struct AssetServerInternal {
pub(crate) asset_io: Box<dyn AssetIo>,
pub(crate) asset_ref_counter: AssetRefCounter,
pub(crate) asset_sources: Arc<RwLock<HashMap<SourcePathId, SourceInfo>>>,
pub(crate) asset_lifecycles: Arc<RwLock<HashMap<Uuid, Box<dyn AssetLifecycle>>>>,
pub(crate) asset_lifecycles: Arc<RwLock<HashMap<UniqueAssetId, Box<dyn AssetLifecycle>>>>,
loaders: RwLock<Vec<Arc<dyn AssetLoader>>>,
extension_to_loader_index: RwLock<HashMap<String, usize>>,
handle_to_path: Arc<RwLock<HashMap<HandleId, AssetPath<'static>>>>,
Expand Down Expand Up @@ -880,7 +881,7 @@ mod test {
assert!(server.get_handle_path(&handle).is_none());

// invalid HandleId
let invalid_id = HandleId::new(Uuid::new_v4(), 42);
let invalid_id = HandleId::new(0, 42);
assert!(server.get_handle_path(invalid_id).is_none());

// invalid AssetPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Asset, Assets};
use bevy_app::prelude::*;
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics, MAX_DIAGNOSTIC_NAME_WIDTH};
use bevy_ecs::system::{Res, ResMut};
use bevy_reflect::Uuid;

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

impl<T: Asset> AssetCountDiagnosticsPlugin<T> {
pub fn diagnostic_id() -> DiagnosticId {
DiagnosticId(T::TYPE_UUID)
DiagnosticId(Uuid::from_u128(T::TYPE_UUID as u128))
}

pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
Expand Down
9 changes: 4 additions & 5 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::{
Asset, Assets,
};
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize};
use bevy_utils::Uuid;
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, UniqueAssetId};
use crossbeam_channel::{Receiver, Sender};
use serde::{Deserialize, Serialize};

Expand All @@ -32,7 +31,7 @@ use serde::{Deserialize, Serialize};
)]
#[reflect_value(Serialize, Deserialize, PartialEq, Hash)]
pub enum HandleId {
Id(Uuid, u64),
Id(UniqueAssetId, u64),
AssetPathId(AssetPathId),
}

Expand Down Expand Up @@ -60,7 +59,7 @@ impl HandleId {
}

#[inline]
pub const fn new(type_uuid: Uuid, id: u64) -> Self {
pub const fn new(type_uuid: UniqueAssetId, id: u64) -> Self {
HandleId::Id(type_uuid, id)
}
}
Expand Down Expand Up @@ -306,7 +305,7 @@ pub struct HandleUntyped {
}

impl HandleUntyped {
pub const fn weak_from_u64(uuid: Uuid, id: u64) -> Self {
pub const fn weak_from_u64(uuid: UniqueAssetId, id: u64) -> Self {
Self {
id: HandleId::new(uuid, id),
handle_type: HandleType::Weak,
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_asset/src/info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{path::AssetPath, LabelId};
use bevy_utils::{HashMap, HashSet, Uuid};
use bevy_reflect::UniqueAssetId;
use bevy_utils::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

Expand All @@ -12,15 +13,15 @@ pub struct SourceMeta {
pub struct AssetMeta {
pub label: Option<String>,
pub dependencies: Vec<AssetPath<'static>>,
pub type_uuid: Uuid,
pub type_uuid: UniqueAssetId,
}

/// Info about a specific asset, such as its path and its current load state
#[derive(Clone, Debug)]
pub struct SourceInfo {
pub meta: Option<SourceMeta>,
pub path: PathBuf,
pub asset_types: HashMap<LabelId, Uuid>,
pub asset_types: HashMap<LabelId, UniqueAssetId>,
pub load_state: LoadState,
pub committed_assets: HashSet<LabelId>,
pub version: usize,
Expand All @@ -33,7 +34,7 @@ impl SourceInfo {
})
}

pub fn get_asset_type(&self, label_id: LabelId) -> Option<Uuid> {
pub fn get_asset_type(&self, label_id: LabelId) -> Option<UniqueAssetId> {
self.asset_types.get(&label_id).cloned()
}
}
Expand Down
15 changes: 7 additions & 8 deletions crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate proc_macro;

use std::{collections::hash_map::DefaultHasher, hash::Hasher};

use bevy_macro_utils::BevyManifest;
use quote::{quote, ToTokens};
use syn::{parse::*, *};
Expand Down Expand Up @@ -50,17 +52,14 @@ pub fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre

let uuid =
uuid.expect("No `#[uuid = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"` attribute found.");
let bytes = uuid
.as_bytes()
.iter()
.map(|byte| format!("{:#X}", byte))
.map(|byte_str| syn::parse_str::<LitInt>(&byte_str).unwrap());

let mut s = DefaultHasher::new();
std::hash::Hash::hash(&uuid, &mut s);
let unique_id = s.finish();

let gen = quote! {
impl #bevy_reflect_path::TypeUuid for #name {
const TYPE_UUID: #bevy_reflect_path::Uuid = #bevy_reflect_path::Uuid::from_bytes([
#( #bytes ),*
]);
const TYPE_UUID: #bevy_reflect_path::UniqueAssetId = #unique_id;
}
};
gen.into()
Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_reflect/src/type_uuid.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
pub use bevy_reflect_derive::TypeUuid;
pub use bevy_utils::Uuid;

pub type UniqueAssetId = u64;

/// A trait for types with a statically associated UUID.
pub trait TypeUuid {
const TYPE_UUID: Uuid;
const TYPE_UUID: UniqueAssetId;
}

/// A trait for types with an associated UUID.
pub trait TypeUuidDynamic {
fn type_uuid(&self) -> Uuid;
fn type_uuid(&self) -> UniqueAssetId;
fn type_name(&self) -> &'static str;
}

Expand All @@ -17,7 +19,7 @@ where
T: TypeUuid,
{
/// Returns the UUID associated with this value's type.
fn type_uuid(&self) -> Uuid {
fn type_uuid(&self) -> UniqueAssetId {
Self::TYPE_UUID
}

Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use bevy_ecs::{
system::{lifetimeless::*, SystemParamItem},
};
use bevy_math::{const_vec2, Vec2};
use bevy_reflect::Uuid;
use bevy_render::{
color::Color,
render_asset::RenderAssets,
Expand Down Expand Up @@ -416,7 +415,7 @@ pub fn queue_sprites(

// Impossible starting values that will be replaced on the first iteration
let mut current_batch = SpriteBatch {
image_handle_id: HandleId::Id(Uuid::nil(), u64::MAX),
image_handle_id: HandleId::Id(u64::MAX, u64::MAX),
colored: false,
};
let mut current_batch_entity = Entity::from_raw(u32::MAX);
Expand Down

0 comments on commit 8455c37

Please sign in to comment.