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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use widestring::{U32CStr, U32CString};
#[derive(Debug)]
#[repr(transparent)]
pub struct SfStr(U32CStr);
impl SfStr {
pub(crate) unsafe fn from_ptr_str<'a>(p: *const u32) -> &'a Self {
let ptr: *const U32CStr = U32CStr::from_ptr_str(p);
&*(ptr as *const Self)
}
#[must_use]
pub fn to_rust_string(&self) -> String {
self.0.to_string().unwrap()
}
pub(crate) fn as_ptr(&self) -> *const u32 {
self.0.as_ptr()
}
}
pub trait SfStrConv {
#[doc(hidden)]
fn with_as_sfstr<F, R>(self, fun: F) -> R
where
F: FnOnce(&SfStr) -> R;
}
impl<'a> SfStrConv for &'a SfStr {
fn with_as_sfstr<F, R>(self, fun: F) -> R
where
F: FnOnce(&SfStr) -> R,
{
fun(self)
}
}
impl<'a> SfStrConv for &'a str {
fn with_as_sfstr<F, R>(self, fun: F) -> R
where
F: FnOnce(&SfStr) -> R,
{
let u32cstring = U32CString::from_str(self).unwrap();
#[allow(trivial_casts)]
let sfstr: &SfStr = unsafe { &*(&*u32cstring as *const _ as *const _) };
fun(sfstr)
}
}
impl<'a> SfStrConv for &'a String {
fn with_as_sfstr<F, R>(self, fun: F) -> R
where
F: FnOnce(&SfStr) -> R,
{
let str: &str = self;
str.with_as_sfstr(fun)
}
}