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: Implement various traits on GStr manually #921

Merged
merged 1 commit into from Jan 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion glib/src/gstring.rs
Expand Up @@ -20,7 +20,6 @@ use crate::{prelude::*, translate::*, value::FromValue, Type, Value};
///
/// This type is very similar to [`std::ffi::CStr`], but with one added constraint: the string
/// must also be valid UTF-8.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct GStr(str);

Expand Down Expand Up @@ -333,6 +332,43 @@ macro_rules! gstr {
};
}

impl fmt::Debug for GStr {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<&str as fmt::Debug>::fmt(&self.as_str(), f)
}
}

impl PartialEq for GStr {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_str().eq(other.as_str())
}
}

impl Eq for GStr {}

impl PartialOrd for GStr {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}

impl Ord for GStr {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}

impl hash::Hash for GStr {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
}
}

impl Default for &GStr {
#[inline]
fn default() -> Self {
Expand Down