I'd like to split up this Stack into HStack, VStack and ZStack structs while sharing code that doesn't depend on the orientation. I suppose I could make some free functions, but there would still be some boilerplate. Is there a nice way to do this? Code is here: https://github.com/audulus/rui/blob/main/src/views/stack.rs
#Sharing code between impls
22 messages · Page 1 of 1 (latest)
you could have a Stack trait, which look like:
trait Stack<VT> {
fn layout(&self, id: ViewId, sz: LocalSize, cx: &mut Context, vger: &mut Vger) -> LocalSize;
fn children(&self) -> &VT;
fn children_mut(&mut self) -> &mut VT;
// process, draw, dirty, hittest, commands, gc, access as default methods
}
and in the default methods, instead of using self.children you can use self.children() or self.children_mut(), depends on the situation
I was trying that but couldn’t get it to work with the View trait which it needs to implement
ahh, hm
something like this perhaps?
trait Stack<VT: ViewTuple + 'static> {
fn layout(&self, id: ViewId, sz: LocalSize, cx: &mut Context, vger: &mut Vger) -> LocalSize;
fn children(&self) -> &VT;
}
impl<VT: ViewTuple + 'static, S: Stack<VT>> View for S {
fn layout(&self, id: ViewId, sz: LocalSize, cx: &mut Context, vger: &mut Vger) -> LocalSize {
<S as Stack<VT>>::layout(self)
}
fn process(
&self,
event: &Event,
id: ViewId,
cx: &mut Context,
vger: &mut Vger,
actions: &mut Vec<Box<dyn Any>>,
) {
let mut c = 0;
self.children().foreach_view(&mut |child| {
let child_id = id.child(&c);
let offset = cx.layout.entry(child_id).or_default().offset;
(*child).process(&event.offset(-offset), child_id, cx, vger, actions);
c += 1;
})
}
// and so on
}
@wild lance
a blanket impl of View for anything implementing Stack, given the implementors provide the required methods layout, children and children_mut
actually i realized that none of these methods take &mut self
so really it's just layout and children
Ok cool! Maybe I can make that work. Thanks @steady mesa !
np 
hmm I get that VT is an unconstrained type parameter
-errors
If you're getting large or confusing errors please post the full error message from cargo check in a code block instead of the errors in your IDE so that we can understand your problem better:
```rust
// error from cargo check here
```
error[E0207]: the type parameter `VT` is not constrained by the impl trait, self type, or predicates
--> src/views/stack.rs:22:6
|
22 | impl<VT: ViewTuple, S:StackCommon<VT>> View for S {
| ^^ unconstrained type parameter
For more information about this error, try `rustc --explain E0207`.
yeah i think it's time for associated types
ok will try!