知识卡片
C布局属性只解决表示一部分
#[repr©]让Rust结构按C兼容规则排列字段,便于与相应C结构交互;字段类型、对齐与编译目标仍需一致。它不把内部String等任意Rust类型变成可直接跨语言布局,也不解决指针的寿命、所有权或线程安全。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 6. Beyond Standard Rust Accessing C Data from Rust · 查看原书依据
repr(C)] for Rust types used in FFI; this representation is designed for the purpose of allowing C interoperability: /* C data structure definition. */ /* Changes here must be reflected in lib.rs. */ typedef struct { uint8_t byte; uint32_t integer; } FfiStruct; // Equivalent Rust data structure. // Changes here must be reflected in lib.h / lib.c. #[repr(C)]
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第6章《Accessing C Data from Rust》(spine10;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch06.html;u178-ch06-011)。
- 结论依据:原文在“repr(C)”附近的定义、例子或代码支撑本卡核心说明:#[repr(C)]让Rust结构按C兼容规则排列字段,便于与相应C结构交互;字段类型、对齐与编译目标仍需一致。边界按已读前提解释;示例未执行。
- 原始内容:repr(C)] for Rust types used in FFI; this representation is designed for the purpose of allowing C interoperability: /* C data structure definition. */ /* Changes here must be reflected in lib.rs. */ typedef struct { uint8_t byte; uint32_t integer; } FfiStruct; // Equivalent Rust data structure. // Changes here must be reflected in lib.h / lib.c. #[repr(C)]