我可以在 Rust 中定义具有自身类型参数的特征吗? [英] Can I define a trait with a type parameter of itself in Rust?

查看:27
本文介绍了我可以在 Rust 中定义具有自身类型参数的特征吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rust 的新手,在处理特征和泛型时遇到了麻烦.我首先定义了一个 trait 来为我做一些工作,然后定义了一个通用结构,将它用作类型参数.现在我意识到在原始特征中,我实际上想使用我定义的结构体,所以我处于一种循环中.我不确定如何摆脱它,并想知道我想要的东西是否可以通过泛型实现,或者我可能需要使用拳击或其他方法?下面的最小示例.

I'm new to Rust and am having trouble working with traits and generics. I started by defining a trait to do some work for me, then defined a generic struct that uses it as a type parameter. Now I realized that in the original trait, I actually want to use the struct I defined, so I'm in a kind of loop. I'm not sure how to get out of it, and wondering if what I want is even possible via generics, or maybe I'll need to use boxing or some other method? Minimal example below.

这就是我最初的做法:

trait Worker {
  fn work() -> WorkResult;
}

struct WorkResult {
  result: u64;
}

接下来我意识到我想保留一个工作人员在结果上,这样我就可以懒惰地生成值并使用各种工作人员实现.

Next I realized I want to keep a worker on the result so I can lazily generate the the value and use various worker implementations.

trait Worker {
  fn work() -> WorkResult;
}

struct WorkResult<T> where T: Worker {
  result: u64;
  worker: T;
}

这就是 Worker 特性开始变得奇怪的地方.WorkResult 现在需要一个类型参数,但它在 trait 中是未知的.我尝试了很多不同的方法,但无法让编译器喜欢它.

This is where stuff started getting weird in the Worker trait. WorkResult now needs a type parameter, but it's unknown in the trait. I tried a bunch of different methods, but can't get the compiler to like it.

尝试:

trait Worker<T> where T: Worker {
  fn work() -> WorkResult<T>;
}

struct WorkResult<T> where T: Worker {
  result: u64;
  worker: T;
}

那根本行不通.编译器甚至无法识别参数:

That didn't work at all. Compiler doesn't even recognize the parameter:

error[E0107]: wrong number of type arguments: expected 1, found 0

我不能在这里插入任何具体类型(而且我认为我不应该这样做).有什么办法可以在这里完成我想要的吗?

I can't plug in any concrete type here (and I don't think I should have to). Is there any way to accomplish what I want here?

推荐答案

这里的问题是 Worker trait 需要一个类型参数,即使用作边界,所以你需要写 <代码>其中 T:工人.

The problem here is that the Worker trait needs a type parameter even when used as a bound, so you would need to write where T: Worker<U>.

不幸的是,在特征定义的情况下,这将是递归的.例如,如果您这样做了:

Unfortunately, in the case of the trait definition, this would get recursive. For example, if you did:

trait Worker<T> 
    where T: Worker<U>,
{...}

然后您需要向 worker 添加第二个通用参数,这将需要第三个,依此类推.另一种可能性, where T: Worker 在技术上可以编译,但它使该特征不可能(或者至少,超出我的能力)成功实现.

You would then need to add a second generic parameter to worker, which would necessitate a third, and so on ad infinitum. The other possibility, where T: Worker<T> technically compiles, but it makes the trait impossible (or at the very least, beyond my ability) to successfully implement.

一种可能的解决方案是将类型参数移动到 work 函数,如下所示:

One potential solution would be to move the type parameter to the work function, like so:

trait Worker {
    fn work<T: Worker>() -> WorkResult<T>;
}

struct WorkResult<T: Worker> {
    result: u64,
    worker: T,
}

(注意 struct WorkResult 只是 struct WorkResult where T: Worker 的简写)

(Note that struct WorkResult<T: Worker> is just shorthand for struct WorkResult<T> where T: Worker)

这篇关于我可以在 Rust 中定义具有自身类型参数的特征吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆