9.7 练习:C 库
06-练习:C 库 — Comprehensive Rust
2 分钟阅读
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/ffi/c-library-example.html
9.7 练习:C 库
| |
C 库常用
void*参数隐藏实现细节。考虑这个自然语言处理库的头文件,它隐藏了
TextAnalyst与Analysis类型。在 Rust 中可用类似下面的类型来模拟:
1 2 3 4 5 6// Copyright 2026 Google LLC // SPDX-License-Identifier: Apache-2.0 #[repr(C)] pub struct TextAnalyst { _private: [u8; 0], }练习:请学员为该库编写包装器。
建议解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64// Copyright 2026 Google LLC // SPDX-License-Identifier: Apache-2.0 // ffi.rs use std::ffi::c_char; use std::os::raw::c_void; #[repr(C)] pub struct TextAnalyst { _private: [u8; 0], } #[repr(C)] #[derive(Debug, Clone, Copy)] pub struct Token { pub start: *const c_char, pub length: usize, pub index: usize, } #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TAError { Ok = 0, NullPointer = 1, OutOfMemory = 2, Other = 3, } pub type Tokenizer = Option< unsafe extern "C" fn(token: *mut Token, extra_context: *mut c_void) -> bool, >; pub type TokenCallback = Option< unsafe extern "C" fn( user_context: *mut c_void, token: *mut Token, result: *mut c_void, ) -> bool, >; unsafe extern "C" { pub fn ta_new() -> *mut TextAnalyst; pub fn ta_free(ta: *mut TextAnalyst); pub fn ta_reset(ta: *mut TextAnalyst); pub fn ta_set_tokenizer(ta: *mut TextAnalyst, func: *const Tokenizer); pub fn ta_set_text( ta: *mut TextAnalyst, text: *const c_char, len: usize, make_copy: bool, ) -> TAError; pub fn ta_foreach_token( ta: *const TextAnalyst, callback: *const TokenCallback, user_context: *mut c_void, ) -> usize; pub fn ta_error_string(error: TAError) -> *const c_char; }