【问题标题】:cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements由于需求冲突,无法推断出自动强制的适当生命周期
【发布时间】:2014-06-08 17:51:51
【问题描述】:

我收到此错误 - “由于要求冲突,无法推断自动强制的适当生命周期”。但是,我尝试明确强制执行start_duty 要求。

error.rs:45:1: 55:2 note: consider using an explicit lifetime parameter as shown: fn start_duty<'dutylife>(duty: &'dutylife Duty) -> &'dutylife Job<'dutylife>
error.rs:45 fn start_duty<'dutylife> (duty: &'dutylife Duty) -> &'dutylife Job {
error.rs:46 
error.rs:47     let j : Job = Job {
error.rs:48         duty: duty,
error.rs:49         output: "".to_string(),
error.rs:50         success: JobNotDone
            ...
error.rs:48:15: 48:19 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
error.rs:48         duty: duty,
                          ^~~~
error: aborting due to previous error

我的代码的一些删减版本会导致错误。从概念上讲,我想做的是生成一个引用职责的新工作。工作只能在职责的整个生命周期内存在;当职责消失时,工作也应该消失。

enum Source {
    Nothing,                        // Nothing
    Git(String, String),            // reponame, refname
    Hg(String, String),             // reponame, csid
    Url(String)                     // curl down what's here
}

enum JobResult {
    JobNotDone,
    JobSuccess,
    JobFailure,
    JobError
}

/*
Jobs

Jobs are always attached to the Duty that spawned them; there can be
no Job without the duty. So we take a lifetime param of the duty reference
*/
struct Job<'r> {
    duty:  &'r Duty,            // pointer back to
    output: String,             // no output = ""
    success: JobResult
}

enum Action {
    BashScript(String)
}

struct Duty {
    name: String,
    source: Source,
    action: Action,
    comment: Option<String>
}

struct Agent<'r> {
    hostname : String,
    uid : u64,
    job : Option<Job<'r>>,                  // mutable, agents
}

// returns new Job, but with duty referenced.
fn start_duty<'dutylife> (duty: &'dutylife Duty) -> &'dutylife Job {

    let j : Job = Job {
        duty: duty,
        output: "".to_string(),
        success: JobNotDone

    };

    return &j;
}


fn main () {
}

【问题讨论】:

    标签: rust borrow-checker


    【解决方案1】:

    此函数签名承诺返回对 Job 的引用。

    fn start_duty<'dutylife> (duty: &'dutylife Duty) -> &'dutylife Job
    

    您可能想要做的是返回一个包含对Duty 的引用的Job

    fn start_duty<'dutylife> (duty: &'dutylife Duty) -> Job<'dutylife> {
    
        Job {
            duty: duty,
            output: "".to_string(),
            success: JobNotDone
        }
    
    }
    

    还有另一个错误,代码试图返回对该函数中创建的作业的引用。我也修复了这个问题,代码现在可以编译了。让我知道这是否是你想要做的。

    编辑:回应“工作只能在职责的生命周期内存在;当职责消失时,工作也应该存在。”部分。

    这不能以您尝试的方式完成,因为当函数结束时 Job 对象将不复存在,并且对它的任何引用都将变为无效。

    最简单的方法是让Duty 拥有处理它的Job(s)(通过给它一个Option&lt;Job&gt;Option&lt;Vec&lt;Job&gt;&gt; 字段)。这是单一所有者的方法。多个所有者要复杂得多,并且会涉及引用计数指针或原始指针。

    【讨论】:

    • 嗯,我试图返回一个参考,我不想转移一个副本(虽然tbh,不确定最终结果是什么)。我在考虑你的所有权言论。无论如何,我会继续前进,当我搞砸时,借款检查员会冲我大喊大叫。 :)
    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 2016-06-01
    • 2021-08-02
    • 2020-01-29
    • 2021-11-02
    • 2022-01-06
    • 2020-05-18
    • 2017-07-12
    相关资源
    最近更新 更多