Applications currently cannot dynamically generate different types of Element
s as they need to be passed to a function that is generic over E: Element
and it is not possible for a library user to downcast
a dyn Element
to a specific Element
.
I think it would make sense to implement downcast methods (probably using downcast) or have methods take Box<dyn Element>
directly however the overhead the latter would cause for the case where the discrete type is known may not be acceptable in this case.
Actually, it would be possible for a user to do this by creating a new, downcastable, trait and implementing it for every instance of
Element
so feel free to close this issue if you feel that this is the most appropriate solution.
Thanks for bringing this up!
There are two distinct cases: containers (
LinearLayout
,TableLayout
) and wrappers (StyledElement
, ...). For containers, the elements are stored in aBox
anyway, so it should be possible to add boxed elements. I don’t want to change the argument type of the existing methods because that would make them harder to use, but I think we should add new methods for adding boxed elements, e. g.push_box
. Would that work for you?
Having push_box in some cases but not in others seems a bit unfortunate but I think it makes the most sense. Especially since the Elements need to be unboxed anyways if they are not stored in a Box internally.
To be clear, I’m not against adding boxed methods for the wrappers. But I first have to think about the consequences of that change, whereas it is a no-brainer for the containers as we already use boxed elements anyway.
The easiest solution would be to change the method signature from
fn push<E: Element + 'static>(&mut self, element: E) {}to
fn push<E: Into<Box<dyn Element>>>(&mut self, element: E) {}but unfortunately, that does not work because
Box<dyn Element>
does not implementFrom<T: Element>
. An alternative would be to introduce aIntoBoxedElement
helper trait similar to cursive’sIntoBoxedView
. But as we already have quite a lot of breaking changes for the v0.2.0 release, I’d rather postpone that to the next release.
I’ve introduced the
IntoBoxedElement
trait and changedDocument
,LinearLayout
andTableLayoutRow
to accept implementors of that trait (instead ofElement
) in commit d086145acb0ad6e03c8331dd83e00f7a1c80511c. We might want to change the wrapper types to use boxes instead of type parameters too – I’ve created #35 for that.