Error E0201 when implementing foreign trait for local type with parameter

我们两清 提交于 2020-01-05 04:17:05

问题


I'm trying to add the C type parameter to this code (playground):

use std::ops::Index;

struct ConnectionHandle(usize);
struct Connection<C>(C);

impl<C> Index<ConnectionHandle> for Vec<Connection<C>> {
    type Output = Connection<C>;
    fn index(&self, ch: ConnectionHandle) -> &Self::Output {
        &self[ch.0]
    }
}

But doing so causes this error message:

error[E0210]: type parameter `C` must be used as the type parameter for some local type (e.g. `MyStruct<C>`)
 --> src/lib.rs:6:1
  |
6 | impl<C> Index<ConnectionHandle> for Vec<Connection<C>> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `C` must be used as the type parameter for some local type
  |
  = note: only traits defined in the current crate can be implemented for a type parameter

Why isn't this allowed? Connection is local, so per the explanation for E0201 it seems like this should not result in orphans.


回答1:


The problem is that Vec<Connection<C>> isn't considered a local type, because Vec isn't local (and isn't fundamental).

RFC 2451 will make it legal, however. An implementation was merged on January 4th, so it's not in stable yet, but it works with a recent nightly if the re_rebalance_coherence feature is enabled.



来源:https://stackoverflow.com/questions/54400991/error-e0201-when-implementing-foreign-trait-for-local-type-with-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!