async_tensorrt/ffi/
error.rs

1use cpp::cpp;
2
3/// Get last error message produced by TensorRT.
4///
5/// # Thread-safety
6///
7/// This function could return an error produced by a function that executed on a different thread.
8pub fn get_last_error_message() -> String {
9    // SAFETY: This is safe because first, we copy the error out of `GLOBAL_LOGGER`, which is thread-safe,
10    // and then we take ownership of the string before destroying it on the C++ side.
11    let error_boxed_ptr = cpp!(unsafe [] -> *mut std::ffi::c_void as "void*" {
12        const std::string lastError = GLOBAL_LOGGER.getLastError();
13        const char* lastErrorCstr = lastError.c_str();
14        void* lastErrorPtr = rust!(Logger_takeLastError [
15            lastErrorCstr : *const std::os::raw::c_char as "const char*"
16        ] -> *mut std::ffi::c_void as "void*" {
17            let error_boxed = Box::new(
18                std::ffi::CStr::from_ptr(lastErrorCstr)
19                    .to_str()
20                    .unwrap_or_default()
21                    .to_owned()
22            );
23            Box::into_raw(error_boxed) as *mut std::ffi::c_void
24        });
25        return lastErrorPtr;
26    });
27    // SAFETY: This is safe because we boxed the error ourselves earlier and used `Box::into_raw`
28    // to get this pointer.
29    let error_boxed = unsafe { Box::from_raw(error_boxed_ptr as *mut String) };
30    let error = *error_boxed;
31    if !error.is_empty() {
32        error
33    } else {
34        "unknown error".to_string()
35    }
36}