From 9b24a35d66d1200b1f3b177f55cdc8f1e9d140b8 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 12 Sep 2023 11:25:47 -0400 Subject: [PATCH] glib/GStringPtr: Add `as_str()` and `Deref` I was updating some code to glib 0.18 and we were previously using `as_str()` - which is what the default Rust nomenclature for an *infallible* reference-to-reference conversion uses. The `to_str()` is used for fallible conversions (e.g. https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_str) which this is not. --- glib/src/gstring.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/glib/src/gstring.rs b/glib/src/gstring.rs index edabfb2a20b0..43ee14685f79 100644 --- a/glib/src/gstring.rs +++ b/glib/src/gstring.rs @@ -694,10 +694,17 @@ impl GStringPtr { // rustdoc-stripper-ignore-next /// Returns the corresponding [`&str`]. #[inline] - pub fn to_str(&self) -> &str { + pub fn as_str(&self) -> &str { self.to_gstr().as_str() } + // rustdoc-stripper-ignore-next + /// This is just an alias for [`as_str`]. + #[inline] + pub fn to_str(&self) -> &str { + self + } + // rustdoc-stripper-ignore-next /// Returns the string's C pointer. #[inline] @@ -724,6 +731,15 @@ impl Clone for GStringPtr { } } +impl Deref for GStringPtr { + type Target = str; + + #[inline] + fn deref(&self) -> &str { + self.to_str() + } +} + impl IntoGlibPtr<*mut c_char> for GStringPtr { #[inline] unsafe fn into_glib_ptr(self) -> *mut c_char {