Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@maueki

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

More than 1 year has passed since last update.

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

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

そもそも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(); // コンパイルエラー
}
0
Help us understand the problem. What is going on with this article?
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
itage
ITAGEは「IT」のAGENCYになることを夢、目標として進化、変化していきます。「It’s It Agency」

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?