Add trait to convert FFI reference to &wstr

You can now use a reference to CxxWString or an allocated UniquePtr<CxxWString>
to get an &wstr temporary to use without having to allocate again (e.g. via
`from_ffi()`).
This commit is contained in:
Mahmoud Al-Qudsi
2023-03-10 12:40:11 -06:00
parent 1a7e3024cc
commit c8d2f7a0da

View File

@@ -153,3 +153,20 @@ fn from_ffi(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
/// Convert from FFI types to a reference to a wide string (i.e. a [`wstr`]) without allocating.
pub trait AsWstr<'a> {
fn as_wstr(&'a self) -> &'a wstr;
}
impl<'a> AsWstr<'a> for cxx::UniquePtr<cxx::CxxWString> {
fn as_wstr(&'a self) -> &'a wstr {
wstr::from_char_slice(self.as_chars())
}
}
impl<'a> AsWstr<'a> for cxx::CxxWString {
fn as_wstr(&'a self) -> &'a wstr {
wstr::from_char_slice(self.as_chars())
}
}