8000 Framework generic parameter deduction fails with type alias · Issue #27 · serenity-rs/framework · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.
This repository was archived by the owner on May 21, 2024. It is now read-only.
Framework generic parameter deduction fails with type alias #27
Open
@kangalio

Description

@kangalio

Code like this can be successfully processed by the #[command] proc-macro:

#[command]
pub async fn some_command(ctx: FrameworkContext<Data>, msg: &Message) {}

However, code like this fails:

type Context = FrameworkContext<Data>;

#[command]
pub async fn some_command(ctx: Context, msg: &Message) {}

Error:

error[E0308]: mismatched types
  --> src/crates.rs:42:1
   |
42 | #[command]
   | ^^^^^^^^^^
   | |
   | expected `()`, found struct `Data`
   | expected `serenity_framework::command::Command` because of return type
   |
   = note: expected struct `serenity_framework::command::Command<()>`
              found struct `serenity_framework::command::Command<Data>`

I think the reason for this failure is that #[command] attempts to deduce the user data type by parsing generic parameters of the ctx function argument. Because of the type alias, it can't find any generic parameters and falsely thinks this function uses the default user data (()).

If this suspicion is correct, the issue can be worked around something like this:

// Inside framework crate
pub struct Context<D = DefaultData, E = DefaultError> {
    // ...
}

#[doc(hidden)]
pub trait _ContextTypes {
    type D;
    type E;
}

impl<D, E> _ContextTypes for Context<D, E> {
    type D = D;
    type E = E;
}

The command_attr crate then uses <ARGUMENTTYPE as _ContextTypes>::D and <ARGUMENTTYPE as _ContextTypes>::E instead of trying to deduce the generic parameters from ARGUMENTTYPE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0