如何编写具有动态泛型的结构?
假设我有以下结构,
trait T{}
struct A<X:T>{
...
}
我想知道这样的事情是否可能
Box<A<dyn T>>
当前我收到错误
the trait `Sized` is not implemented for `(dyn T + 'static)`
,但是当我添加时
trait T:Sized{}
我得到
the trait cannot be made into an object because it requires `Self: Sized`
Let's say I have the following struct
trait T{}
struct A<X:T>{
...
}
I'm wondering whether something like this is possible
Box<A<dyn T>>
Currently I get errors
the trait `Sized` is not implemented for `(dyn T + 'static)`
but when I add
trait T:Sized{}
I get
the trait cannot be made into an object because it requires `Self: Sized`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的,但泛型默认具有
Sized
绑定,这会阻止它们被未调整大小的特征对象实例化。您需要指定T: ?Sized
:例如:
游乐场。
It is possible, but generics have the
Sized
bound by default, which prevents them from being instantiated by a trait object, which is unsized. You need to specifyT: ?Sized
:E.g.:
Playground.