Skip to content

Commit f8df5df

Browse files
committed
glib: use Into<Value> instead of ToValue when possible
1 parent eb5d0bc commit f8df5df

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

glib-macros/src/closure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ impl ToTokens for Closure {
255255
);
256256
#(#inner_before)*
257257
#(#arg_values)*
258-
#crate_ident::closure::ToClosureReturnValue::to_closure_return_value(
259-
&(#closure)(#(#arg_names),*)
258+
#crate_ident::closure::IntoClosureReturnValue::into_closure_return_value(
259+
(#closure)(#(#arg_names),*)
260260
)
261261
})
262262
};

glib/src/closure.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,20 @@ impl Closure {
340340
}
341341
}
342342

343-
pub trait ToClosureReturnValue {
344-
fn to_closure_return_value(&self) -> Option<Value>;
343+
pub trait IntoClosureReturnValue {
344+
fn into_closure_return_value(self) -> Option<Value>;
345345
}
346346

347-
impl ToClosureReturnValue for () {
348-
fn to_closure_return_value(&self) -> Option<Value> {
347+
impl IntoClosureReturnValue for () {
348+
fn into_closure_return_value(self) -> Option<Value> {
349349
None
350350
}
351351
}
352352

353-
impl<T: ToValue> ToClosureReturnValue for T {
354-
fn to_closure_return_value(&self) -> Option<Value> {
355-
Some(self.to_value())
353+
impl<T: Into<Value>> IntoClosureReturnValue for T {
354+
#[inline]
355+
fn into_closure_return_value(self) -> Option<Value> {
356+
Some(self.into())
356357
}
357358
}
358359

glib/src/gobject/signal_group.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ mod tests {
187187
group.connect_closure(
188188
"sig-with-ret",
189189
false,
190-
glib::closure_local!(@watch obj => move |o: &SignalObject| -> String {
190+
glib::closure_local!(@watch obj => move |o: &SignalObject| -> &'static crate::GStr {
191191
assert_eq!(o, obj);
192-
format!("Hello")
192+
crate::gstr!("Hello")
193193
}),
194194
);
195195
group.set_target(Some(&obj));
196196
obj.emit_by_name::<()>("sig-with-args", &[&5u32, &"World"]);
197197
assert_eq!(*store.borrow(), "a 5 b World");
198-
let ret = obj.emit_by_name::<String>("sig-with-ret", &[]);
198+
let ret = obj.emit_by_name::<crate::GString>("sig-with-ret", &[]);
199199
assert_eq!(ret, "Hello");
200200
group.set_target(Object::NONE);
201201
let ret = obj.emit_by_name::<Option<String>>("sig-with-ret", &[]);

glib/src/object.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,13 +1608,13 @@ impl<'a, O: IsA<Object> + IsClass> ObjectBuilder<'a, O> {
16081608

16091609
// rustdoc-stripper-ignore-next
16101610
/// Set property `name` to the given value `value`.
1611-
pub fn property<T: ToValue + 'a>(self, name: &'a str, value: T) -> Self {
1611+
pub fn property(self, name: &'a str, value: impl Into<Value>) -> Self {
16121612
let ObjectBuilder {
16131613
type_,
16141614
mut properties,
16151615
..
16161616
} = self;
1617-
properties.push((name, value.to_value()));
1617+
properties.push((name, value.into()));
16181618

16191619
ObjectBuilder {
16201620
type_,
@@ -1692,7 +1692,7 @@ pub trait ObjectExt: ObjectType {
16921692
/// If the property does not exist, if the type of the property is different than
16931693
/// the provided value, or if the property is not writable.
16941694
#[doc(alias = "g_object_set_property")]
1695-
fn set_property<V: ToValue>(&self, property_name: &str, value: V);
1695+
fn set_property(&self, property_name: &str, value: impl Into<Value>);
16961696

16971697
// rustdoc-stripper-ignore-next
16981698
/// Sets the property `property_name` of the object to value `value`.
@@ -2298,15 +2298,15 @@ impl<T: ObjectType> ObjectExt for T {
22982298
}
22992299

23002300
#[track_caller]
2301-
fn set_property<V: ToValue>(&self, property_name: &str, value: V) {
2301+
fn set_property(&self, property_name: &str, value: impl Into<Value>) {
23022302
let pspec = self.find_property(property_name).unwrap_or_else(|| {
23032303
panic!(
23042304
"property '{property_name}' of type '{}' not found",
23052305
self.type_()
23062306
)
23072307
});
23082308

2309-
let mut property_value = value.to_value();
2309+
let mut property_value = value.into();
23102310
validate_property_type(self.type_(), false, &pspec, &mut property_value);
23112311
unsafe {
23122312
gobject_ffi::g_object_set_property(
@@ -3642,7 +3642,7 @@ impl<'a, 'f, 't> BindingBuilder<'a, 'f, 't> {
36423642
/// See [`Self::transform_from_with_values`] for a version which operates on `glib::Value`s.
36433643
pub fn transform_from<
36443644
S: FromValue<'f>,
3645-
T: ToValue,
3645+
T: Into<Value>,
36463646
F: Fn(&'f crate::Binding, S) -> Option<T> + Send + Sync + 'static,
36473647
>(
36483648
self,
@@ -3651,7 +3651,7 @@ impl<'a, 'f, 't> BindingBuilder<'a, 'f, 't> {
36513651
Self {
36523652
transform_from: Some(Box::new(move |binding, from_value| {
36533653
let from_value = from_value.get().expect("Wrong value type");
3654-
func(binding, from_value).map(|r| r.to_value())
3654+
func(binding, from_value).map(|r| r.into())
36553655
})),
36563656
..self
36573657
}
@@ -3681,7 +3681,7 @@ impl<'a, 'f, 't> BindingBuilder<'a, 'f, 't> {
36813681
/// See [`Self::transform_to_with_values`] for a version which operates on `glib::Value`s.
36823682
pub fn transform_to<
36833683
S: FromValue<'t>,
3684-
T: ToValue,
3684+
T: Into<Value>,
36853685
F: Fn(&'t crate::Binding, S) -> Option<T> + Send + Sync + 'static,
36863686
>(
36873687
self,
@@ -3690,7 +3690,7 @@ impl<'a, 'f, 't> BindingBuilder<'a, 'f, 't> {
36903690
Self {
36913691
transform_to: Some(Box::new(move |binding, from_value| {
36923692
let from_value = from_value.get().expect("Wrong value type");
3693-
func(binding, from_value).map(|r| r.to_value())
3693+
func(binding, from_value).map(|r| r.into())
36943694
})),
36953695
..self
36963696
}

0 commit comments

Comments
 (0)