- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这曾经有效:
struct Foo<'a, T> {
parent:&'a (Array<T> + 'a)
}
impl<'a, T> Foo<'a, T> { //'
pub fn new<T>(parent:&Array<T>) -> Foo<T> {
return Foo {
parent: parent
};
}
}
trait Array<T> {
fn as_foo(&self) -> Foo<T> {
return Foo::new(self);
}
}
fn main() {
}
:15:21: 15:25 error: the trait
core::kinds::Sized
is not implemented for the typeSelf
:15 return Foo::new(self);
最佳答案
这里发生了两件事:特征对象强制(错误)和对象安全(修复它)。
错误
正如错误消息所暗示的,代码的困难部分是 Foo::new(self)
, 这是因为 pub fn new<T>(parent: &Array<T>) -> ...
,即self
被强制转换为 &Array<T>
特征对象。我将代码简化为:
trait Array {
fn as_foo(&self) {
let _ = self as &Array; // coerce to a trait object
}
}
fn main() {}
<anon>:3:13: 3:27 error: the trait `core::kinds::Sized` is not implemented for the type `Self`
<anon>:3 let _ = self as &Array; // coerce to a trait object
^~~~~~~~~~~~~~
Self
是实现特征的类型的替代名称。与大多数通用参数不同,
Self
默认情况下可能是未调整大小的 (
?Sized
),因为
RFC 546和
#20341出于允许的目的,例如
impl Array<T> for Array<T>
默认情况下更频繁地工作(我们稍后会谈到)。
self
有类型
&Self
.如果
Self
是一个大小的类型,那么这是一个普通的引用:一个单一的指针。如果
Self
是无大小的类型(如
[T]
或特征),然后是
&Self
(
&[T]
或
&Trait
)是一个切片/特征对象:一个胖指针。
&T
可以转换为特征对象的是
T
大小:Rust 不支持使胖指针变胖,只有瘦指针 → 胖指针有效。因此,由于编译器不知道
Self
将永远是
Sized
(记住,它是特殊的,默认为
?Sized
)它必须假设最坏的情况:强制是不合法的,因此它是不允许的。
Self: Sized
当我们想做强制时。这样做的明显方法是使
Self
总是
Sized
,即覆盖默认的
?Sized
绑定(bind)如下:
trait Array: Sized {
fn as_foo(&self) {
let _ = self as &Array; // coerce to a trait object
}
}
fn main() {}
Sized
Self
是破坏对象安全的事情之一:
<anon>:3:13: 3:17 error: cannot convert to a trait object because trait `Array` is not object-safe [E0038]
<anon>:3 let _ = self as &Array; // coerce to a trait object
^~~~
<anon>:3:13: 3:17 note: the trait cannot require that `Self : Sized`
<anon>:3 let _ = self as &Array; // coerce to a trait object
^~~~
<anon>:3:13: 3:17 note: the trait cannot require that `Self : Sized`
<anon>:3 let _ = self as &Array; // coerce to a trait object
^~~~
trait ArrayExt: Sized + Array { fn as_foo(&self) { ... } }
并为所有 Sized + Array
实现类型 fn array_as_foo<A: Array>(x: &A) { ... }
where
我们可以非常具体地确定何时
Self
的子句应该执行
Sized
,将其限制为仅需要它的方法,而不会感染特征的其余部分:
trait Array {
fn as_foo(&self) where Self: Sized {
let _ = self as &Array; // coerce to a trait object
}
}
fn main() {}
where
像这样的子句,编译器理解 (a) 强制是合法的,因为
Self
是
Sized
所以
self
是一个细指针,并且 (b) 无论如何调用 trait 对象的方法都是非法的,因此不会破坏对象的安全性。要看到它被禁止,更改
as_foo
的正文到
let x = self as &Array; // coerce to a trait object
x.as_foo();
<anon>:4:7: 4:15 error: the trait `core::kinds::Sized` is not implemented for the type `Array`
<anon>:4 x.as_foo();
^~~~~~~~
where
一样简单。
as_foo
的子句方法:
struct Foo<'a, T> { //'
parent:&'a (Array<T> + 'a)
}
impl<'a, T> Foo<'a, T> {
pub fn new(parent:&Array<T>) -> Foo<T> {
return Foo {
parent: parent
};
}
}
trait Array<T> {
fn as_foo(&self) -> Foo<T> where Self: Sized {
return Foo::new(self);
}
}
fn main() {
}
<T>
中不必要的
pub fn new<T>
,因为这会导致推理失败。)
关于rust - 什么是 'core::kinds::Sized` 没有为 rust 中的 `Self' 类型实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27816023/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!