From dc856e5ccc048ce8b71013d874a9bb6fe1f45c3d 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/collections/strv.rs | 2 +- glib/src/gstring.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index c3de86c334e0..5a2ee41e7ea3 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -409,7 +409,7 @@ impl Clone for StrV { unsafe { let mut s = Self::with_capacity(self.len()); for (i, item) in self.iter().enumerate() { - *s.ptr.as_ptr().add(i) = GString::from(item.to_str()).into_glib_ptr(); + *s.ptr.as_ptr().add(i) = GString::from(item.as_str()).into_glib_ptr(); } s.len = self.len(); *s.ptr.as_ptr().add(s.len) = ptr::null_mut(); diff --git a/glib/src/gstring.rs b/glib/src/gstring.rs index edabfb2a20b0..99d2ae440405 100644 --- a/glib/src/gstring.rs +++ b/glib/src/gstring.rs @@ -694,10 +694,18 @@ 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] + #[deprecated = "Use as_str instead"] + pub fn to_str(&self) -> &str { + self + } + // rustdoc-stripper-ignore-next /// Returns the string's C pointer. #[inline] @@ -724,6 +732,15 @@ impl Clone for GStringPtr { } } +impl Deref for GStringPtr { + type Target = str; + + #[inline] + fn deref(&self) -> &str { + self.as_str() + } +} + impl IntoGlibPtr<*mut c_char> for GStringPtr { #[inline] unsafe fn into_glib_ptr(self) -> *mut c_char { @@ -749,7 +766,7 @@ impl fmt::Debug for GStringPtr { impl fmt::Display for GStringPtr { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.to_str()) + f.write_str(self.as_str()) } } @@ -863,7 +880,7 @@ impl Ord for GStringPtr { impl PartialOrd for String { #[inline] fn partial_cmp(&self, other: &GStringPtr) -> Option { - Some(self.as_str().cmp(other.to_str())) + Some(self.as_str().cmp(other)) } } @@ -877,7 +894,7 @@ impl PartialOrd for GString { impl PartialOrd for GStringPtr { #[inline] fn partial_cmp(&self, other: &String) -> Option { - Some(self.to_str().cmp(other)) + Some(self.as_str().cmp(other)) } } @@ -891,7 +908,7 @@ impl PartialOrd for GStringPtr { impl PartialOrd for str { #[inline] fn partial_cmp(&self, other: &GStringPtr) -> Option { - Some(self.cmp(other.to_str())) + Some(self.cmp(other.as_str())) } } @@ -905,14 +922,14 @@ impl PartialOrd for GStr { impl PartialOrd for GStringPtr { #[inline] fn partial_cmp(&self, other: &str) -> Option { - Some(self.to_str().cmp(other)) + Some(self.as_str().cmp(other)) } } impl PartialOrd<&str> for GStringPtr { #[inline] fn partial_cmp(&self, other: &&str) -> Option { - Some(self.to_str().cmp(other)) + Some(self.as_str().cmp(other)) } } @@ -933,7 +950,7 @@ impl PartialOrd<&GStr> for GStringPtr { impl PartialOrd for &str { #[inline] fn partial_cmp(&self, other: &GStringPtr) -> Option { - Some(self.cmp(&other.to_str())) + Some(self.cmp(&other.as_str())) } } @@ -954,7 +971,7 @@ impl AsRef for GStringPtr { impl std::hash::Hash for GStringPtr { #[inline] fn hash(&self, state: &mut H) { - self.to_str().hash(state); + self.as_str().hash(state); } }