簡體   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!()
    }
}

以上是引發此錯誤的核心代碼。 col_type.const_int(0, true)可以返回具有 'ctx 生命周期的IntValue 當我嘗試將此值設置為我的結構時,發生了此錯誤。 我是 Rust 的新手。 據我所知,只有引用可能會導致終身問題。 但是,在我的用例中,我只想放置一個值而不是對結構的引用(即使該值具有生命周期)。

這是錯誤堆棧:

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 function 中,你試圖給它一個IntValue ,它只存在於init function 結束之前。

您可以通過指定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());
    }

    ...
}

如果不這樣做,則與以下內容相同:

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

    ...
}

您需要指定所有讓,例如:

let int_value:IntValue<'ctx> = col_type.const_int(0, true)

並對 Option 和單元格 left-s 重復此操作。

考慮到 a,b,ctx 他們需要更多的管理,如果可能的話嘗試一次:

impl<'a,'b,'ctx>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM