impl more traits for bevy_core::Name (#3611)

# Objective

- `Name` component is missing some useful trait impls.

## Solution

- Implement the missing traits. `Display`, `AsRef<str>`, and several other conversions to and from strings.
This commit is contained in:
JoJoJet 2022-02-04 03:07:20 +00:00
parent f00aec2454
commit 6b8d64cd01

View file

@ -68,12 +68,48 @@ impl Name {
}
}
impl std::fmt::Display for Name {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.name, f)
}
}
/* Conversions from strings */
impl From<&str> for Name {
#[inline(always)]
fn from(name: &str) -> Self {
Name::new(name.to_owned())
}
}
impl From<String> for Name {
#[inline(always)]
fn from(name: String) -> Self {
Name::new(name)
}
}
/* Conversions to strings */
impl AsRef<str> for Name {
#[inline(always)]
fn as_ref(&self) -> &str {
&self.name
}
}
impl From<&Name> for String {
#[inline(always)]
fn from(val: &Name) -> String {
val.as_str().to_owned()
}
}
impl From<Name> for String {
#[inline(always)]
fn from(val: Name) -> String {
val.name.into_owned()
}
}
impl Hash for Name {
fn hash<H: Hasher>(&self, state: &mut H) {