bevy/crates/bevy_reflect/src
Zachary Harrold fcfa60844a
Remove allocation in get_short_name (#15294)
`ShortName` is lazily evaluated and does not allocate, instead providing
`Display` and `Debug` implementations which write directly to a
formatter using the original algorithm. When using `ShortName` in format
strings (`panic`, `dbg`, `format`, etc.) you can directly use the
`ShortName` type. If you require a `String`, simply call
`ShortName(...).to_string()`.

# Objective

- Remove the requirement for allocation when using `get_short_name`

## Solution

- Added new type `ShortName` which wraps a name and provides its own
`Debug` and `Display` implementations, using the original
`get_short_name` algorithm without the need for allocating.
- Removed `get_short_name`, as `ShortName(...)` is more performant and
ergonomic.
- Added `ShortName::of::<T>` method to streamline the common use-case
for name shortening.

## Testing

- CI

## Migration Guide

### For `format!`, `dbg!`, `panic!`, etc.

```rust
// Before
panic!("{} is too short!", get_short_name(name));

// After
panic!("{} is too short!", ShortName(name));
```

### Need a `String` Value

```rust
// Before
let short: String = get_short_name(name);

// After
let short: String = ShortName(name).to_string();
```

## Notes

`ShortName` lazily evaluates, and directly writes to a formatter via
`Debug` and `Display`, which removes the need to allocate a `String`
when printing a shortened type name. Because the implementation has been
moved into the `fmt` method, repeated printing of the `ShortName` type
may be less performant than converting it into a `String`. However, no
instances of this are present in Bevy, and the user can get the original
behaviour by calling `.to_string()` at no extra cost.

---------

Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
2024-09-19 15:34:03 +00:00
..
enums bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
func bevy_reflect: Mention FunctionRegistry in bevy_reflect::func docs (#15147) 2024-09-10 23:39:05 +00:00
impls Use FromReflect when extracting entities in dynamic scenes (#15174) 2024-09-15 14:33:39 +00:00
path ParsedPath::try_from<&str> (#15180) 2024-09-13 17:37:09 +00:00
serde bevy_reflect: Add DynamicTyped trait (#15108) 2024-09-13 17:17:10 +00:00
array.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
attributes.rs Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
fields.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
from_reflect.rs bevy_reflect: Update on_unimplemented attributes (#15110) 2024-09-09 16:26:17 +00:00
lib.rs Remove allocation in get_short_name (#15294) 2024-09-19 15:34:03 +00:00
list.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
map.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
reflect.rs bevy_reflect: Add Reflectable trait (#5772) 2024-09-18 00:36:41 +00:00
reflectable.rs bevy_reflect: Add Reflectable trait (#5772) 2024-09-18 00:36:41 +00:00
remote.rs bevy_reflect: Reflect remote types (#6042) 2024-08-12 19:12:53 +00:00
set.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
std_traits.rs fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
struct_trait.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
tuple_struct.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
tuple.rs bevy_reflect: Add Type type (#14838) 2024-08-25 17:57:07 +00:00
type_info_stack.rs bevy_reflect: Contextual serialization error messages (#13888) 2024-09-09 17:52:40 +00:00
type_info.rs bevy_reflect: Add Reflectable trait (#5772) 2024-09-18 00:36:41 +00:00
type_path.rs bevy_reflect: Update on_unimplemented attributes (#15110) 2024-09-09 16:26:17 +00:00
type_registry.rs bevy_reflect: Add Reflectable trait (#5772) 2024-09-18 00:36:41 +00:00
utility.rs bevy_reflect: Add DynamicTyped trait (#15108) 2024-09-13 17:17:10 +00:00