#Sharing code between impls

22 messages · Page 1 of 1 (latest)

wild lance
steady mesa
#

and in the default methods, instead of using self.children you can use self.children() or self.children_mut(), depends on the situation

wild lance
steady mesa
#

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

wild lance
#

Ok cool! Maybe I can make that work. Thanks @steady mesa !

steady mesa
#

np ferristhumbsup

wild lance
tender quailBOT
#

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
```

wild lance
#
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`.
steady mesa
#

ah, you're not using VT in the actual-

#

hmmmmm

wild lance
#

associated type?

#

perhaps

steady mesa
#

yeah i think it's time for associated types

wild lance
#

ok will try!