Skip to content

Commit

Permalink
Move primitive type registration into bevy_reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jun 3, 2022
1 parent 9976ecb commit d8c5266
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
19 changes: 2 additions & 17 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,15 @@ impl Plugin for CorePlugin {
app.register_type::<HashSet<String>>()
.register_type::<Option<String>>()
.register_type::<Entity>()
.register_type::<Name>()
.register_type::<Range<f32>>();
.register_type::<Name>();

register_rust_types(app);
register_math_types(app);
}
}

fn register_rust_types(app: &mut App) {
app.register_type::<bool>()
.register_type::<u8>()
.register_type::<u16>()
.register_type::<u32>()
.register_type::<u64>()
.register_type::<u128>()
.register_type::<usize>()
.register_type::<i8>()
.register_type::<i16>()
.register_type::<i32>()
.register_type::<i64>()
.register_type::<i128>()
.register_type::<isize>()
.register_type::<f32>()
.register_type::<f64>()
app.register_type::<Range<f32>>()
.register_type::<String>()
.register_type::<Option<String>>();
}
Expand Down
38 changes: 37 additions & 1 deletion crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use serde::Deserialize;
use std::{any::TypeId, fmt::Debug, sync::Arc};

/// A registry of reflected types.
#[derive(Default)]
pub struct TypeRegistry {
registrations: HashMap<TypeId, TypeRegistration>,
short_name_to_id: HashMap<String, TypeId>,
Expand Down Expand Up @@ -35,7 +34,44 @@ pub trait GetTypeRegistration {
fn get_type_registration() -> TypeRegistration;
}

impl Default for TypeRegistry {
fn default() -> Self {
Self::new()
}
}

impl TypeRegistry {
/// Create a type registry with *no* registered types.
pub fn empty() -> Self {
Self {
registrations: Default::default(),
short_name_to_id: Default::default(),
full_name_to_id: Default::default(),
ambiguous_names: Default::default(),
}
}

/// Create a type registry with default registrations for primitive types.
pub fn new() -> Self {
let mut registry = Self::empty();
registry.register::<bool>();
registry.register::<u8>();
registry.register::<u16>();
registry.register::<u32>();
registry.register::<u64>();
registry.register::<u128>();
registry.register::<usize>();
registry.register::<i8>();
registry.register::<i16>();
registry.register::<i32>();
registry.register::<i64>();
registry.register::<i128>();
registry.register::<isize>();
registry.register::<f32>();
registry.register::<f64>();
registry
}

/// Registers the type `T`.
pub fn register<T>(&mut self)
where
Expand Down

0 comments on commit d8c5266

Please sign in to comment.