minimize-rust-ffi-crate-surface

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Minimize Rust FFI Crate Surface

最小化Rust FFI Crate的对外暴露面

Remove C symbols defined in a Rust FFI crate or file that are either unused or only used in C/C++ unit tests.
移除Rust FFI crate或文件中定义的、未使用或仅在C/C++单元测试中使用的C符号。

Arguments

参数

  • <path>
    : Path to the Rust crate or file.
  • <path 1> <path 2>
    : Multiple Rust crates/files.
If the path doesn't start with
src/
, assume it to be in the
src/redisearch_rs/c_entrypoint
directory. E.g.
numeric_range_tree_ffi
becomes
src/redisearch_rs/numeric_range_tree_ffi
. If the path points to a directory, review the documentation of all Rust files in that directory.
  • <path>
    :Rust crate或文件的路径。
  • <path 1> <path 2>
    :多个Rust crate/文件。
如果路径不是以
src/
开头,则默认其位于
src/redisearch_rs/c_entrypoint
目录下。例如,
numeric_range_tree_ffi
会被解析为
src/redisearch_rs/numeric_range_tree_ffi
。 如果路径指向一个目录,则检查该目录下所有Rust文件的文档。

Instructions

操作步骤

  • Use analyze-rust-ffi-crate-surface to enumerate and analyze the usage of all the FFI symbols exposed by the Rust crate or file (e.g.
    extern "C" fn
    annotated with
    #[unsafe(no_mangle)]
    or type definitions).
  • For each unused symbol:
    • Delete its Rust definition.
    • Run C/C++ unit tests to ensure the symbol was indeed unused (via
      ./build.sh RUN_UNIT_TESTS
      )
  • For each symbol that is only used in C/C++ unit tests, elaborate a plan to either:
    • Refactor the C/C++ unit tests not to use it.
    • Remove the C/C++ unit tests (or assertions) that rely on it, since they are prying into the implementation details of the Rust crate.
    • Keep the symbol, but mark it as "test only" in the Rust documentation.
  • 使用analyze-rust-ffi-crate-surface来枚举并分析Rust crate或文件暴露的所有FFI符号的使用情况(例如带有
    #[unsafe(no_mangle)]
    注解的
    extern "C" fn
    或类型定义)。
  • 对于每个未使用的符号:
    • 删除其Rust定义。
    • 运行C/C++单元测试以确保该符号确实未被使用(通过
      ./build.sh RUN_UNIT_TESTS
  • 对于每个仅在C/C++单元测试中使用的符号,制定一个方案,选择以下其中一种处理方式:
    • 重构C/C++单元测试,使其不再使用该符号。
    • 移除依赖该符号的C/C++单元测试(或断言),因为这些测试窥探了Rust crate的实现细节。
    • 保留该符号,但在Rust文档中将其标记为“仅用于测试”。