Skip to content

Dowgrade/Upgrade not ususable in traits #1322

Open
@gdesmott

Description

@gdesmott
trait Mytrait: Downgrade {
    fn mushroom(&self) {
        let weak = self.downgrade();
        let this = weak.upgrade().unwrap();
        this.snake();
    }

    fn snake(&self) {}
}

fails with:

error[E0599]: no method named `snake` found for associated type `<<Self as Downgrade>::Weak as Upgrade>::Strong` in the current scope
   --> glib/src/clone.rs:116:14
    |
116 |         this.snake();
    |              ^^^^^ method not found in `<<Self as Downgrade>::Weak as Upgrade>::Strong`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
note: `Mytrait` defines an item `snake`, perhaps you need to implement it
   --> glib/src/clone.rs:112:1
    |
112 | trait Mytrait: Downgrade {
    | ^^^^^^^^^^^^^^^^^^^^^^^^

I tried adding a bound tying back Upgrade::Strong with the trait but that doesn't work either:

trait Mytrait: Downgrade
where
    <<Self as Downgrade>::Weak as Upgrade>::Strong: Mytrait,
{
    fn mushroom(&self) {
        let weak = self.downgrade();
        let this = weak.upgrade().unwrap();
        this.snake();
    }

    fn snake(&self) {}
}
error[E0277]: the trait bound `<<<<Self as Downgrade>::Weak as Upgrade>::Strong as Downgrade>::Weak as Upgrade>::Strong: Mytrait` is not satisfied
   --> glib/src/clone.rs:114:53
    |
114 |     <<Self as Downgrade>::Weak as Upgrade>::Strong: Mytrait,
    |                                                     ^^^^^^^ the trait `Mytrait` is not implemented for `<<<<Self as Downgrade>::Weak as Upgrade>::Strong as Downgrade>::Weak as Upgrade>::Strong`
    |
note: required by a bound in `Mytrait`
   --> glib/src/clone.rs:114:53
    |
112 | trait Mytrait: Downgrade
    |       ------- required by a bound in this trait
113 | where
114 |     <<Self as Downgrade>::Weak as Upgrade>::Strong: Mytrait,
    |                                                     ^^^^^^^ required by this bound in `Mytrait`
    = note: `Mytrait` is a "sealed trait", because to implement it you also need to implement `clone::Mytrait`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it
help: consider further restricting the associated type
    |
114 |     <<Self as Downgrade>::Weak as Upgrade>::Strong: Mytrait, <<<<Self as Downgrade>::Weak as Upgrade>::Strong as Downgrade>::Weak as Upgrade>::Strong: Mytrait
    |                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there a way to make this work easily?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions