Xử lý lỗi trong CXX
CXX’s support for Result<T,E> relies on C++ exceptions, so we can’t use that in Chromium. Alternatives:
-
The
Tpart ofResult<T, E>can be:- Returned via out parameters (e.g. via
&mut T). This requires thatTcan be passed across the FFI boundary - for exampleThas to be:- A primitive type (like
u32orusize) - A type natively supported by
cxx(likeUniquePtr<T>) that has a suitable default value to use in a failure case (unlikeBox<T>).
- A primitive type (like
- Retained on the Rust side, and exposed via reference. This may be needed when
Tis a Rust type, which cannot be passed across the FFI boundary, and cannot be stored inUniquePtr<T>.
- Returned via out parameters (e.g. via
-
The
Epart ofResult<T, E>can be:- Returned as a boolean (e.g.
truerepresenting success, andfalserepresenting failure) - Preserving error details is in theory possible, but so far hasn’t been needed in practice.
- Returned as a boolean (e.g.