使用 returns 与一个参数具有相同生命周期的关联类型的函数定义特征
Define a trait with a function that returns an associated type with the same lifetime as one parameter
我正在尝试用一个函数定义特征,returns 一个与一个参数具有相同生命周期的关联类型。
概念上类似于以下内容(不起作用:lifetime parameter not allowed on this type [Self::Output]
):
trait Trait {
type Output;
fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>;
}
我在 Stack Overflow 和 Internet 上发现了几个关于关联类型生命周期的问题,但 none 似乎有所帮助。有人建议定义整个特征的生命周期:
trait Trait<'a> {
type Output;
fn get_output(&self, input: &'a i32) -> Self::Output;
}
但这也不起作用:它编译了,但是下面的函数无法编译:
fn f<'a, T>(var: &T)
where T: Trait<'a>
{
let input = 0i32;
let output = var.get_output(&input);
}
报错:
error: `input` does not live long enough
--> <anon>:9:35
|
| let output = var.get_output( &input );
| ^^^^^ does not live long enough
| }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 7:48...
--> <anon>:7:49
|
| fn f<'a, T>( var : &T ) where T : Trait<'a> {
| _________________________________________________^ starting here...
| | let input = 0i32;
| | let output = var.get_output( &input );
| | }
| |_^ ...ending here
我应该如何定义特征以使其按照我想要的方式运行?
目前这是不可能的,即使在夜间 Rust 中也是如此。
这需要某种形式的高级类型 (HKT),当前设想的方法称为关联类型构造函数 (ATC)。
引入 ATC 的主要动机实际上就是这个用例。
对于 ATC,语法为:
trait Trait {
type Output<'a>;
fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>;
}
注意:如果您想了解 ATC,请参阅 Niko's series of articles。
对于您所拥有的特定示例,您可以使用 Shepmaster 指出的 HRTB(高级特征界限)解决此问题:
fn f<T>(var: &T)
where for<'a> T: Trait<'a>
{ ... }
然而这是相当有限的(值得注意的是,仅限于特征范围)。
这需要使用 higher-ranked trait bounds:
fn f<T>(var: &T)
where for<'a> T: Trait<'a>
{
let input = 0i32;
let output = var.get_output(&input);
}
另请参阅:
我正在尝试用一个函数定义特征,returns 一个与一个参数具有相同生命周期的关联类型。
概念上类似于以下内容(不起作用:lifetime parameter not allowed on this type [Self::Output]
):
trait Trait {
type Output;
fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>;
}
我在 Stack Overflow 和 Internet 上发现了几个关于关联类型生命周期的问题,但 none 似乎有所帮助。有人建议定义整个特征的生命周期:
trait Trait<'a> {
type Output;
fn get_output(&self, input: &'a i32) -> Self::Output;
}
但这也不起作用:它编译了,但是下面的函数无法编译:
fn f<'a, T>(var: &T)
where T: Trait<'a>
{
let input = 0i32;
let output = var.get_output(&input);
}
报错:
error: `input` does not live long enough
--> <anon>:9:35
|
| let output = var.get_output( &input );
| ^^^^^ does not live long enough
| }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 7:48...
--> <anon>:7:49
|
| fn f<'a, T>( var : &T ) where T : Trait<'a> {
| _________________________________________________^ starting here...
| | let input = 0i32;
| | let output = var.get_output( &input );
| | }
| |_^ ...ending here
我应该如何定义特征以使其按照我想要的方式运行?
目前这是不可能的,即使在夜间 Rust 中也是如此。
这需要某种形式的高级类型 (HKT),当前设想的方法称为关联类型构造函数 (ATC)。
引入 ATC 的主要动机实际上就是这个用例。
对于 ATC,语法为:
trait Trait {
type Output<'a>;
fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>;
}
注意:如果您想了解 ATC,请参阅 Niko's series of articles。
对于您所拥有的特定示例,您可以使用 Shepmaster 指出的 HRTB(高级特征界限)解决此问题:
fn f<T>(var: &T)
where for<'a> T: Trait<'a>
{ ... }
然而这是相当有限的(值得注意的是,仅限于特征范围)。
这需要使用 higher-ranked trait bounds:
fn f<T>(var: &T)
where for<'a> T: Trait<'a>
{
let input = 0i32;
let output = var.get_output(&input);
}
另请参阅: