llama_cpp_bindings_sys/cpp_text_view.rs
1use std::ffi::c_char;
2use std::slice;
3use std::str;
4use std::str::Utf8Error;
5
6/// Borrows the UTF-8 text of a C++ string llama.cpp exposes as a pointer and a length.
7///
8/// # Safety
9///
10/// `text` must point to `length` readable bytes that stay alive and unchanged for `'text`.
11///
12/// # Errors
13///
14/// Returns [`Utf8Error`] when the bytes are not UTF-8.
15pub const unsafe fn cpp_text_view<'text>(
16 text: *const c_char,
17 length: usize,
18) -> Result<&'text str, Utf8Error> {
19 str::from_utf8(unsafe { slice::from_raw_parts(text.cast::<u8>(), length) })
20}