Skip to content

Commit bf90afe

Browse files
committed
glib: Implement various traits on GStr manually
Deriving the impls would consider the trailing NUL-terminator but this is inconsistent with the other trait impls that don't consider it. Also the `Debug` impl is now just printing the string.
1 parent c38b053 commit bf90afe

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

glib/src/gstring.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{prelude::*, translate::*, value::FromValue, Type, Value};
2020
///
2121
/// This type is very similar to [`std::ffi::CStr`], but with one added constraint: the string
2222
/// must also be valid UTF-8.
23-
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2423
#[repr(transparent)]
2524
pub struct GStr(str);
2625

@@ -333,6 +332,43 @@ macro_rules! gstr {
333332
};
334333
}
335334

335+
impl fmt::Debug for GStr {
336+
#[inline]
337+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338+
<&str as fmt::Debug>::fmt(&self.as_str(), f)
339+
}
340+
}
341+
342+
impl PartialEq for GStr {
343+
#[inline]
344+
fn eq(&self, other: &Self) -> bool {
345+
self.as_str().eq(other.as_str())
346+
}
347+
}
348+
349+
impl Eq for GStr {}
350+
351+
impl PartialOrd for GStr {
352+
#[inline]
353+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
354+
self.as_str().partial_cmp(other.as_str())
355+
}
356+
}
357+
358+
impl Ord for GStr {
359+
#[inline]
360+
fn cmp(&self, other: &Self) -> Ordering {
361+
self.as_str().cmp(other.as_str())
362+
}
363+
}
364+
365+
impl hash::Hash for GStr {
366+
#[inline]
367+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
368+
self.as_str().hash(state)
369+
}
370+
}
371+
336372
impl Default for &GStr {
337373
#[inline]
338374
fn default() -> Self {

0 commit comments

Comments
 (0)