struct - 如何接受扩展 PartialEq 的盒装特征对象?
问题描述
我想创建一个自定义类型,它接受任何实现扩展特征的对象PartialEq
。
// In the external crate
// Actual Type:-
// [webdriver::command::ExtensionCommand](https://docs.rs/webdriver/0.40.2/webdriver/command/enum.WebDriverExtensionCommand.html)
pub trait Foo: Send + Clone + PartialEq {}
// Actual Type:-
// [webdriver::command::WebDriverCommand](https://docs.rs/webdriver/0.40.2/webdriver/command/enum.WebDriverCommand.html)
pub struct Bar<T: Foo> {
foo: T,
}
// In my crate
type BarTwo = Bar<Box<dyn Foo>>;
我无法更改特征或结构,因为这些类型不在我的箱子中。我不能将泛型类型用于BarTwo
.
输出
error[E0038]: the trait `Foo` cannot be made into an object
--> src/lib.rs:12:23
|
4 | pub trait Foo : Send + Clone + PartialEq{}
| --- --------- ...because it uses `Self` as a type parameter in this
| |
| this trait cannot be made into an object...
...
12 | type BarTwo = Bar<Box<dyn Foo>>;
| ^^^^^^^ the trait `Foo` cannot be made into an object
解决方案
推荐阅读
- windows - MSYS2 无法删除 Windows docker 卷中的文件
- javascript - 功能 id="no-link" 无法正常工作
- excel - 如何使用 VBA 在一个工作簿中动态更新多个 PivotCache?
- wpf - WPF 中的饼图 NuGet 包
- npgsql - Npgsql 错误 - 在 Npgsql 提供程序清单中找不到存储类型“nvarchar(MAX)”
- c++ - 基于数据类型变量选择模板实现的优雅方式
- qt - Qt - 使用单击事件启动进程并使用第二个单击事件再次停止它
- javascript - 退格键不会删除输入字段中的字母?
- python - 用户警告:在已关闭的文件上调用 close()。warn("在已经关闭的文件上调用 close()。")
- linux - 缺少符号:cuDevicePrimaryCtxRelease 与 cuDevicePrimaryCtxRelease_v2