Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glib/GStringPtr: Add as_str() and Deref<Target=&str> #1181

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion glib/src/collections/strv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 26 additions & 9 deletions glib/src/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
cgwalters marked this conversation as resolved.
Show resolved Hide resolved
self
}

// rustdoc-stripper-ignore-next
/// Returns the string's C pointer.
#[inline]
Expand All @@ -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 {
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -863,7 +880,7 @@ impl Ord for GStringPtr {
impl PartialOrd<GStringPtr> for String {
#[inline]
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
Some(self.as_str().cmp(other.to_str()))
Some(self.as_str().cmp(other))
}
}

Expand All @@ -877,7 +894,7 @@ impl PartialOrd<GStringPtr> for GString {
impl PartialOrd<String> for GStringPtr {
#[inline]
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
Some(self.to_str().cmp(other))
Some(self.as_str().cmp(other))
}
}

Expand All @@ -891,7 +908,7 @@ impl PartialOrd<GString> for GStringPtr {
impl PartialOrd<GStringPtr> for str {
#[inline]
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
Some(self.cmp(other.to_str()))
Some(self.cmp(other.as_str()))
}
}

Expand All @@ -905,14 +922,14 @@ impl PartialOrd<GStringPtr> for GStr {
impl PartialOrd<str> for GStringPtr {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
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<std::cmp::Ordering> {
Some(self.to_str().cmp(other))
Some(self.as_str().cmp(other))
}
}

Expand All @@ -933,7 +950,7 @@ impl PartialOrd<&GStr> for GStringPtr {
impl PartialOrd<GStringPtr> for &str {
#[inline]
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other.to_str()))
Some(self.cmp(&other.as_str()))
}
}

Expand All @@ -954,7 +971,7 @@ impl AsRef<GStringPtr> for GStringPtr {
impl std::hash::Hash for GStringPtr {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.to_str().hash(state);
self.as_str().hash(state);
}
}

Expand Down
Loading