Skip to content

Commit 84667c8

Browse files
Merge pull request #882 from sdroege/into-gstr
glib: Use `IntoGStr` trait in a couple of places
2 parents 5091b7d + 899b40b commit 84667c8

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

glib/src/object.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
thread_guard::thread_id,
1414
translate::*,
1515
value::FromValue,
16-
Closure, PtrSlice, RustClosure, SignalHandlerId, Type, Value,
16+
Closure, IntoGStr, PtrSlice, RustClosure, SignalHandlerId, Type, Value,
1717
};
1818

1919
// rustdoc-stripper-ignore-next
@@ -2482,10 +2482,12 @@ impl<T: ObjectType> ObjectExt for T {
24822482

24832483
fn stop_signal_emission_by_name(&self, signal_name: &str) {
24842484
unsafe {
2485-
gobject_ffi::g_signal_stop_emission_by_name(
2486-
self.as_object_ref().to_glib_none().0,
2487-
signal_name.to_glib_none().0,
2488-
);
2485+
signal_name.run_with_gstr(|signal_name| {
2486+
gobject_ffi::g_signal_stop_emission_by_name(
2487+
self.as_object_ref().to_glib_none().0,
2488+
signal_name.as_ptr(),
2489+
)
2490+
});
24892491
}
24902492
}
24912493

@@ -3012,10 +3014,12 @@ impl<T: ObjectType> ObjectExt for T {
30123014
#[inline]
30133015
fn notify(&self, property_name: &str) {
30143016
unsafe {
3015-
gobject_ffi::g_object_notify(
3016-
self.as_object_ref().to_glib_none().0,
3017-
property_name.to_glib_none().0,
3018-
);
3017+
property_name.run_with_gstr(|property_name| {
3018+
gobject_ffi::g_object_notify(
3019+
self.as_object_ref().to_glib_none().0,
3020+
property_name.as_ptr(),
3021+
)
3022+
});
30193023
}
30203024
}
30213025

@@ -3257,10 +3261,12 @@ impl ObjectClass {
32573261
unsafe {
32583262
let klass = self as *const _ as *const gobject_ffi::GObjectClass;
32593263

3260-
from_glib_none(gobject_ffi::g_object_class_find_property(
3261-
klass as *mut _,
3262-
property_name.to_glib_none().0,
3263-
))
3264+
property_name.run_with_gstr(|property_name| {
3265+
from_glib_none(gobject_ffi::g_object_class_find_property(
3266+
klass as *mut _,
3267+
property_name.as_ptr(),
3268+
))
3269+
})
32643270
}
32653271
}
32663272

glib/src/quark.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::{ffi::CStr, fmt, num::NonZeroU32};
3+
use std::{fmt, num::NonZeroU32};
44

5-
use crate::translate::*;
5+
use crate::{translate::*, GStr, IntoGStr};
66

77
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
88
#[repr(transparent)]
@@ -12,18 +12,14 @@ pub struct Quark(NonZeroU32);
1212
impl Quark {
1313
#[doc(alias = "g_quark_from_string")]
1414
#[allow(clippy::should_implement_trait)]
15-
pub fn from_str(s: &str) -> Quark {
16-
unsafe { from_glib(ffi::g_quark_from_string(s.to_glib_none().0)) }
15+
pub fn from_str(s: impl IntoGStr) -> Quark {
16+
unsafe { s.run_with_gstr(|s| from_glib(ffi::g_quark_from_string(s.as_ptr()))) }
1717
}
1818

1919
#[allow(clippy::trivially_copy_pass_by_ref)]
2020
#[doc(alias = "g_quark_to_string")]
21-
pub fn as_str<'a>(&self) -> &'a str {
22-
unsafe {
23-
CStr::from_ptr(ffi::g_quark_to_string(self.into_glib()))
24-
.to_str()
25-
.unwrap()
26-
}
21+
pub fn as_str<'a>(&self) -> &'a GStr {
22+
unsafe { GStr::from_ptr(ffi::g_quark_to_string(self.into_glib())) }
2723
}
2824

2925
#[doc(alias = "g_quark_try_string")]
@@ -38,8 +34,8 @@ impl fmt::Debug for Quark {
3834
}
3935
}
4036

41-
impl<'a> From<&'a str> for Quark {
42-
fn from(s: &'a str) -> Self {
37+
impl<T: IntoGStr> From<T> for Quark {
38+
fn from(s: T) -> Self {
4339
Self::from_str(s)
4440
}
4541
}

glib/src/subclass/signal.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use std::{fmt, num::NonZeroU32, ptr, sync::Mutex};
44

55
use crate::{
6-
prelude::*, translate::*, utils::is_canonical_pspec_name, Closure, SignalFlags, Type, Value,
6+
prelude::*, translate::*, utils::is_canonical_pspec_name, Closure, IntoGStr, SignalFlags, Type,
7+
Value,
78
};
89

910
// rustdoc-stripper-ignore-next
@@ -192,13 +193,15 @@ impl SignalId {
192193
let mut signal_id = std::mem::MaybeUninit::uninit();
193194
let mut detail_quark = std::mem::MaybeUninit::uninit();
194195
unsafe {
195-
let found: bool = from_glib(gobject_ffi::g_signal_parse_name(
196-
name.to_glib_none().0,
197-
type_.into_glib(),
198-
signal_id.as_mut_ptr(),
199-
detail_quark.as_mut_ptr(),
200-
force_detail.into_glib(),
201-
));
196+
let found: bool = name.run_with_gstr(|name| {
197+
from_glib(gobject_ffi::g_signal_parse_name(
198+
name.as_ptr(),
199+
type_.into_glib(),
200+
signal_id.as_mut_ptr(),
201+
detail_quark.as_mut_ptr(),
202+
force_detail.into_glib(),
203+
))
204+
});
202205

203206
if found {
204207
Some((
@@ -217,7 +220,9 @@ impl SignalId {
217220
#[inline]
218221
pub fn lookup(name: &str, type_: Type) -> Option<Self> {
219222
unsafe {
220-
let signal_id = gobject_ffi::g_signal_lookup(name.to_glib_none().0, type_.into_glib());
223+
let signal_id = name.run_with_gstr(|name| {
224+
gobject_ffi::g_signal_lookup(name.as_ptr(), type_.into_glib())
225+
});
221226
if signal_id == 0 {
222227
None
223228
} else {

glib/src/types.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{fmt, marker::PhantomData, mem, ptr};
77

8-
use crate::{translate::*, Slice};
8+
use crate::{translate::*, IntoGStr, Slice};
99

1010
// rustdoc-stripper-ignore-next
1111
/// A GLib or GLib-based library type
@@ -206,9 +206,12 @@ impl Type {
206206
}
207207

208208
#[doc(alias = "g_type_from_name")]
209-
pub fn from_name(name: &str) -> Option<Self> {
209+
pub fn from_name(name: impl IntoGStr) -> Option<Self> {
210210
unsafe {
211-
let type_: Self = from_glib(gobject_ffi::g_type_from_name(name.to_glib_none().0));
211+
let type_ = name.run_with_gstr(|name| {
212+
Self::from_glib(gobject_ffi::g_type_from_name(name.as_ptr()))
213+
});
214+
212215
Some(type_).filter(|t| t.is_valid())
213216
}
214217
}

0 commit comments

Comments
 (0)