From c8d2f7a0da7add668733727aeba57acf0d866fde Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Fri, 10 Mar 2023 12:40:11 -0600 Subject: [PATCH] Add trait to convert FFI reference to &wstr You can now use a reference to CxxWString or an allocated UniquePtr to get an &wstr temporary to use without having to allocate again (e.g. via `from_ffi()`). --- fish-rust/src/wchar_ffi.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/fish-rust/src/wchar_ffi.rs b/fish-rust/src/wchar_ffi.rs index dab6c31f3..92c7f7137 100644 --- a/fish-rust/src/wchar_ffi.rs +++ b/fish-rust/src/wchar_ffi.rs @@ -153,3 +153,20 @@ fn from_ffi(&self) -> Vec { 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 { + 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()) + } +}