- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 &'a [u8]
上实现一个特征,并在使用更高等级生命周期的实现中使用它,例如:
pub trait IntoTest<'a, T> {
fn into_test(&'a self) -> T where Self: Sized;
}
impl<'a> IntoTest<'a, &'a [u8]> for &'a [u8] {
fn into_test(&'a self) -> &'a [u8] {
self
}
}
fn higher_ranked_lifetime<T>(test: T) where T: for<'a> IntoTest<'a, &'a [u8]> {
let _t = test.into_test();
}
fn main() {
println!("Hello, world!");
let vec = vec![1u8];
let slice = &vec[..];
higher_ranked_lifetime(slice);
}
我得到的错误是:
<anon>:19:5: 19:27 error: the trait `for<'a> IntoTest<'a, &'a [u8]>` is not implemented for the type `&[u8]` [E0277]
<anon>:19 higher_ranked_lifetime(slice);
我应该怎么做?这甚至是正确的做法吗?我不想将我的 higher_ranked_lifetime
限定在特定的生命周期内,但它适用于传递给它的任何 byte slice 。
最佳答案
这不是正确的方法,编译器是正确的。
要了解为什么会这样,一种方法是同时查看类型(切片)和您认为它实现的特征:
for<'a> IntoTest<'a, &'a [u8]>
&'a [u8]
在这里,为了清楚起见,我在切片上添加了一个生命周期参数。您可以看到在特征中绑定(bind)了生命周期参数 'a
是bound通过 for
限定符,换句话说,这个声明是自包含的,它不依赖于外部范围内的任何东西。
然而,在切片中,生命周期参数 'a
自由 - 它在某些外部范围内定义,例如,通过生命周期省略。因此,切片根本无法满足您的特征实现。为了让它发挥作用,你的特质应该像这样实现:
impl<'a, 'b> IntoTest<'a, &'a [u8]> for &'b [u8] {
fn into_test(&'a self) -> &'a [u8] {
unimplemented!()
}
}
哪个compiles ,尽管它没有也不能做你想做的事。您可以看到,在此声明中,切片的生命周期和特征签名中的生命周期是不相交的,因此切片现在确实满足 for<'a>
。基于绑定(bind)。
还有另一种方式来看待这个问题。 T: for<'a> IntoTest<'a, &'a [u8]>
绑定(bind)意味着类型 T
为特征的每个可能的生命周期参数实现特征,因此函数本身可以决定它想要的生命周期。例如,它可以请求 'static
.但很自然地在你的代码中 &[u8]
绑定(bind)到 main 方法中的向量,不能是 'static
.因此违反了健全性-这&[u8]
无法提供用户想要的任何生命周期,因此它不会实现 for<'a> IntoTest<'a, &'a [u8]>
.
请注意,仅向泛型函数添加生命周期参数也不起作用:
fn higher_ranked_lifetime<'a, T>(test: T) where T: IntoTest<'a, &'a [u8]> {
let _t = test.into_test();
}
会出现这样的错误:
<anon>:12:14: 12:18 error: `test` does not live long enough
<anon>:12 let _t = test.into_test();
^~~~
<anon>:11:79: 13:2 note: reference must be valid for the lifetime 'a as defined on the block at 11:78...
<anon>:11 fn higher_ranked_lifetime<'a, T>(test: T) where T: IntoTest<'a, &'a [u8]> {
<anon>:12 let _t = test.into_test();
<anon>:13 }
<anon>:11:79: 13:2 note: ...but borrowed value is only valid for the scope of parameters for function at 11:78
<anon>:11 fn higher_ranked_lifetime<'a, T>(test: T) where T: IntoTest<'a, &'a [u8]> {
<anon>:12 let _t = test.into_test();
<anon>:13 }
这又是正确的。请记住,您的特质是这样定义的:
pub trait IntoTest<'a, T> {
fn into_test(&'a self) -> T where Self: Sized;
}
在这里你需要self
通过引用传递,其生命周期与特征的生命周期参数相同。但是,上面泛型函数的声明使得这个方法原则上是不可调用的:
fn higher_ranked_lifetime<'a, T>(test: T) where T: IntoTest<'a, &'a [u8]>
这里指定特征的生命周期参数等于函数的生命周期参数。然而,test
参数本身仅存在于函数体中。因此,对 test
的任何引用,包括调用 into_test()
时的隐式:
let _t = (&test).into_test();
的生命周期将严格小于生命周期参数,因此它不能用作 into_test()
的参数。方法。这正是错误的原因。
因为你没有说明你真正需要什么,所以很难说你应该做什么。我想最通用的方法之一就是将生命周期参数放在特征上并使特征方法接受 self
按值,并相应地修复通用函数:
pub trait IntoTest<T> {
fn into_test(self) -> T;
}
impl<'a> IntoTest<&'a [u8]> for &'a [u8] {
fn into_test(self) -> &'a [u8] {
self
}
}
fn higher_ranked_lifetime<'a, T>(test: T) where T: IntoTest<&'a [u8]> {
let _t = test.into_test();
}
fn main() {
println!("Hello, world!");
let vec = vec![1u8];
let slice = &vec[..];
higher_ranked_lifetime(slice);
}
这does work因为现在 trait 本身没有任何生命周期参数,也没有添加任何关于如何调用它的要求。生命周期参数现在已移至实现它的类型。
关于types - 无法为字节切片实现更高等级的生命周期类型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197330/
简而言之: 我怎样才能切片?也就是说,能够指定我想从多个索引(例如 y = x[(2, 5, 11)] )中提取,而不仅仅是单个索引(例如 y = x[2] )。 简单示例 : 说我有这个数据: d
是否可以在 F# 中对 Array2D 进行切片? 说,let tmp =Array2D.init 100 100 (fun x y -> x * 100 + y) 如何从 tmp 中检索某些列或某些
例如,我希望文本仅显示“此处”,但它不起作用。文本经常变化,但我需要的单词保持在固定位置。我想访问该词。 我做错了什么? function myFunction() { var x = doc
当尝试使用spring的分页或切片来迭代非常大的mongodb集合时,程序运行正常,但在某些时候下一页/切片为空,并且在调试时出现“包含未知实例的页面/切片”消息. 这是代码示例: do { Pa
有人能给我一个关于如何分割 ListView 的例子吗?我正在使用 SimpleCursorAdapter 在 ListView 中显示数据.. 我的代码是这样的。 private WordDbAda
这个问题在这里已经有了答案: C++ slicing causing leak / undefined behavior / crash (3 个答案) 关闭 8 年前。 如果我有如下代码: cla
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 5 年前。 我目前有 500 行数据。我想使用前五十行,然后跳过 50 行,依此类推。我该如何继续这
为什么对一行或一列进行切片会产生“无量纲数组”?例如: import numpy as np arr = np.zeros((10,10)) print arr.shape # (10, 10) 但是
我有以下 pandas 数据框: Shortcut_Dimension_4_Code Stage_Code 10225003 2 8225003
如何通过数组为 ruby 中的散列创建切片,如下所示: info = { :key1 => "Lorem", :key2 => "something...", :key3 => "
这个问题在这里已经有了答案: regex to get all text outside of brackets (4 个答案) 关闭 5 年前。 我正在编写的这个程序接收到一个大小不同的字符串,其
我尝试使用 tf.Tensor.getitem 对张量进行切片功能如下: indices = [0, 5] data[:,:,indices] 但是我得到以下错误: TypeError: can on
这个问题在这里已经有了答案: Can I create a "view" on a Python list? (10 个答案) 关闭 7 年前。 有没有一种方法可以在 Python 3 中创建序列的
我想弄清楚如何从多维数组中获取单个维度(为了论证,假设它是二维的),我有一个多维数组: double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4,
我有一个 std::vector。我想创建代表该 vector 切片的迭代器。我该怎么做?在伪 C++ 中: class InterestingType; void doSomething(slice
写在前面 前面的文章介绍了Go的一些基本类型,本文开始涉及Go的一些容器类型,它们都是可以包含多个元素的数据结构,如数组、切片、map 数组 数组是具有相同类型且长度固定的一组元素集合,定义的格式:v
给定一个 numpy 数组和一个 __getitem__ 类型的索引,是否有一种惯用的方法来获取数组的相应切片,总是返回一个数组而不是标量? 有效索引的示例包括:int、slice、省略号或上述的元组
我创建了一个继承自 pandas.DataFrame 的类。在此类中添加了元数据(不是添加到列中,而是添加到类实例中): class MeasurementPoint(pandas.DataFrame
我想在空间上剪切视频以生成 N x M 个文件。 例如,我想把 test.video 拆分成 NxM 的瓦片? Video tiles 最佳答案 您可以使用 ffmpeg 及其 crop filter
这是一个示例代码。比如我想拉德国 在页面加载时切片。在这段代码中,它拉取第一个切片。 无功图; var 传说; var chartData = [{ 国家:“立陶宛”, 值:260}, { 国家:“爱
我是一名优秀的程序员,十分优秀!