CXX Error Handling: QR Example
QR ์ฝ๋ ์์ฑ๊ธฐ์์์ ๊ฐ์ด ๊ฐ๋จํ ๋ถ์ธ๋ก ์ฑ๊ณต์ ๋ํ๋ผ ์ ์๋ ๊ฒฝ์ฐ: ์ฑ๊ณต์ ๋ํ๋ด๋ ๋ถ์ธ์ ๋ฐํํ๊ณ out ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋กํฉ๋๋ค.
#[cxx::bridge(namespace = "qr_code_generator")]
mod ffi {
extern "Rust" {
fn generate_qr_code_using_rust(
data: &[u8],
min_version: i16,
out_pixels: Pin<&mut CxxVector<u8>>,
out_qr_size: &mut usize,
) -> bool;
}
}
Students may be curious about the semantics of the out_qr_size
output. This is not the size of the vector, but the size of the QR code (and admittedly it is a bit redundant - this is the square root of the size of the vector).
It may be worth pointing out the importance of initializing out_qr_size
before calling into the Rust function. Creation of a Rust reference that points to uninitialized memory results in Undefined Behavior (unlike in C++, when only the act of dereferencing such memory results in UB).
If students ask about Pin
, then explain why CXX needs it for mutable references to C++ data: the answer is that C++ data canโt be moved around like Rust data, because it may contain self-referential pointers.