You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cucumber-Rust测试带生命周期的Rust库遇编译错误

问题:Cucumber-Rust测试中带生命周期结构体的编译错误

我开发了一个Rust库,想用Cucumber-Rust进行测试。库中有带生命周期的结构体,例如:

pub struct Intersection<'a> {
     time: f64,
     object: &'a Shape,
}

库本身运行正常,但测试时定义的测试数据存储结构体出现编译问题:

最初的WorldEntry枚举和CucumberWorld结构体:

#[derive(Debug)]
pub enum WorldEntry {
    Tuple(Tuple),
    Double(f64),
    Color(Color),
    Canvas(Canvas),
    PPM(String),
    Matrix(Matrix),
    Ray(Ray),
    Shape(Shape),
    Intersect(Intersection),
    Interset(Intersections),
    Hit(Option<Intersection>),
    Light(PointLight),
    Material(Material),
}

#[derive(Debug, World)]
#[world(init = Self::new)]
pub struct CucumberWorld {
    all_entries: HashMap<String, WorldEntry>,
}

编译器提示缺少生命周期参数,添加生命周期后:

#[derive(Debug)]
pub enum WorldEntry<'a> {
    Tuple(Tuple),
    Double(f64),
    Color(Color),
    Canvas(Canvas),
    PPM(String),
    Matrix(Matrix),
    Ray(Ray),
    Shape(Shape),
    Intersect(Intersection<'a>),
    Interset(Intersections<'a>),
    Hit(Option<Intersection<'a>>),
    Light(PointLight),
    Material(Material),
}

#[derive(Debug, World)]
#[world(init = Self::new)]
pub struct CucumberWorld<'a> {
    all_entries: HashMap<String, WorldEntry<'a>>,
}

仍出现编译错误:

error[E0106]: missing lifetime specifier
  --> tests/world/mod.rs:26:12
   |
26 | pub struct CucumberWorld<'a> {
   |            ^^^^^^^^^^^^^ expected named lifetime parameter
   |
help: consider introducing a named lifetime parameter
   |
26 | pub struct CucumberWorld<'a><'a><'a> {
   |                         ++++

解决方案

核心原因是Cucumber-Rust的World trait要求实现类型必须满足'static生命周期——测试框架需要在整个测试流程中持有World实例,带自定义生命周期参数的结构体无法满足这个要求。解决思路是消除Intersection的生命周期依赖,或者调整测试数据的存储方式:

1. 将引用改为所有权

修改Intersection结构体,直接持有Shape的所有权而非引用,这样就不需要生命周期参数:

pub struct Intersection {
    time: f64,
    object: Shape,
}

之后WorldEntry和CucumberWorld都不需要添加生命周期,即可正常编译。

2. 使用智能指针共享所有权

如果需要在多个地方共享Shape的实例,可以用Rc(单线程)或Arc(多线程)代替引用,同样消除生命周期:

use std::rc::Rc;

pub struct Intersection {
    time: f64,
    object: Rc<Shape>,
}

这种方式既保留了共享能力,又能让结构体满足'static要求,适配Cucumber的World规则。

3. 不在World中存储带引用的类型

如果必须保留Intersection的引用结构,可以在测试步骤中临时创建和使用该结构体,而非将其存入World的HashMap中。World仅存储拥有所有权的基础数据(比如Shape实例),测试时按需构建带引用的Intersection。


内容的提问来源于stack exchange,提问作者Averell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 18:45:27