blob: 3d8a9f0800db1382a12f6baa54ab47a0f93c4b51 [file] [log] [blame]
use std::slice;
use std::str;
// Not necessarily ABI compatible with &str. Codegen performs the translation.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct RustStr {
ptr: *const u8,
len: usize,
}
impl RustStr {
pub fn from(s: &str) -> Self {
RustStr {
ptr: s.as_ptr(),
len: s.len(),
}
}
pub unsafe fn as_str<'a>(self) -> &'a str {
let slice = slice::from_raw_parts(self.ptr, self.len);
str::from_utf8_unchecked(slice)
}
}
#[export_name = "cxxbridge01$str$valid"]
unsafe extern "C" fn str_valid(ptr: *const u8, len: usize) -> bool {
let slice = slice::from_raw_parts(ptr, len);
str::from_utf8(slice).is_ok()
}