LoginSignup
0
0

More than 3 years have passed since last update.

[Rust] トレイトの参照でハマる

Posted at

素朴なOOPLの気持ちで書いてるとハマる話

経緯

経緯を簡略化したコードで書いていく。

元のコード

struct Context;

impl Context {
    fn do_something(&mut self) {}
}

fn func(context: &mut Context) {
    context.do_something();
}

実際はContextはメンバ持ってたり、do_something()も空実装ではないが省略。

次にContextをトレイトにしたくなった。

trait Context {
    fn do_something(&mut self);
}

struct ContextImpl;

impl Context for ContextImpl {
    fn do_something(&mut self) {}
}

fn func(context: &mut Context) {
    context.do_something();
}

特に問題は無い。

ここに、Hogeトレイトを追加して、Contextに対して実装する。

trait Hoge {
    fn hoge(&mut self);
}

impl<T: Context> Hoge for T {
    fn hoge(&mut self) {
        self.do_something();
    }
}

fn func(context: &mut Context) {
    context.do_something();
    context.hoge();
}

コンパイルエラーになる

error[E0599]: no method named `hoge` found for type `&mut dyn Context` in the current scope
  --> src/main.rs:23:13
   |
23 |     context.hoge();
   |             ^^^^
   |
   = note: the method `hoge` exists but the following trait bounds were not satisfied:
           `&mut dyn Context : Hoge`
           `dyn Context : Hoge`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `hoge`, perhaps you need to implement it:
           candidate #1: `Hoge`

error: aborting due to previous error

何がいけなかったかと言うと、func()の引数を&mut Contextのままにしておいたのがダメだった。

fn func<T: Context>(context: &mut T) {
    context.do_something();
    context.hoge();
}

これでコンパイルエラーはなくなる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0