Skip to content

Move Name out of bevy_core #16894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/animation_curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! # use bevy_math::curve::{Curve, Interval, FunctionCurve};
//! # use bevy_animation::{AnimationClip, AnimationTargetId, animated_field, animation_curves::*};
//! # use bevy_transform::components::Transform;
//! # use bevy_core::Name;
//! # use bevy_ecs::name::Name;
//! # use bevy_math::vec3;
//! # let wobble_curve = FunctionCurve::new(
//! # Interval::UNIT,
Expand Down Expand Up @@ -144,7 +144,7 @@ use downcast_rs::{impl_downcast, Downcast};
///
/// # use bevy_animation::{AnimationClip, AnimationTargetId, VariableCurve, AnimationEntityMut, AnimationEvaluationError, animation_curves::EvaluatorId};
/// # use bevy_animation::prelude::{AnimatableProperty, AnimatableKeyframeCurve, AnimatableCurve};
/// # use bevy_core::Name;
/// # use bevy_ecs::name::Name;
/// # use bevy_reflect::Reflect;
/// # use bevy_render::camera::PerspectiveProjection;
/// # use std::any::TypeId;
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use crate::{

use bevy_app::{Animation, App, Plugin, PostUpdate};
use bevy_asset::{Asset, AssetApp, Assets};
use bevy_core::Name;
use bevy_ecs::{
entity::{VisitEntities, VisitEntitiesMut},
prelude::*,
Expand Down
7 changes: 1 addition & 6 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@

extern crate alloc;

mod name;
#[cfg(feature = "serialize")]
mod serde;
mod task_pool_options;

use bevy_ecs::system::Resource;
pub use name::*;
pub use task_pool_options::*;

/// The core prelude.
///
/// This includes the most common types in this crate, re-exported for your convenience.
pub mod prelude {
#[doc(hidden)]
pub use crate::{
FrameCountPlugin, Name, NameOrEntity, TaskPoolOptions, TaskPoolPlugin,
TypeRegistrationPlugin,
};
pub use crate::{FrameCountPlugin, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin};
}

use bevy_app::prelude::*;
Expand Down
38 changes: 1 addition & 37 deletions crates/bevy_core/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,7 @@ use serde::{
Deserialize, Deserializer, Serialize, Serializer,
};

use super::{name::Name, FrameCount};

impl Serialize for Name {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}

impl<'de> Deserialize<'de> for Name {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_str(EntityVisitor)
}
}

struct EntityVisitor;

impl<'de> Visitor<'de> for EntityVisitor {
type Value = Name;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str(any::type_name::<Name>())
}

fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(Name::new(v.to_string()))
}

fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Ok(Name::new(v))
}
}
use super::FrameCount;

// Manually implementing serialize/deserialize allows us to use a more compact representation as simple integers
impl Serialize for FrameCount {
Expand Down Expand Up @@ -76,12 +46,6 @@ mod tests {

use serde_test::{assert_tokens, Token};

#[test]
fn test_serde_name() {
let name = Name::new("MyComponent");
assert_tokens(&name, &[Token::String("MyComponent")]);
}

#[test]
fn test_serde_frame_count() {
let frame_count = FrameCount(100);
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ bumpalo = "3"
[dev-dependencies]
rand = "0.8"
static_assertions = "1.1.0"
serde_test = "1.0"

[[example]]
name = "events"
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod event;
pub mod identifier;
pub mod intern;
pub mod label;
pub mod name;
pub mod observer;
pub mod query;
#[cfg(feature = "bevy_reflect")]
Expand All @@ -59,6 +60,7 @@ pub mod prelude {
component::{require, Component},
entity::{Entity, EntityMapper},
event::{Event, EventMutator, EventReader, EventWriter, Events},
name::{Name, NameOrEntity},
observer::{CloneEntityWithObserversExt, Observer, Trigger},
query::{Added, AnyOf, Changed, Has, Or, QueryBuilder, QueryState, With, Without},
removal_detection::RemovedComponents,
Expand Down
79 changes: 68 additions & 11 deletions crates/bevy_core/src/name.rs → crates/bevy_ecs/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#[cfg(feature = "bevy_reflect")]
use bevy_ecs::reflect::ReflectComponent;
use bevy_ecs::{component::Component, entity::Entity, query::QueryData};
//! Provides the [`Name`] [`Component`], used for identifying an [`Entity`].

use alloc::borrow::Cow;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::std_traits::ReflectDefault;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use crate::{self as bevy_ecs, component::Component, entity::Entity, query::QueryData};

use alloc::{
borrow::{Cow, ToOwned},
string::String,
};
use bevy_utils::FixedHasher;
use core::{
hash::{BuildHasher, Hash, Hasher},
ops::Deref,
};

#[cfg(feature = "serialize")]
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};

#[cfg(feature = "bevy_reflect")]
use {
crate::reflect::ReflectComponent,
bevy_reflect::{std_traits::ReflectDefault, Reflect},
};

#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};

Expand Down Expand Up @@ -101,14 +112,13 @@ impl core::fmt::Debug for Name {
/// Convenient query for giving a human friendly name to an entity.
///
/// ```
/// # use bevy_core::prelude::*;
/// # use bevy_ecs::prelude::*;
/// # #[derive(Component)] pub struct Score(f32);
/// fn increment_score(mut scores: Query<(NameOrEntity, &mut Score)>) {
/// for (name, mut score) in &mut scores {
/// score.0 += 1.0;
/// if score.0.is_nan() {
/// bevy_utils::tracing::error!("Score for {name} is invalid");
/// log::error!("Score for {name} is invalid");
/// }
/// }
/// }
Expand Down Expand Up @@ -213,10 +223,44 @@ impl Deref for Name {
}
}

#[cfg(feature = "serialize")]
impl Serialize for Name {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}

#[cfg(feature = "serialize")]
impl<'de> Deserialize<'de> for Name {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_str(NameVisitor)
}
}

#[cfg(feature = "serialize")]
struct NameVisitor;

#[cfg(feature = "serialize")]
impl<'de> Visitor<'de> for NameVisitor {
type Value = Name;

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str(core::any::type_name::<Name>())
}

fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(Name::new(v.to_string()))
}

fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Ok(Name::new(v))
}
}

#[cfg(test)]
mod tests {
use super::*;
use bevy_ecs::world::World;
use crate::world::World;

#[test]
fn test_display_of_debug_name() {
Expand All @@ -233,3 +277,16 @@ mod tests {
assert_eq!(d2.to_string(), "MyName");
}
}

#[cfg(all(test, feature = "serialize"))]
mod serde_tests {
use super::Name;

use serde_test::{assert_tokens, Token};

#[test]
fn test_serde_name() {
let name = Name::new("MyComponent");
assert_tokens(&name, &[Token::String("MyComponent")]);
}
}
2 changes: 1 addition & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use bevy_asset::{
io::Reader, AssetLoadError, AssetLoader, Handle, LoadContext, ReadAssetBytesError,
};
use bevy_color::{Color, LinearRgba};
use bevy_core::Name;
use bevy_core_pipeline::prelude::Camera3d;
use bevy_ecs::{
entity::{Entity, EntityHashMap},
name::Name,
world::World,
};
use bevy_hierarchy::{BuildChildren, ChildBuild, WorldChildBuilder};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_hierarchy/src/valid_parent_check_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<T> Default for ReportHierarchyIssue<T> {
/// (See B0004 explanation linked in warning message)
pub fn check_hierarchy_component_has_valid_parent<T: Component>(
parent_query: Query<
(Entity, &Parent, Option<&bevy_core::Name>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary for this PR, but we could replace the Option<&Name> + Entity with NameOrEntity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree, I'll leave it off just to keep things less controversial, but it'd be a nice little follow-up.

(Entity, &Parent, Option<&Name>),
(With<T>, Or<(Changed<Parent>, Added<T>)>),
>,
component_query: Query<(), With<T>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! The gamepad input functionality.

use crate::{Axis, ButtonInput, ButtonState};
use bevy_core::Name;
#[cfg(feature = "bevy_reflect")]
use bevy_ecs::prelude::ReflectComponent;
use bevy_ecs::{
change_detection::DetectChangesMut,
component::Component,
entity::Entity,
event::{Event, EventReader, EventWriter},
name::Name,
prelude::require,
system::{Commands, Query},
};
Expand Down Expand Up @@ -316,7 +316,7 @@ pub enum ButtonSettingsError {
/// ```
/// # use bevy_input::gamepad::{Gamepad, GamepadAxis, GamepadButton};
/// # use bevy_ecs::system::Query;
/// # use bevy_core::Name;
/// # use bevy_ecs::name::Name;
/// #
/// fn gamepad_usage_system(gamepads: Query<(&Name, &Gamepad)>) {
/// for (name, gamepad) in &gamepads {
Expand Down
Loading