简体   繁体   English

尝试将具有生命周期的返回值设置为结构时,由于存在冲突的要求,无法推断出 autoref 的适当生命周期

[英]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.

相关问题 由于需求冲突,无法为 autoref 推断适当的生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements Rust:由于要求冲突,无法推断 autoref 的适当生命周期 - Rust: cannot infer an appropriate lifetime for autoref due to conflicting requirements `由于需求冲突,无法为 autoref 推断适当的生命周期`,但由于特征定义约束,无法更改任何内容 - `cannot infer an appropriate lifetime for autoref due to conflicting requirements` but can't change anything due to trait definition constraints 由于递归结构中的冲突要求,无法推断出适当的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements 错误:由于需求冲突,无法推断autoref的适当生命周期[E0495] - error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495] Rust:错误[E0495]:由于需求冲突,无法为 autoref 推断合适的生命周期 - Rust: error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements 创建引用错误的递归列表“由于要求冲突,无法推断 autoref 的适当生命周期” - Creating a recursive list of references errors with "cannot infer an appropriate lifetime for autoref due to conflicting requirements" 将结构转换为具有生存期的特征得到“由于需求冲突,无法为生存期参数&#39;a&#39;推断适当的生存期” - casting struct to trait with lifetime got “cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements” 由于对由具有 Serde Deserialize 的枚举组成的结构的要求相互冲突,因此无法为生命周期参数“de”推断合适的生命周期 - cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements for struct made of enums with Serde Deserialize
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM