@@ -236,10 +236,20 @@ impl Variant {
236
236
#[ inline]
237
237
#[ doc( alias = "g_variant_is_of_type" ) ]
238
238
pub fn is < T : StaticVariantType > ( & self ) -> bool {
239
+ self . is_type ( & T :: static_variant_type ( ) )
240
+ }
241
+
242
+ // rustdoc-stripper-ignore-next
243
+ /// Returns `true` if the type of the value corresponds to `type_`.
244
+ ///
245
+ /// This is equivalent to [`self.type_().is_subtype_of(type_)`](VariantTy::is_subtype_of).
246
+ #[ inline]
247
+ #[ doc( alias = "g_variant_is_of_type" ) ]
248
+ pub fn is_type ( & self , type_ : & VariantTy ) -> bool {
239
249
unsafe {
240
250
from_glib ( ffi:: g_variant_is_of_type (
241
251
self . to_glib_none ( ) . 0 ,
242
- T :: static_variant_type ( ) . to_glib_none ( ) . 0 ,
252
+ type_ . to_glib_none ( ) . 0 ,
243
253
) )
244
254
}
245
255
}
@@ -415,8 +425,20 @@ impl Variant {
415
425
pub fn array_from_iter < T : StaticVariantType , I : IntoIterator < Item = Variant > > (
416
426
children : I ,
417
427
) -> Self {
418
- let type_ = T :: static_variant_type ( ) ;
428
+ Self :: array_from_iter_with_type ( & T :: static_variant_type ( ) , children)
429
+ }
419
430
431
+ // rustdoc-stripper-ignore-next
432
+ /// Creates a new Variant array from children with the specified type.
433
+ ///
434
+ /// # Panics
435
+ ///
436
+ /// This function panics if not all variants are of type `type_`.
437
+ #[ doc( alias = "g_variant_new_array" ) ]
438
+ pub fn array_from_iter_with_type < I : IntoIterator < Item = Variant > > (
439
+ type_ : & VariantTy ,
440
+ children : I ,
441
+ ) -> Self {
420
442
unsafe {
421
443
let mut builder = mem:: MaybeUninit :: uninit ( ) ;
422
444
ffi:: g_variant_builder_init ( builder. as_mut_ptr ( ) , type_. as_array ( ) . to_glib_none ( ) . 0 ) ;
@@ -426,7 +448,7 @@ impl Variant {
426
448
== ffi:: GFALSE
427
449
{
428
450
ffi:: g_variant_builder_clear ( & mut builder) ;
429
- assert ! ( value. is :: < T > ( ) ) ;
451
+ assert ! ( value. is_type ( type_ ) ) ;
430
452
}
431
453
432
454
ffi:: g_variant_builder_add_value ( & mut builder, value. to_glib_none ( ) . 0 ) ;
@@ -471,22 +493,55 @@ impl Variant {
471
493
#[ doc( alias = "g_variant_new_maybe" ) ]
472
494
pub fn from_maybe < T : StaticVariantType > ( child : Option < & Variant > ) -> Self {
473
495
let type_ = T :: static_variant_type ( ) ;
474
- let ptr = match child {
496
+ match child {
475
497
Some ( child) => {
476
498
assert_eq ! ( type_, child. type_( ) ) ;
477
499
478
- child . to_glib_none ( ) . 0
500
+ Self :: from_some ( child )
479
501
}
480
- None => std:: ptr:: null ( ) ,
481
- } ;
502
+ None => Self :: from_none ( & type_) ,
503
+ }
504
+ }
505
+
506
+ // rustdoc-stripper-ignore-next
507
+ /// Creates a new maybe Variant from a child.
508
+ #[ doc( alias = "g_variant_new_maybe" ) ]
509
+ pub fn from_some ( child : & Variant ) -> Self {
482
510
unsafe {
483
511
from_glib_none ( ffi:: g_variant_new_maybe (
484
- type_ . as_ptr ( ) as * const _ ,
485
- ptr as * mut ffi :: GVariant ,
512
+ ptr :: null ( ) ,
513
+ child . to_glib_none ( ) . 0 ,
486
514
) )
487
515
}
488
516
}
489
517
518
+ // rustdoc-stripper-ignore-next
519
+ /// Creates a new maybe Variant with Nothing.
520
+ #[ doc( alias = "g_variant_new_maybe" ) ]
521
+ pub fn from_none ( type_ : & VariantTy ) -> Self {
522
+ unsafe {
523
+ from_glib_none ( ffi:: g_variant_new_maybe (
524
+ type_. to_glib_none ( ) . 0 ,
525
+ ptr:: null_mut ( ) ,
526
+ ) )
527
+ }
528
+ }
529
+
530
+ // rustdoc-stripper-ignore-next
531
+ /// Extract the value of a maybe Variant.
532
+ ///
533
+ /// Returns the child value, or `None` if the value is Nothing.
534
+ ///
535
+ /// # Panics
536
+ ///
537
+ /// Panics if compiled with `debug_assertions` and the variant is not maybe-typed.
538
+ #[ inline]
539
+ pub fn as_maybe ( & self ) -> Option < Variant > {
540
+ debug_assert ! ( self . type_( ) . is_maybe( ) ) ;
541
+
542
+ unsafe { from_glib_full ( ffi:: g_variant_get_maybe ( self . to_glib_none ( ) . 0 ) ) }
543
+ }
544
+
490
545
// rustdoc-stripper-ignore-next
491
546
/// Constructs a new serialized-mode GVariant instance.
492
547
#[ doc( alias = "g_variant_new_from_bytes" ) ]
@@ -1923,6 +1978,19 @@ mod tests {
1923
1978
assert_eq ! ( a. get:: <( ) >( ) , Some ( ( ) ) ) ;
1924
1979
}
1925
1980
1981
+ #[ test]
1982
+ fn test_maybe ( ) {
1983
+ assert ! ( <Option <( ) >>:: static_variant_type( ) . is_maybe( ) ) ;
1984
+ let m1 = Some ( ( ) ) . to_variant ( ) ;
1985
+ assert_eq ! ( m1. type_( ) . as_str( ) , "m()" ) ;
1986
+
1987
+ assert_eq ! ( m1. get:: <Option <( ) >>( ) , Some ( Some ( ( ) ) ) ) ;
1988
+ assert ! ( m1. as_maybe( ) . is_some( ) ) ;
1989
+
1990
+ let m2 = None :: < ( ) > . to_variant ( ) ;
1991
+ assert ! ( m2. as_maybe( ) . is_none( ) ) ;
1992
+ }
1993
+
1926
1994
#[ test]
1927
1995
fn test_btreemap ( ) {
1928
1996
assert_eq ! (
0 commit comments