Skip to content

Commit 96ec7f4

Browse files
committed
glib: Add Value::from_type_unchecked()
This avoids the type checking and panic code generation.
1 parent ae20f0d commit 96ec7f4

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

glib/src/value.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,30 @@ crate::wrapper! {
491491

492492
impl Value {
493493
// rustdoc-stripper-ignore-next
494-
/// Creates a new `Value` that is initialized with `type_`
495-
#[inline]
494+
/// Creates a new `Value` that is initialized with `type_`.
495+
///
496+
/// # Panics
497+
///
498+
/// If `type_` can't be stored in a `Value` this function panics.
496499
pub fn from_type(type_: Type) -> Self {
497500
unsafe {
498501
assert_eq!(
499502
gobject_ffi::g_type_check_is_value_type(type_.into_glib()),
500503
ffi::GTRUE
501504
);
505+
Self::from_type_unchecked(type_)
506+
}
507+
}
508+
509+
// rustdoc-stripper-ignore-next
510+
/// Creates a new `Value` that is initialized with `type_`.
511+
///
512+
/// # SAFETY
513+
///
514+
/// This must be called with a valid `type_` that can be stored in `Value`s.
515+
#[inline]
516+
pub unsafe fn from_type_unchecked(type_: Type) -> Self {
517+
unsafe {
502518
let mut value = Value::uninitialized();
503519
gobject_ffi::g_value_init(value.to_glib_none_mut().0, type_.into_glib());
504520
value
@@ -509,7 +525,7 @@ impl Value {
509525
/// Creates a new `Value` that is initialized for a given `ValueType`.
510526
#[inline]
511527
pub fn for_value_type<T: ValueType>() -> Self {
512-
Value::from_type(T::Type::static_type())
528+
unsafe { Value::from_type_unchecked(T::Type::static_type()) }
513529
}
514530

515531
// rustdoc-stripper-ignore-next

0 commit comments

Comments
 (0)