Project: ClickHouse (App Definition and Development / Database)
File: integrating_rust_libraries.md

slug: /en/development/integratingrustlibraries Rust library integration will be described based on BLAKE3 hash-function integration. The first step of integration is to add the library to /rust folder. To do this, you need to create an empty Rust project and include the required library in Cargo.toml. It is also necessary to configure new library compilation as static by adding `crate-type = ["staticlib"]` to Cargo.toml. Next, you need to link the library to CMake using Corrosion library. The first step is to add the library folder in the CMakeLists.txt inside the /rust folder. After that, you should add the CMakeLists.txt file to the library directory. In it, you need to call the Corrosion import function. These lines were used to import BLAKE3: ``` corrosionimportcrate(MANIFESTPATH Cargo.toml NOSTD) targetincludedirectories(chrust_blake3 INTERFACE include) addlibrary(chrust::blake3 ALIAS chrust_blake3) ``` Thus, we will create a correct CMake target using Corrosion, and then rename it with a more convenient name. Note that the name `chrustblake3` comes from Cargo.toml, where it is used as project name (`name = "chrustblake3"`). Since Rust data types are not compatible with C/C++ data types, we will use our empty library project to create shim methods for conversion of data received from C/C++, calling library methods, and inverse conversion for output data. For example, this method was written for BLAKE3: ``` pub unsafe extern "C" fn blake3applyshim( begin: *const c_char, _size: u32, outchardata: *mut u8, ) -> *mut c_char { if begin.is_null() { let err_str = CString::new("input was a null pointer").unwrap(); return errstr.intoraw(); } let mut hasher = blake3::Hasher::new(); let inputbytes = CStr::fromptr(begin); let inputres = inputbytes.to_bytes(); hasher.update(input_res); let mut reader = hasher.finalize_xof(); reader.fill(std::slice::fromrawpartsmut(outchardata, blake3::OUTLEN)); std::ptr::null_mut() } ``` This method gets C-compatible string, its size and output string pointer as input. Then, it converts C-compatible inputs into types that are used by actual library methods and calls them. After that, it should convert library methods' outputs back into C-compatible

In that particular case library supported direct writing into pointer by method fill(), so the conversion was not needed. The main advice here is to create less methods, so you will need to do less conversions on each method call and won't create much overhead. It is worth noting that the `#[no_mangle]` attribute and `extern "C"` are mandatory for all such methods. Without them, it will not be possible to perform a correct C/C++-compatible compilation. Moreover, they are necessary for the next step of the integration. After writing the code for the shim methods, we need to prepare the header file for the library. This can be done manually, or you can use the cbindgen library for auto-generation. In case of using cbindgen, you will need to write a build.rs build script and include cbindgen as a build-dependency. An example of a build script that can auto-generate a header file: ``` let cratedir = env::var("CARGOMANIFEST_DIR").unwrap(); let packagename = env::var("CARGOPKG_NAME").unwrap(); let outputfile = ("include/".toowned() + &format!("{}.h", packagename)).tostring(); match cbindgen::generate(&crate_dir) { Ok(header) => { header.writetofile(&output_file); } Err(err) => { panic!("{}", err) } } ``` Also, you should use attribute #[no_mangle] and `extern "C"` for every C-compatible attribute. Without it library can compile incorrectly and cbindgen won't launch header autogeneration. After all these steps you can test your library in a small project to find all problems with compatibility or header generation. If any problems occur during header generation, you can try to configure it with cbindgen.toml file (you can find a template here: ). It is worth noting the problem that occurred when integrating BLAKE3: MemorySanitizer can cause false-positive reports as it's unable to see if some variables in Rust are initialized or not. It was solved with writing a method with more explicit definition for some variables, although this implementation of method is slower and is used only to fix MemorySanitizer builds.