Help us understand the problem. What is going on with this article?

[Rust][未解決] Selfに対するトレイト境界に関する疑問

More than 1 year has passed since last update.

以下はActixのコードを切り貼りして作ったもの

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ce2e866fdbd1a94464cd1993daad3e92

最後の行がどうしてもコンパイル通すことができない。

そもそもActor::start()でのSelfに対するトレイト境界はmyactor2.start()のようなものををコンパイルエラーにするのが役割なのだろうか?

以下こちらにもコードを貼っておく。

use std::marker::PhantomData;

struct Context<A>
where
    A: Actor<Context = Context<A>>,
{
    _phantom_data: PhantomData<A>,
}

trait ActorContext: Sized {}

impl<A> ActorContext for Context<A>
where
    A: Actor<Context = Self>,
{
}

impl<A> Context<A>
where
    A: Actor<Context = Self>,
{
    fn new() -> Self {
        Context { _phantom_data: PhantomData }
    }
}

trait Actor: Sized {
    type Context: ActorContext;

    fn start(&self)
    where
        Self: Actor<Context = Context<Self>>,
    {
        Context::<Self>::new();
    }
}

struct MyActor;
struct MyActor2;

impl Actor for MyActor {
    type Context = Context<Self>;
}

impl Actor for MyActor2 {
    type Context = SyncContext;
}

struct SyncContext;

impl ActorContext for SyncContext {}

fn main() {
    let myactor = MyActor {};
    myactor.start();

    let myactor2 = MyActor2 {};
    // myactor2.start(); // コンパイルエラー
}
itage
ITAGEは「IT」のAGENCYになることを夢、目標として進化、変化していきます。「It’s It Agency」
http://www.itage.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした