包含带有特征参数的结构的盒子的大小 [英] Size of a box containing a struct with a trait parameter

查看:48
本文介绍了包含带有特征参数的结构的盒子的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个包含特征对象和更多自身特征的结构.令人失望的是以下代码无法编译:

I need a struct that contains a trait object and more of itself. Disappointedly the following code does not compile:

trait Foo {}

struct Bar<T: Foo> {
    bars: Vec<Box<Bar<dyn Foo>>>,
    foo: T,
}

我设法通过添加绑定到 T ?Sized 来强制将其编译,但是我不明白为什么会这样.我认为这是因为所有特征对象都具有相同的大小,但是 Bar 的大小取决于具体类型 T 的大小.如果是这样的话,在内存中如何表示具有不大的 T Bar ?具体来说,是什么在堆上跟踪其大小,以及为什么不能在大小写情况下使用此机制.

I managed to coerce this into compiling by adding the ?Sized bound to T, but I do not understand why this should be the case. I assume this is because all trait objects have the same size, but the size of Bar depends on the size of the concrete type T. If so, how is Bar with an unsized T represented in memory? Specifically what tracks its size on the heap and why can not this mechanism be used in the sized case.

推荐答案

类型 dyn Foo 的大小在编译时未知.因此,当您编写 Bar< dyn Foo> 时,编译器将不允许使用它,因为(默认情况下)类型参数的大小必须确定.编译器建议您通过允许减小 T 的大小来解决此问题,这对于使 T 成为 dyn Foo 是必需的.

The type dyn Foo does not have a size known at compile time. So when you write Bar<dyn Foo>, the compiler won't allow it because (by default) type parameters must be sized. The compiler suggests that you fix this by allowing T to be unsized, which is necessary for T to be dyn Foo.

在内存中如何表示带有未设置大小的 T Bar ?

一个结构最多可以有一个大小不一的字段.然后将其数据首先放在大小字段中,然后再放在未大小字段中,然后放在内存中.此限制意味着可以在编译时知道所有字段的相对内存地址.具有?Sized 类型参数的结构本身可以调整大小,也可以不调整大小,具体取决于其参数的具体类型.当该结构未调整大小时,它无法进入堆栈,因此只能从指针后面使用它.

A struct is allowed to have at most one unsized field. Its data is then laid out in memory with the sized fields first, and then the unsized field last. This restriction means that relative memory addresses of all fields can be known at compile-time. A struct with a ?Sized type parameter can itself be either sized or unsized, depending on the concrete type of its argument. When the struct is unsized, it can't go on the stack so you can only use it from behind a pointer.

目前没有有关此类对象的文档.它不完全是一个特征对象,但它是指向可能未调整大小的对象的指针.如您的示例所示,这可行.但由于我不知道,所以我无法告诉您 vtable 指针的存储位置,而且我不确定如何查找.

There is currently no documentation for this kind of object. It's not exactly a trait object, but it's a pointer to something that may not be sized. As your example shows, this works. But I cannot tell you where the vtable pointer is stored because I don't know, and I'm not sure how to find out.

具体是什么会在堆上跟踪其大小,以及为什么不能在大小写的情况下使用此机制.

Specifically what tracks its size on the heap and why can not this mechanism be used in the sized case.

每个对象的大小实际上并没有改变-每个实例可能有所不同.可以在大小不一的情况下"使用机制 ,但是您没有大小写的情况!即使对于已调整大小的 T bars 集合也将包含未调整大小的 Bar< dyn Foo> 框.这就是为什么您需要 T:?Sized (而不是 T:!Sized ),说这种类型适用于 T 大小或未调整大小.

The size of each object doesn't actually change - it's just potentially different per instance. The mechanism can be used "in the Sized case", but you don't have a sized case! Even for a T that is sized, the bars collection will contain boxes of Bar<dyn Foo> which are unsized. That's why you need to T: ?Sized (as opposed to T: !Sized), to say that this type works for T being either sized or unsized.

这篇关于包含带有特征参数的结构的盒子的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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