Rust in Everything
基于 cu29 1.3.2

常见坑与报错原文排查指南

主线位置:实战排错手册。本页按报错原文列出使用 copper-rs 时最容易踩到的 4 个坑及其源码级根因。

flowchart TB
  err["遇到编译或运行报错"] --> e1["编译期: Cannot find configuration file"]
  err --> e2["编译期: the trait ValueDecode is not implemented"]
  err --> e3["运行期: Section is full / Out of space in section"]
  err --> e4["运行期: rtsan Realtime violation detected"]

坑 1:编译期报错 Cannot find configuration file

  • 触发场景:在结构体上写 #[copper_runtime(config = "copperconfig.ron")] 时宏报错找不到配置文件。
  • 源码根因:core/cu29_derive/src/utils.rs 是相对于当前子 crate 的 CARGO_MANIFEST_DIR(即包含 Cargo.toml 的目录)拼接相对路径的,而不是工作区根目录!
  • 解法:确保 "copperconfig.ron" 放在当前二进制 crate 的 Cargo.toml 同级目录下,且修改 .ron 文件后若未触发增量重编,可在 build.rs 中调用 println!("cargo:rerun-if-changed=copperconfig.ron");。

坑 2:自定义 Payload 报错 the trait bincode::de::Decode<()> is not implemented 或缺少 Reflect

  • 触发场景:自己定义了一个消息结构体 pub struct MyCmd { pub v: f32 } 放进 .ron 的 cnx 中,展开 gen_cumsgs! 时报 Trait 未实现。
  • 源码根因:CuMsgPayload 要求载荷类型同时实现 Default + Debug + Clone + Encode + Decode<()> + Serialize + Deserialize(开启 reflect 或 self-describing-logs 时还需对应的反射派生)。
  • 解法:自定义消息结构体一律派生完整套装,并确认 Cargo.toml 中将 bincode 指向 cu-bincode:
    use bincode::{Decode, Encode};
    use serde::{Deserialize, Serialize};
    
    #[derive(Default, Debug, Clone, PartialEq, Encode, Decode, Serialize, Deserialize)]
    pub struct MyCmd {
      pub v: f32,
    }
    

坑 3:运行期报错单帧 CopperList 超过 section_size_mib

  • 触发场景:在 CopperList 中直接塞入未压缩的高清图像或超大点云数组,运行期 UnifiedLogger 报 Section 空间不足。
  • 源码根因:在 core/cu29_unifiedlog/src/memmap.rs 中,单个 SectionStorage 的容量由 .ron 中的 section_size_mib 决定,若单轮 CopperList 编码后的字节数接近或超过单个 Section 容量,分配器无法容纳。
  • 解法:在 .ron 的 logging: (slab_size_mib: 512, section_size_mib: 64) 中调大 section_size_mib 与 slab_size_mib,或对不需要全量录制的中间大图像在 .ron 节点上设置 logging: (enabled: false)。

坑 4:开启 rtsan 或 memory_monitoring 后发现 process() 中存在隐式堆分配

  • 触发场景:在 process() 中调用了 format!(...) 或 vec![...],被 CountingAlloc 或 rtsan 抓出实时违规。
  • 解法:将状态字符串改为写入 output.metadata.set_status("OK")(底层为 24 字节栈内联 CuCompactString),将动态 Vec 改为 #[derive(Soa)] 定长结构体或启动期预分配的 CuHostMemoryPool。
❗IMPORTANT

动脑筋:如果只想跳过某一个高带宽视觉节点的 CopperList 录制以节省磁盘空间,同时保留 IMU 和控制节点的录制,需要改 Rust 代码吗?

动脑筋答案

完全不需要改 Rust 代码!直接在 copperconfig.ron 对应视觉节点的配置项里加上 logging: (enabled: false),cu29-derive 在生成 CuStampedDataSet 的序列化实现时就会自动跳过该槽位。

小结

  • 掌握 CARGO_MANIFEST_DIR 相对路径、CuMsgPayload 派生要求与 logging 容量配置,即可避开 95% 的常见报错。

💬 关联讨论

加载中...