简体   繁体   English

使用 Any 时如何处理“类型不满足所需的生命周期”?

[英]How to handle “the type does not fulfill the required lifetime” when using Any?

use std::any::Any;

pub enum ObjectType {
    Error,
    Function,
}

pub trait Object {
    fn obj_type(&self) -> ObjectType;

    // Required to downcast a Trait to specify structure
    fn as_any(&self) -> &dyn Any;
}

#[derive(Debug)]
pub struct Function<'a> {
    params: &'a Box<Vec<Box<String>>>,
}

impl<'a> Object for Function<'a> {
    fn obj_type(&self) -> ObjectType {
        ObjectType::Function
    }

    fn as_any(&self) -> &dyn Any { 
        self 
    }
}

When I try compiling the above code I get the following error.当我尝试编译上述代码时,出现以下错误。 Here is the playground link to the code这是代码的操场链接

Compiling playground v0.0.1 (/playground)
error[E0477]: the type `Function<'a>` does not fulfill the required lifetime
  --> src/lib.rs:27:9
   |
27 |         self 
   |         ^^^^
   |
   = note: type must satisfy the static lifetime

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/lib.rs:27:9
   |
27 |         self 
   |         ^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 21:7...
  --> src/lib.rs:21:7
   |
21 | impl <'a>Object for Function<'a> {
   |       ^^
note: ...so that the type `Function<'a>` will meet its required lifetime bounds
  --> src/lib.rs:27:9
   |
27 |         self 
   |         ^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
  --> src/lib.rs:27:9
   |
27 |         self 
   |         ^^^^
   = note: expected `&(dyn Any + 'static)`
              found `&dyn Any`

Why is Rust compiler getting confused about the struct lifetime, since I am just returning self.为什么 Rust 编译器对结构生命周期感到困惑,因为我只是返回 self. Also does it want the lifetime to be 'static.它还希望生命周期是“静态的”吗? It's quite frustrating learning the language.学习语言真是令人沮丧。

The main issue is the as_any method defined in Function .主要问题是Function中定义的as_any方法。 If you look at the Any trait definition in the documentation , you will see that the trait has a lifetime bound of 'static .如果您查看文档中的Any特征定义,您会看到该特征的生命周期为'static The params field in Function has a lifetime of 'a which won't live as long as a lifetime of 'static . Function中的params字段的生命周期为'a ,其生命周期不如'static的生命周期。 One solution is to define the params field as a Box<Vec<Box<String>>> , instead of using a reference and defining a lifetime, but without further context, it is difficult to say.一种解决方案是将params字段定义为Box<Vec<Box<String>>> ,而不是使用引用并定义生命周期,但如果没有进一步的上下文,很难说。

暂无
暂无

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

相关问题 在线程中使用方法时,“类型不满足所需的生命周期” - “the type does not fulfill the required lifetime” when using a method in a thread 无法在产生的线程中调用函数,因为它“未达到所需的生存期” - Cannot call a function in a spawned thread because it “does not fulfill the required lifetime” 在装箱的将来使用参考变量时,“需要显式寿命” - “explicit lifetime required” when using a reference variable in a boxed future 如何限制关联类型接受任何引用生命周期? - How can I constrain an associated type to accept any reference lifetime? 当泛型类型受泛型生命周期限制时,这意味着什么? - What does it mean when a generic type is bounded by a generic lifetime? 使用泛型关联类型时,如何指示类型参数的生命周期? - How do I indicate a lifetime bound on a type parameter when using generic associated types? 为什么使用“Self”作为参数类型会引发生命周期错误? - Why does using "Self" as a parameter type raise a lifetime error? 为什么在使用嵌套的可变引用时,我会收到错误“无法推断泛型参数的适当生命周期”? - Why do I get the error “cannot infer an appropriate lifetime for lifetime parameter in generic type” when using nested mutable references? 使用相关类型的特征与生命周期参数的生命周期错误 - Lifetime error using associated type of trait with lifetime parameter 具有 static 寿命的类型意味着什么? - What does it mean for a type to have a static lifetime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM