FFI中的错误处理
说明
在像C语言这种,错误是用返回码表示的。然而,Rust的类型系统支持通过一个完整的类型来提供更加丰富的错误信息。
下面的实践展示了错误代码的不同类型,以及如何在使用层面上去暴露它们:
- 扁平的枚举(译者注:无实际的成员数据)转换成整型并且作为错误码返回。
- 结构体枚举应该被转换为一个整型错误码和一个包含详细错误信息的字符串。
- 自定义错误类型应该被转换为C语言标准下的表示类型。
代码示例
扁平枚举
enum DatabaseError {IsReadOnly = 1, // user attempted a write operationIOError = 2, // user should read the C errno() for what it wasFileCorrupted = 3, // user should run a repair tool to recover it}impl From<DatabaseError> for libc::c_int {fn from(e: DatabaseError) -> libc::c_int {(e as i8).into()}}
结构体枚举
pub mod errors {enum DatabaseError {IsReadOnly,IOError(std::io::Error),FileCorrupted(String), // message describing the issue}impl From<DatabaseError> for libc::c_int {fn from(e: DatabaseError) -> libc::c_int {match e {DatabaseError::IsReadOnly => 1,DatabaseError::IOError(_) => 2,DatabaseError::FileCorrupted(_) => 3,}}}}pub mod c_api {use super::errors::DatabaseError;#[no_mangle]pub extern "C" fn db_error_description(e: *const DatabaseError) -> *mut libc::c_char {let error: &DatabaseError = unsafe {// SAFETY: pointer lifetime is greater than the current stack frame&*e};let error_str: String = match error {DatabaseError::IsReadOnly => {format!("cannot write to read-only database");}DatabaseError::IOError(e) => {format!("I/O Error: {}", e);}DatabaseError::FileCorrupted(s) => {format!("File corrupted, run repair: {}", &s);}};let c_error = unsafe {// SAFETY: copying error_str to an allocated buffer with a NUL// character at the endlet mut malloc: *mut u8 = libc::malloc(error_str.len() + 1) as *mut _;if malloc.is_null() {return std::ptr::null_mut();}let src = error_str.as_bytes().as_ptr();std::ptr::copy_nonoverlapping(src, malloc, error_str.len());std::ptr::write(malloc.add(error_str.len()), 0);malloc as *mut libc::c_char};c_error}}
自定义错误类型
struct ParseError {expected: char,line: u32,ch: u16}impl ParseError { /* ... */ }/* Create a second version which is exposed as a C structure */#[repr(C)]pub struct parse_error {pub expected: libc::c_char,pub line: u32,pub ch: u16}impl From<ParseError> for parse_error {fn from(e: ParseError) -> parse_error {let ParseError { expected, line, ch } = e;parse_error { expected, line, ch }}}
优点
这样能确保其他语言能够正确访问错误信息,并且不用为此改动Rust代码的API。(译者注:相当于在错误处理时再封装一层,返回最简单的整型和字符串作为错误信息表示)
缺点
这样多写了很多代码,并且有些类型不能很容易地转换成C语言的标准。
