mono_rt/types/string.rs
1use std::ffi::{CStr, CString};
2
3use super::{MonoDomain, mono_handle};
4use crate::{MonoError, Result, api};
5
6mono_handle!(MonoString);
7
8impl MonoString {
9 /// Creates a new Mono string from a Rust string slice in the given domain.
10 ///
11 /// # Errors
12 ///
13 /// Returns [`MonoError::NullByteInName`] if `text` contains an interior null byte.
14 /// Returns [`MonoError::Uninitialized`] if the Mono API has not been initialized.
15 pub fn new(domain: MonoDomain, text: &str) -> Result<Option<Self>> {
16 let c_text = CString::new(text).map_err(|_| MonoError::NullByteInName)?;
17 let ptr = api()?.string_new(domain.as_ptr(), c_text.as_ptr());
18 Ok(Self::from_ptr(ptr))
19 }
20
21 /// Converts this Mono string to a Rust `String`, replacing invalid UTF-8 sequences.
22 ///
23 /// Returns an empty string if the underlying pointer is null after conversion.
24 ///
25 /// # Errors
26 ///
27 /// Returns [`MonoError::Uninitialized`] if the Mono API has not been initialized.
28 pub fn to_string_lossy(self) -> Result<String> {
29 let raw = api()?.string_to_utf8(self.as_ptr().cast());
30 if raw.is_null() {
31 return Ok(String::new());
32 }
33
34 let result = unsafe { CStr::from_ptr(raw) }
35 .to_string_lossy()
36 .into_owned();
37
38 api()?.free(raw.cast());
39
40 Ok(result)
41 }
42}