简体   繁体   English

由于需求冲突,无法推断出自动强制的适当寿命

[英]cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements

I am getting this error - "cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements". 我收到此错误-“由于需求冲突,无法推断出自动强制的适当寿命”。 However, I've tried to enforce start_duty requirements explicitly. 但是,我尝试了明确执行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

A somewhat expurgated version of my code inducing the error. 我的代码有点过时的版本会导致错误。 Conceptually, what I am trying to do is generate a new Job referencing a Duty. 从概念上讲,我试图做的是生成一个引用职责的新Job。 Jobs can only exist for the lifetime of the Duty; 工作只能在职责的整个生命周期中存在; when the Duty goes away, the Job should as well. 当职责消失时,工作也应如此。

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

This function signature promises to return a reference to a Job. 此功能签名承诺返回对作业的引用。

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

What you probably wanted to do was returning a Job that contains a reference to a Duty : 您可能想做的是返回一个包含对Duty的引用的Job

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

    Job {
        duty: duty,
        output: "".to_string(),
        success: JobNotDone
    }

}

There was also another error, the code was trying to return a reference to the Job created in this function. 还有另一个错误,代码试图返回对该函数中创建的Job的引用。 I fixed that as well and the code now compiles. 我也修复了该问题,现在可以编译代码。 Let me know if this was what you were trying to do. 让我知道您是否正在尝试这样做。

Edit: to respond to the "Jobs can only exist for the lifetime of the Duty; when the Duty goes away, the Job should as well." 编辑:回应“工作只能在职责的整个生命周期中存在;当职责消失时,工作也应该存在。” part. 部分。

This cannot be done in the way you've tried because the Job object will cease to exist when the function ends, and any references to it would become invalid. 这无法用您尝试的方式完成,因为当函数结束时,Job对象将不复存在,并且对其的任何引用都将变为无效。

The simplest way would be to let a Duty own the Job (s) working on it (by giving it a Option<Job> or Option<Vec<Job>> field). 最简单的方法是让Duty拥有在其上Job的工作(通过给它一个Option<Job>Option<Vec<Job>>字段)。 This is a single owner approach. 这是单一所有者的方法。 Multiple owners are significantly more complex and would involve reference counted pointers, or raw pointers. 多个所有者要复杂得多,并且会涉及引用计数的指针或原始指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements 匿名函数 - 由于需求冲突,无法推断出合适的生命周期 - Anonymous function - cannot infer an appropriate lifetime due to conflicting requirements 由于递归结构中的冲突要求,无法推断出适当的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct 由于需求冲突,无法为 autoref 推断适当的生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements 作为函数参数的闭包“由于需求冲突而无法推断出适当的寿命” - Closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements” Rust:由于要求冲突,无法推断 autoref 的适当生命周期 - Rust: cannot infer an appropriate lifetime for autoref due to conflicting requirements 尝试实现迭代器:由于需求冲突而无法推断出适当的生存期 - Trying to implement an iterator: cannot infer an appropriate lifetime due to conflicting requirements 由于需求冲突,无法为借用表达式推断出适当的生命周期 - cannot infer an appropriate lifetime for borrow expression 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