Skip to content

Commit 5091b7d

Browse files
Merge pull request #883 from sdroege/value-from-type
glib: Add Value::from_type_unchecked()
2 parents ae20f0d + 3ba6e70 commit 5091b7d

File tree

14 files changed

+61
-42
lines changed

14 files changed

+61
-42
lines changed

cairo/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ macro_rules! gvalue_impl {
7272
impl glib::value::ToValue for $name {
7373
fn to_value(&self) -> glib::Value {
7474
unsafe {
75-
let mut value =
76-
glib::Value::from_type(<$name as glib::StaticType>::static_type());
75+
let mut value = glib::Value::from_type_unchecked(
76+
<$name as glib::StaticType>::static_type(),
77+
);
7778
glib::gobject_ffi::g_value_take_boxed(
7879
value.to_glib_none_mut().0,
7980
self.to_glib_full() as *mut _,
@@ -90,8 +91,9 @@ macro_rules! gvalue_impl {
9091
impl From<$name> for glib::Value {
9192
fn from(v: $name) -> Self {
9293
unsafe {
93-
let mut value =
94-
glib::Value::from_type(<$name as glib::StaticType>::static_type());
94+
let mut value = glib::Value::from_type_unchecked(
95+
<$name as glib::StaticType>::static_type(),
96+
);
9597
glib::gobject_ffi::g_value_take_boxed(
9698
value.to_glib_none_mut().0,
9799
glib::translate::IntoGlibPtr::into_glib_ptr(v) as *mut _,
@@ -154,8 +156,9 @@ macro_rules! gvalue_impl_inline {
154156
let ptr =
155157
glib::ffi::g_malloc0(std::mem::size_of::<$ffi_name>()) as *mut $ffi_name;
156158
ptr.write(self.0);
157-
let mut value =
158-
glib::Value::from_type(<$name as glib::StaticType>::static_type());
159+
let mut value = glib::Value::from_type_unchecked(
160+
<$name as glib::StaticType>::static_type(),
161+
);
159162
glib::gobject_ffi::g_value_take_boxed(
160163
value.to_glib_none_mut().0,
161164
ptr as *mut _,

glib-macros/src/boxed_derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
153153
fn to_value(&self) -> #crate_ident::Value {
154154
unsafe {
155155
let ptr: *mut #name = ::std::boxed::Box::into_raw(::std::boxed::Box::new(self.clone()));
156-
let mut value = #crate_ident::Value::from_type(<#name as #crate_ident::StaticType>::static_type());
156+
let mut value = #crate_ident::Value::from_type_unchecked(<#name as #crate_ident::StaticType>::static_type());
157157
#crate_ident::gobject_ffi::g_value_take_boxed(
158158
#crate_ident::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
159159
ptr as *mut _
@@ -172,7 +172,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
172172
#[inline]
173173
fn from(v: #name) -> Self {
174174
unsafe {
175-
let mut value = #crate_ident::Value::from_type(<#name as #crate_ident::StaticType>::static_type());
175+
let mut value = #crate_ident::Value::from_type_unchecked(<#name as #crate_ident::StaticType>::static_type());
176176
#crate_ident::gobject_ffi::g_value_take_boxed(
177177
#crate_ident::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
178178
#crate_ident::translate::IntoGlibPtr::<*mut #name>::into_glib_ptr(v) as *mut _,

glib-macros/src/shared_boxed_derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub fn impl_shared_boxed(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
174174
fn to_value(&self) -> #crate_ident::Value {
175175
unsafe {
176176
let ptr = #refcounted_type_prefix::into_raw(self.0.clone());
177-
let mut value = #crate_ident::Value::from_type(<#name as #crate_ident::StaticType>::static_type());
177+
let mut value = #crate_ident::Value::from_type_unchecked(<#name as #crate_ident::StaticType>::static_type());
178178
#crate_ident::gobject_ffi::g_value_take_boxed(
179179
#crate_ident::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
180180
ptr as *mut _
@@ -193,7 +193,7 @@ pub fn impl_shared_boxed(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
193193
#[inline]
194194
fn from(v: #name) -> Self {
195195
unsafe {
196-
let mut value = #crate_ident::Value::from_type(<#name as #crate_ident::StaticType>::static_type());
196+
let mut value = #crate_ident::Value::from_type_unchecked(<#name as #crate_ident::StaticType>::static_type());
197197
#crate_ident::gobject_ffi::g_value_take_boxed(
198198
#crate_ident::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
199199
#crate_ident::translate::IntoGlibPtr::<*mut #refcounted_type_prefix::InnerType>::into_glib_ptr(v) as *mut _,

glib/src/boxed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ macro_rules! glib_boxed_wrapper {
344344
#[inline]
345345
fn to_value(&self) -> $crate::Value {
346346
unsafe {
347-
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
347+
let mut value = $crate::Value::from_type_unchecked(<Self as $crate::StaticType>::static_type());
348348
$crate::gobject_ffi::g_value_take_boxed(
349349
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
350350
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(self) as *mut _,
@@ -363,7 +363,7 @@ macro_rules! glib_boxed_wrapper {
363363
#[inline]
364364
fn from(o: $name $(<$($generic),+>)?) -> Self {
365365
unsafe {
366-
let mut value = $crate::Value::from_type(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
366+
let mut value = $crate::Value::from_type_unchecked(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
367367
$crate::gobject_ffi::g_value_take_boxed(
368368
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
369369
$crate::translate::IntoGlibPtr::<*mut $ffi_name>::into_glib_ptr(o) as *mut _,

glib/src/boxed_inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ macro_rules! glib_boxed_inline_wrapper {
588588
#[inline]
589589
fn to_value(&self) -> $crate::Value {
590590
unsafe {
591-
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
591+
let mut value = $crate::Value::from_type_unchecked(<Self as $crate::StaticType>::static_type());
592592
$crate::gobject_ffi::g_value_set_boxed(
593593
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
594594
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(self).0 as *mut _,

glib/src/enums.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl EnumValue {
245245
/// Convert enum value to a `Value`.
246246
pub fn to_value(&self, enum_: &EnumClass) -> Value {
247247
unsafe {
248-
let mut v = Value::from_type(enum_.type_());
248+
let mut v = Value::from_type_unchecked(enum_.type_());
249249
gobject_ffi::g_value_set_enum(v.to_glib_none_mut().0, self.0.value);
250250
v
251251
}
@@ -761,7 +761,7 @@ impl FlagsValue {
761761
/// Convert flags value to a `Value`.
762762
pub fn to_value(&self, flags: &FlagsClass) -> Value {
763763
unsafe {
764-
let mut v = Value::from_type(flags.type_());
764+
let mut v = Value::from_type_unchecked(flags.type_());
765765
gobject_ffi::g_value_set_flags(v.to_glib_none_mut().0, self.0.value);
766766
v
767767
}
@@ -814,7 +814,7 @@ impl Eq for FlagsValue {}
814814
pub struct FlagsBuilder<'a>(&'a FlagsClass, Option<Value>);
815815
impl<'a> FlagsBuilder<'a> {
816816
fn new(flags_class: &FlagsClass) -> FlagsBuilder {
817-
let value = Value::from_type(flags_class.type_());
817+
let value = unsafe { Value::from_type_unchecked(flags_class.type_()) };
818818
FlagsBuilder(flags_class, Some(value))
819819
}
820820

glib/src/object.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ macro_rules! glib_object_wrapper {
10701070
#[inline]
10711071
fn to_value(&self) -> $crate::Value {
10721072
unsafe {
1073-
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
1073+
let mut value = $crate::Value::from_type_unchecked(<Self as $crate::StaticType>::static_type());
10741074
$crate::gobject_ffi::g_value_take_object(
10751075
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
10761076
$crate::translate::ToGlibPtr::<*mut $ffi_name>::to_glib_full(self) as *mut _,
@@ -1090,7 +1090,7 @@ macro_rules! glib_object_wrapper {
10901090
#[inline]
10911091
fn from(o: $name $(<$($generic),+>)?) -> Self {
10921092
unsafe {
1093-
let mut value = $crate::Value::from_type(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
1093+
let mut value = $crate::Value::from_type_unchecked(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
10941094
$crate::gobject_ffi::g_value_take_object(
10951095
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
10961096
$crate::translate::IntoGlibPtr::<*mut $ffi_name>::into_glib_ptr(o) as *mut _,
@@ -2363,7 +2363,7 @@ impl<T: ObjectType> ObjectExt for T {
23632363
}
23642364

23652365
unsafe {
2366-
let mut value = Value::from_type(pspec.value_type());
2366+
let mut value = Value::from_type_unchecked(pspec.value_type());
23672367
gobject_ffi::g_object_get_property(
23682368
self.as_object_ref().to_glib_none().0,
23692369
pspec.name().as_ptr() as *const _,
@@ -2719,7 +2719,7 @@ impl<T: ObjectType> ObjectExt for T {
27192719
validate_signal_arguments(type_, &signal_query, &mut args[1..]);
27202720

27212721
let mut return_value = if signal_query.return_type() != Type::UNIT {
2722-
Value::from_type(signal_query.return_type().into())
2722+
Value::from_type_unchecked(signal_query.return_type().into())
27232723
} else {
27242724
Value::uninitialized()
27252725
};
@@ -2766,7 +2766,7 @@ impl<T: ObjectType> ObjectExt for T {
27662766
validate_signal_arguments(type_, &signal_query, &mut args[1..]);
27672767

27682768
let mut return_value = if signal_query.return_type() != Type::UNIT {
2769-
Value::from_type(signal_query.return_type().into())
2769+
Value::from_type_unchecked(signal_query.return_type().into())
27702770
} else {
27712771
Value::uninitialized()
27722772
};
@@ -2867,7 +2867,7 @@ impl<T: ObjectType> ObjectExt for T {
28672867
validate_signal_arguments(type_, &signal_query, &mut args[1..]);
28682868

28692869
let mut return_value = if signal_query.return_type() != Type::UNIT {
2870-
Value::from_type(signal_query.return_type().into())
2870+
Value::from_type_unchecked(signal_query.return_type().into())
28712871
} else {
28722872
Value::uninitialized()
28732873
};
@@ -2920,7 +2920,7 @@ impl<T: ObjectType> ObjectExt for T {
29202920
validate_signal_arguments(type_, &signal_query, &mut args[1..]);
29212921

29222922
let mut return_value = if signal_query.return_type() != Type::UNIT {
2923-
Value::from_type(signal_query.return_type().into())
2923+
Value::from_type_unchecked(signal_query.return_type().into())
29242924
} else {
29252925
Value::uninitialized()
29262926
};

glib/src/param_spec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ unsafe impl<'a> crate::value::FromValue<'a> for &'a ParamSpec {
6969
impl crate::value::ToValue for ParamSpec {
7070
fn to_value(&self) -> crate::Value {
7171
unsafe {
72-
let mut value = crate::Value::from_type(ParamSpec::static_type());
72+
let mut value = crate::Value::from_type_unchecked(ParamSpec::static_type());
7373
gobject_ffi::g_value_take_param(value.to_glib_none_mut().0, self.to_glib_full());
7474
value
7575
}
@@ -85,7 +85,7 @@ impl From<ParamSpec> for crate::Value {
8585
#[inline]
8686
fn from(s: ParamSpec) -> Self {
8787
unsafe {
88-
let mut value = crate::Value::from_type(ParamSpec::static_type());
88+
let mut value = crate::Value::from_type_unchecked(ParamSpec::static_type());
8989
gobject_ffi::g_value_take_param(value.to_glib_none_mut().0, s.into_glib_ptr());
9090
value
9191
}
@@ -349,7 +349,7 @@ macro_rules! define_param_spec {
349349
impl crate::value::ToValue for $rust_type {
350350
fn to_value(&self) -> crate::Value {
351351
unsafe {
352-
let mut value = crate::Value::from_type($rust_type::static_type());
352+
let mut value = crate::Value::from_type_unchecked($rust_type::static_type());
353353
gobject_ffi::g_value_take_param(value.to_glib_none_mut().0, $crate::translate::ToGlibPtr::<*const $ffi_type>::to_glib_full(self) as *mut _);
354354
value
355355
}
@@ -365,7 +365,7 @@ macro_rules! define_param_spec {
365365
#[inline]
366366
fn from(s: $rust_type) -> Self {
367367
unsafe {
368-
let mut value = crate::Value::from_type($rust_type::static_type());
368+
let mut value = crate::Value::from_type_unchecked($rust_type::static_type());
369369
gobject_ffi::g_value_take_param(
370370
value.to_glib_none_mut().0,
371371
$crate::translate::IntoGlibPtr::<*mut gobject_ffi::GParamSpec>::into_glib_ptr(s),

glib/src/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ macro_rules! glib_shared_wrapper {
379379
#[inline]
380380
fn to_value(&self) -> $crate::Value {
381381
unsafe {
382-
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
382+
let mut value = $crate::Value::from_type_unchecked(<Self as $crate::StaticType>::static_type());
383383
$crate::gobject_ffi::g_value_take_boxed(
384384
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
385385
$crate::translate::ToGlibPtr::<*mut $ffi_name>::to_glib_full(self) as *mut _,
@@ -398,7 +398,7 @@ macro_rules! glib_shared_wrapper {
398398
#[inline]
399399
fn from(s: $name $(<$($generic),+>)?) -> Self {
400400
unsafe {
401-
let mut value = $crate::Value::from_type(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
401+
let mut value = $crate::Value::from_type_unchecked(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
402402
$crate::gobject_ffi::g_value_take_boxed(
403403
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
404404
$crate::translate::IntoGlibPtr::<*mut $ffi_name>::into_glib_ptr(s) as *mut _,

glib/src/subclass/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ pub(crate) unsafe fn signal_chain_from_overridden(
10811081
"Arguments must be forwarded without changes when chaining up"
10821082
);
10831083

1084-
let mut result = Value::from_type(token.1);
1084+
let mut result = Value::from_type_unchecked(token.1);
10851085
gobject_ffi::g_signal_chain_from_overridden(
10861086
values.as_ptr() as *mut Value as *mut gobject_ffi::GValue,
10871087
result.to_glib_none_mut().0,

0 commit comments

Comments
 (0)