简体   繁体   English

在 Rust 中通过关联类型实现一个 trait 的问题

[英]Problem with implementing a trait over another trait with associated types in Rust

I have this trait:我有这个特点:


trait Pokemon {
    type Move;
    fn pick_move(&self) -> Self::Move;
}

Certain types implement the trait, like so:某些类型实现了 trait,如下所示:


#[derive(PartialEq, Clone, Copy)]
enum Fire {
    Charmander,
    Charmeleon,
    Charizard
}

#[derive(PartialEq, Clone, Copy)]
enum FireMove {
    Ember, FlameThrower, FireBlast
}

#[derive(PartialEq, Clone, Copy)]
enum Water {
    Squirtle,
    Wartortle,
    Blastoise
}

#[derive(PartialEq, Clone, Copy)]
enum WaterMove {
    Bubble,
    WaterGun
}

impl Pokemon for Fire {
    type Move = FireMove;

    fn pick_move(&self) -> Self::Move {
        match self {
            Self::Charmander => Self::Move::Ember,
            Self::Charmeleon => Self::Move::FlameThrower,
            Self::Charizard => Self::Move::FireBlast,
        }
    }
}

impl Pokemon for Water {
    type Move = WaterMove;

    fn pick_move(&self) -> Self::Move {
        if *self == Water::Squirtle {
            return Self::Move::Bubble;
        }
        Self::Move::WaterGun
    }
}

For the types that implemented the Pokemon trait, I want to implement a trait Battle :对于实现Pokemon trait 的类型,我想实现一个 trait Battle


trait Battle {
  fn battle(&self) -> ??;
}

In the end, what I'd want to achieve is that I should be able to call .battle on any type that implements the Pokemon trait and eventually be able to return the Pokemon that wins a battle.最后,我想要实现的是我应该能够在任何实现Pokemon特性的类型上调用.battle并最终能够返回赢得战斗的 Pokemon。

I've thought about using such a method too:我也考虑过使用这样的方法:


fn battle<T>(pokemon: T, foe: T) -> T
where T: Pokemon
{
    let p_move = pokemon.pick_move();
    let f_move = foe.pick_move();

    if p_move == f_move {
        return pokemon;
    }

    foe
}

Unfortunately, here I'm not able to compare the Move s of the arguments that have been passed.不幸的是,在这里我无法比较已传递参数的Move

One way I came close to achieving this is by doing something like this:我接近实现这一目标的一种方法是做这样的事情:


trait Battle {
    type Pokemons;
    
    fn battle(&self, foe: Self::Pokemons) -> Self::Pokemons;
}

#[derive(PartialEq, Clone, Copy)]
enum PokemonTypes {
    Fire(Fire),
    Water(Water),
    Grass(Grass)
}

impl Battle for Fire {
    type Pokemons = PokemonTypes;

    fn battle(&self, foe: Self::Pokemons) -> Self::Pokemons {
        match foe {
            Self::Pokemons::Water(pokemon) => Self::Pokemons::Water(pokemon),
            _ => Self::Pokemons::Fire(*self) // because Fire beats Grass type
        }
    }
}

So basically, how do I implement this Battle trait that should help me compare the moves and/or the pokemon types and decide the winner?所以基本上,我如何实现这个Battle特性来帮助我比较动作和/或口袋妖怪类型并决定获胜者?

You could abstract the move types into another enum, and remove the associated type in the trait:您可以将移动类型抽象为另一个枚举,并删除特征中的关联类型:

#[derive(PartialEq, Clone, Copy)]
enum Move {
    Water(WaterMove),
    Fire(FireMove),
}

trait Pokemon {
    fn pick_move(&self) -> Move;
}

And then the same way, you can directly use your implementation for the Battle system, but implementing battle over PokemonType :然后以同样的方式,您可以直接使用Battle系统的实现,通过PokemonType实现对战:

#[derive(PartialEq, Clone, Copy)]
enum PokemonType {
    Fire(Fire),
    Water(Water),
}

trait Battle {
    fn battle(&self, foe: PokemonType) -> PokemonType;
}

impl Battle for PokemonType {
    fn battle(&self, foe: PokemonType) -> PokemonType {
        match (self, foe) {
            (p1@PokemonType::Water(_), p2@PokemonType::Fire(_)) => {
                *p1
                // do watever
            }
            _ => foe // handle other patterns
        }
    }
}

Playground 操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM