depthai_sys/string_utils.rs
1// String utilities for working with C++ std::string via cxx
2// With autocxx, we get native cxx::CxxString support
3
4use std::ffi::{CStr, CString};
5
6/// Helper function to convert a C string to a Rust String
7pub unsafe fn c_str_to_string(c_str: *const std::os::raw::c_char) -> String {
8 if c_str.is_null() {
9 return String::new();
10 }
11 unsafe { CStr::from_ptr(c_str).to_string_lossy().into_owned() }
12}
13
14/// Convert a Rust &str to a C-compatible string
15pub fn str_to_cstring(s: &str) -> Result<CString, std::ffi::NulError> {
16 CString::new(s)
17}
18
19// Note: With autocxx, we can directly use cxx::CxxString which provides:
20// - .to_string_lossy() to convert to Rust String
21// - .as_bytes() to get the underlying bytes
22// - Direct integration with C++ std::string
23//
24// This is much cleaner than the previous opaque type approach.