1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::QString;
use cpp_core::{CppBox, Ptr};
use std::os::raw::{c_char, c_int};

/// Allows to convert Qt strings to `std` strings
impl<'a> From<&'a QString> for String {
    fn from(s: &'a QString) -> String {
        s.to_std_string()
    }
}

impl QString {
    /// Creates Qt string from an `std` string.
    pub fn from_std_str<S: AsRef<str>>(s: S) -> CppBox<QString> {
        let slice = s.as_ref().as_bytes();
        unsafe {
            QString::from_utf8_char_int(
                Ptr::from_raw(slice.as_ptr() as *const c_char),
                slice.len() as c_int,
            )
        }
    }

    /// Creates `std` string from a Qt string.
    pub fn to_std_string(&self) -> String {
        unsafe {
            let buf = self.to_utf8();
            let bytes = std::slice::from_raw_parts(
                buf.const_data().as_raw_ptr() as *const u8,
                buf.size() as usize,
            );
            std::str::from_utf8_unchecked(bytes).to_string()
        }
    }
}

/// Creates a `QString` from a Rust string.
///
/// This is the same as `QString::from_std_str(str)`.
pub fn qs<S: AsRef<str>>(str: S) -> CppBox<QString> {
    QString::from_std_str(str)
}