Skip to content

Commit 9b61929

Browse files
authored
Merge pull request #1181 from cgwalters/gstring-as-str
glib/GStringPtr: Add `as_str()` and `Deref<Target=&str>`
2 parents 153420b + dc856e5 commit 9b61929

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

glib/src/collections/strv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl Clone for StrV {
409409
unsafe {
410410
let mut s = Self::with_capacity(self.len());
411411
for (i, item) in self.iter().enumerate() {
412-
*s.ptr.as_ptr().add(i) = GString::from(item.to_str()).into_glib_ptr();
412+
*s.ptr.as_ptr().add(i) = GString::from(item.as_str()).into_glib_ptr();
413413
}
414414
s.len = self.len();
415415
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();

glib/src/gstring.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,18 @@ impl GStringPtr {
694694
// rustdoc-stripper-ignore-next
695695
/// Returns the corresponding [`&str`].
696696
#[inline]
697-
pub fn to_str(&self) -> &str {
697+
pub fn as_str(&self) -> &str {
698698
self.to_gstr().as_str()
699699
}
700700

701+
// rustdoc-stripper-ignore-next
702+
/// This is just an alias for [`as_str`].
703+
#[inline]
704+
#[deprecated = "Use as_str instead"]
705+
pub fn to_str(&self) -> &str {
706+
self
707+
}
708+
701709
// rustdoc-stripper-ignore-next
702710
/// Returns the string's C pointer.
703711
#[inline]
@@ -724,6 +732,15 @@ impl Clone for GStringPtr {
724732
}
725733
}
726734

735+
impl Deref for GStringPtr {
736+
type Target = str;
737+
738+
#[inline]
739+
fn deref(&self) -> &str {
740+
self.as_str()
741+
}
742+
}
743+
727744
impl IntoGlibPtr<*mut c_char> for GStringPtr {
728745
#[inline]
729746
unsafe fn into_glib_ptr(self) -> *mut c_char {
@@ -749,7 +766,7 @@ impl fmt::Debug for GStringPtr {
749766
impl fmt::Display for GStringPtr {
750767
#[inline]
751768
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
752-
f.write_str(self.to_str())
769+
f.write_str(self.as_str())
753770
}
754771
}
755772

@@ -863,7 +880,7 @@ impl Ord for GStringPtr {
863880
impl PartialOrd<GStringPtr> for String {
864881
#[inline]
865882
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
866-
Some(self.as_str().cmp(other.to_str()))
883+
Some(self.as_str().cmp(other))
867884
}
868885
}
869886

@@ -877,7 +894,7 @@ impl PartialOrd<GStringPtr> for GString {
877894
impl PartialOrd<String> for GStringPtr {
878895
#[inline]
879896
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
880-
Some(self.to_str().cmp(other))
897+
Some(self.as_str().cmp(other))
881898
}
882899
}
883900

@@ -891,7 +908,7 @@ impl PartialOrd<GString> for GStringPtr {
891908
impl PartialOrd<GStringPtr> for str {
892909
#[inline]
893910
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
894-
Some(self.cmp(other.to_str()))
911+
Some(self.cmp(other.as_str()))
895912
}
896913
}
897914

@@ -905,14 +922,14 @@ impl PartialOrd<GStringPtr> for GStr {
905922
impl PartialOrd<str> for GStringPtr {
906923
#[inline]
907924
fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
908-
Some(self.to_str().cmp(other))
925+
Some(self.as_str().cmp(other))
909926
}
910927
}
911928

912929
impl PartialOrd<&str> for GStringPtr {
913930
#[inline]
914931
fn partial_cmp(&self, other: &&str) -> Option<std::cmp::Ordering> {
915-
Some(self.to_str().cmp(other))
932+
Some(self.as_str().cmp(other))
916933
}
917934
}
918935

@@ -933,7 +950,7 @@ impl PartialOrd<&GStr> for GStringPtr {
933950
impl PartialOrd<GStringPtr> for &str {
934951
#[inline]
935952
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
936-
Some(self.cmp(&other.to_str()))
953+
Some(self.cmp(&other.as_str()))
937954
}
938955
}
939956

@@ -954,7 +971,7 @@ impl AsRef<GStringPtr> for GStringPtr {
954971
impl std::hash::Hash for GStringPtr {
955972
#[inline]
956973
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
957-
self.to_str().hash(state);
974+
self.as_str().hash(state);
958975
}
959976
}
960977

0 commit comments

Comments
 (0)