[英]Cannot infer an appropriate lifetime for autoref due to conflicting requirements when tried to set a return value with lifetime to a struct
pub struct SumAggregator<'ctx> {
input: i32,
state: Cell<Option<IntValue<'ctx>>>,
}
impl<'ctx> SumAggregator<'ctx> {
pub fn new() -> SumAggregator<'ctx> {
SumAggregator {
input: 0,
state: Cell::new(None),
}
}
}
impl<'ctx> Aggregator for SumAggregator<'ctx> {
fn init(&self, generator: &Generator, layout: &mut Layout, idx: i32) {
let col_type = generator.context.i32_type();
self.state.set(Some(col_type.const_int(0, true)));
generator.build_debug("initialized state value:", self.state.get().unwrap().as_basic_value_enum());
}
fn process(&self, val: IntValue) {
unimplemented!()
}
}
Above is the core code which throws this error.以上是引发此错误的核心代码。
col_type.const_int(0, true)
can return a IntValue
with a 'ctx lifetime. col_type.const_int(0, true)
可以返回具有 'ctx 生命周期的IntValue
。 When I tried to set this value to my struct, this error occured.当我尝试将此值设置为我的结构时,发生了此错误。 I am new to Rust.
我是 Rust 的新手。 As far as I know, only references could cause lifetime problem.
据我所知,只有引用可能会导致终身问题。 However, in my use case, I just want to put a value not a reference to a struct(even though this value has a lifetime).
但是,在我的用例中,我只想放置一个值而不是对结构的引用(即使该值具有生命周期)。
Here is the error stack:这是错误堆栈:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/operator/groupby/mod.rs:40:42
|
40 | let col_type = generator.context.i32_type();
| ^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #3 defined on the method body at 37:5...
--> src/operator/groupby/mod.rs:37:5
|
37 | fn init(&self, generator: &Generator, layout: &mut Layout, idx: i32) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/operator/groupby/mod.rs:40:24
|
40 | let col_type = generator.context.i32_type();
| ^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'ctx` as defined on the impl at 36:6...
--> src/operator/groupby/mod.rs:36:6
|
36 | impl<'ctx> Aggregator for SumAggregator<'ctx> {
| ^^^^
note: ...so that the expression is assignable
--> src/operator/groupby/mod.rs:41:24
|
41 | self.state.set(Some(col_type.const_int(0, true)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `Option<inkwell::values::IntValue<'ctx>>`
found `Option<inkwell::values::IntValue<'_>>`
The problem is that SumAggregator
wants to have an IntValue
that lives at least as long as itself.问题是
SumAggregator
希望拥有一个至少与自身一样长的IntValue
。 But in the init
function you are trying to give it an IntValue
that lives only until the end of the init
function.但是在
init
function 中,你试图给它一个IntValue
,它只存在于init
function 结束之前。
You can make sure that the IntValue
lives long enough by specifying the Generator
's reference's lifetime:您可以通过指定
Generator
的引用的生命周期来确保IntValue
的生命周期足够长:
impl<'ctx> Aggregator for SumAggregator<'ctx> {
fn init(&self, generator: &'ctx Generator, layout: &mut Layout, idx: i32) {
// col_type is &'ctx IntType
let col_type = generator.context.i32_type();
// int_value is &'ctx IntValue
let int_value = col_type.const_int(0, true)
// putting &'ctx IntValue into &'ctx IntValue
self.state.set(Some(int_value));
generator.build_debug("initialized state value:", self.state.get().unwrap().as_basic_value_enum());
}
...
}
Without doing that it is the same as:如果不这样做,则与以下内容相同:
impl<'ctx> Aggregator for SumAggregator<'ctx> {
fn init<'a, 'b>(&self, generator: &'a Generator, layout: &'b mut Layout, idx: i32) {
// col_type is &'a IntType
let col_type = generator.context.i32_type();
// int_value is &'a IntValue
let int_value = col_type.const_int(0, true)
// trying to put &'a IntValue into &'ctx IntValue
self.state.set(Some(int_value));
generator.build_debug("initialized state value:", self.state.get().unwrap().as_basic_value_enum());
}
...
}
You need to specific all of lets for example:您需要指定所有让,例如:
let int_value:IntValue<'ctx> = col_type.const_int(0, true)
And repeat it for Option and cell left-s.并对 Option 和单元格 left-s 重复此操作。
Consider to a,b,ctx they need more management and if it is possible try it once:考虑到 a,b,ctx 他们需要更多的管理,如果可能的话尝试一次:
impl<'a,'b,'ctx>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.