Skip to content

Impl Reflect for Box<[T]> - #11646

Open
tguichaoua wants to merge 20 commits into
bevyengine:mainfrom
tguichaoua:impl_reflect_for_boxed_slice
Open

Impl Reflect for Box<[T]>#11646
tguichaoua wants to merge 20 commits into
bevyengine:mainfrom
tguichaoua:impl_reflect_for_boxed_slice

Conversation

@tguichaoua

@tguichaoua tguichaoua commented Feb 1, 2024

Copy link
Copy Markdown
Contributor

Objective

Solution

Implements one the solutions proposed in #11570 (comment).

  • Add FixedLenList as super trait of List.

Changelog

  • Added FixedLenList trait as a super trait of List which contains all the methods that did not change the size of the list (e.g. insert, remove, etc.).
  • Implement Reflect for Box<[T]> as a FixedLenList.

Migration Guide

  • ReflectRef, ReflectMut and ReflectOwned have a new variant named FixedLenList.
    Accessing a fixed len list from one of these enum may require some boilerplate code :
let list_ref = match b.reflect_ref() {
    ReflectRef::FixedLenList(list) => list,
    ReflectRef::List(list) => list.as_fixed_len_list(),
    _ => unimplemented!(),
};
  • Every types that implement List must also implement FixedLenList
  • The following methods have been migrated from List to FixedLenList
    • get
    • get_mut
    • len
    • is_empty
    • iter
    • clone_dynamic

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Reflection Runtime information about types labels Feb 1, 2024

@doonv doonv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like this, but I'm not particularly a fan of the FixedLenList name. But I don't have a better name to suggest.

Comment thread crates/bevy_reflect/src/list.rs Outdated
@tguichaoua

Copy link
Copy Markdown
Contributor Author

I like this, but I'm not particularly a fan of the FixedLenList name. But I don't have a better name to suggest.

Yes me too.
Another idea I had is to name the base trait List and the resizable list one something like ResizableList.
But it's probably confusing to rename an existing trait and introduce a new trait with the old name.

@Adamkob12

Copy link
Copy Markdown
Contributor

name suggestions / general ideas:

  • List -> DyanmicList , FixedLenList -> List
  • "Collection"
  • "SizedList"
  • "DynamicArray"
  • "Slice"

but I also think that FixedSizeList / FixedLenList are clear enough - I wouldn't mind them.

@pablo-lua pablo-lua left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Liked this changes and this sure can be useful in importing the std types into reflection, but there is something bugging me, and commented on that.

Comment thread crates/bevy_reflect/src/list.rs Outdated
@pablo-lua

Copy link
Copy Markdown
Contributor

name suggestions / general ideas:

[...]

  • "SizedList"

Liked the name SizedList, but agree that FixedLenList is a good name too. Another possible name is FixedSizeList if we can't follow with Len for some reason

@pablo-lua pablo-lua left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm on board with this, the user can probably do some performances improvements with something that they knows that has a fixed len. And helping the user to get reflection on Box<[T]> is good too.

Comment thread crates/bevy_reflect/src/list.rs Outdated
@spectria-limina

Copy link
Copy Markdown
Contributor

Why not just Array? In Rust parlance, an array has a static size, so it seems like a natural complement to List being the dynamic one.

@doonv

doonv commented Feb 12, 2024

Copy link
Copy Markdown
Contributor

Why not just Array? In Rust parlance, an array has a static size, so it seems like a natural complement to List being the dynamic one.

Because an Array means the size is known at compile time, a Box<[T]> is a fixed length list with a size known at runtime instead of compile time.

@spectria-limina

Copy link
Copy Markdown
Contributor

Ahh... SizedList, perhaps? Though that could have the same problem...

@soqb soqb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

gah! another Cow<Foo>/Box<Foo>/&Foo inconsistency. starting to think we should somehow have ReflectUnsized/ReflectBorrow traits which make things easier.

regarding the implementation, i would rather we used List and bit the performance pain point of just reallocating via Vec (using this From impl and Vec::into_boxed_slice) on every insertion since i think trait complexity is already extremely high in Reflect (and only going up).

@spectria-limina

Copy link
Copy Markdown
Contributor

It may be a good idea to start with a simple implementation and revisit later if it's proving a hotspot

@SpecificProtagonist

SpecificProtagonist commented Jul 9, 2024

Copy link
Copy Markdown
Contributor

Because an Array means the size is known at compile time, a Box<[T]> is a fixed length list with a size known at runtime instead of compile time.

Reflection can only be accessed at runtime, so Array and the proposed FixedLenList both are the same.

@janhohenheim janhohenheim added D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Sep 15, 2024
@NthTensor

Copy link
Copy Markdown
Contributor

This PR appears to have gone stale. Nominating to close.

@cart cart closed this May 5, 2026
@cart cart reopened this May 5, 2026
@SOF3

SOF3 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

I would like to rework on this if necessary. What is blocking on this PR (other than author inactive)? Was the naming bikeshed unresolved?

@JaySpruce JaySpruce added S-Nominated-To-Close A triage team member thinks this PR or issue should be closed out. and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Aug 7, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Reflection Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Reflection Runtime information about types C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Nominated-To-Close A triage team member thinks this PR or issue should be closed out.

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

Implement Reflect for Box<[T]>