gpt4 book ai didi

rust - 尝试为结构设置带有生存期的返回值时,由于需求冲突,因此无法为autoref推断适当的生存期

转载 作者:行者123 更新时间:2023-12-03 11:38:04 25 4
gpt4 key购买 nike


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!()
}
}
上面是引发此错误的核心代码。 col_type.const_int(0, true)可以返回带有'ctx生命周期的 IntValue。当我尝试将此值设置为我的结构时,发生此错误。我是Rust的新手。据我所知,只有引用可能会导致生命周期问题。但是,在我的用例中,我只想放置一个值而不是对struct的引用(即使该值具有生存期)。
这是错误堆栈:
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<'_>>`

最佳答案

问题在于SumAggregator希望拥有一个至少与自己生命周期一样长的IntValue。但是,在init函数中,您尝试为它提供一个IntValue,它仅在init函数结束之前有效。
您可以通过指定IntValue的引用的生存期来确保Generator的生存期足够长:

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());
}

...
}
如果不这样做,它与以下内容相同:
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());
}

...
}

关于rust - 尝试为结构设置带有生存期的返回值时,由于需求冲突,因此无法为autoref推断适当的生存期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66326276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com