bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Impl `Reflect` for `Box<[T]>`

Open tguichaoua opened this issue 2 years ago • 8 comments

Objective

  • Fixes #11570.

Solution

Implements one the solutions proposed in https://github.com/bevyengine/bevy/issues/11570#issuecomment-1913542767.

  • 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

tguichaoua avatar Feb 01 '24 11:02 tguichaoua

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.

tguichaoua avatar Feb 01 '24 22:02 tguichaoua

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.

Adamkob12 avatar Feb 02 '24 16:02 Adamkob12

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 avatar Feb 05 '24 23:02 pablo-lua

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.

spectria-limina avatar Feb 12 '24 17:02 spectria-limina

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.

doonv avatar Feb 12 '24 17:02 doonv

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

spectria-limina avatar Feb 12 '24 18:02 spectria-limina

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

spectria-limina avatar Feb 19 '24 16:02 spectria-limina

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.

SpecificProtagonist avatar Jul 09 '24 17:07 SpecificProtagonist