@@ -5,7 +5,7 @@ use std::{cmp, ffi::CStr, fmt, ptr};
5
5
use crate :: {
6
6
translate:: * ,
7
7
value:: { FromValue , ValueTypeChecker } ,
8
- Type , Value ,
8
+ HasParamSpec , ParamSpecEnum , ParamSpecFlags , StaticType , Type , Value ,
9
9
} ;
10
10
11
11
#[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash ) ]
@@ -67,11 +67,18 @@ impl fmt::Debug for EnumClass {
67
67
}
68
68
69
69
impl EnumClass {
70
+ // rustdoc-stripper-ignore-next
71
+ /// Create a new `EnumClass` from a static type `T`.
72
+ ///
73
+ /// Panics if `T` is not representing an enum.
74
+ pub fn new < T : StaticType + HasParamSpec < ParamSpec = ParamSpecEnum > > ( ) -> Self {
75
+ Self :: with_type ( T :: static_type ( ) ) . expect ( "invalid enum class" )
76
+ }
70
77
// rustdoc-stripper-ignore-next
71
78
/// Create a new `EnumClass` from a `Type`.
72
79
///
73
80
/// Returns `None` if `type_` is not representing an enum.
74
- pub fn new ( type_ : Type ) -> Option < Self > {
81
+ pub fn with_type ( type_ : Type ) -> Option < Self > {
75
82
unsafe {
76
83
let is_enum: bool = from_glib ( gobject_ffi:: g_type_is_a (
77
84
type_. into_glib ( ) ,
@@ -255,7 +262,7 @@ impl EnumValue {
255
262
/// Convert enum value from a `Value`.
256
263
pub fn from_value ( value : & Value ) -> Option < ( EnumClass , & EnumValue ) > {
257
264
unsafe {
258
- let enum_class = EnumClass :: new ( value. type_ ( ) ) ?;
265
+ let enum_class = EnumClass :: with_type ( value. type_ ( ) ) ?;
259
266
let v = enum_class. value ( gobject_ffi:: g_value_get_enum ( value. to_glib_none ( ) . 0 ) ) ?;
260
267
let v = & * ( v as * const EnumValue ) ;
261
268
Some ( ( enum_class, v) )
@@ -341,11 +348,18 @@ impl fmt::Debug for FlagsClass {
341
348
}
342
349
343
350
impl FlagsClass {
351
+ // rustdoc-stripper-ignore-next
352
+ /// Create a new `FlagsClass` from a static type `T`.
353
+ ///
354
+ /// Panics if `T` is not representing an flags type.
355
+ pub fn new < T : StaticType + HasParamSpec < ParamSpec = ParamSpecFlags > > ( ) -> Self {
356
+ Self :: with_type ( T :: static_type ( ) ) . expect ( "invalid flags class" )
357
+ }
344
358
// rustdoc-stripper-ignore-next
345
359
/// Create a new `FlagsClass` from a `Type`
346
360
///
347
361
/// Returns `None` if `type_` is not representing a flags type.
348
- pub fn new ( type_ : Type ) -> Option < Self > {
362
+ pub fn with_type ( type_ : Type ) -> Option < Self > {
349
363
unsafe {
350
364
let is_flags: bool = from_glib ( gobject_ffi:: g_type_is_a (
351
365
type_. into_glib ( ) ,
@@ -771,7 +785,7 @@ impl FlagsValue {
771
785
/// Convert flags values from a `Value`. This returns all flags that are set.
772
786
pub fn from_value ( value : & Value ) -> Option < ( FlagsClass , Vec < & FlagsValue > ) > {
773
787
unsafe {
774
- let flags_class = FlagsClass :: new ( value. type_ ( ) ) ?;
788
+ let flags_class = FlagsClass :: with_type ( value. type_ ( ) ) ?;
775
789
let mut res = Vec :: new ( ) ;
776
790
let f = gobject_ffi:: g_value_get_flags ( value. to_glib_none ( ) . 0 ) ;
777
791
for v in flags_class. values ( ) {
@@ -931,11 +945,10 @@ impl std::error::Error for InvalidFlagsError {}
931
945
#[ cfg( test) ]
932
946
mod tests {
933
947
use super :: * ;
934
- use crate :: StaticType ;
935
948
936
949
#[ test]
937
950
fn test_flags ( ) {
938
- let flags = FlagsClass :: new ( crate :: BindingFlags :: static_type ( ) ) . unwrap ( ) ;
951
+ let flags = FlagsClass :: new :: < crate :: BindingFlags > ( ) ;
939
952
let values = flags. values ( ) ;
940
953
let def1 = values
941
954
. iter ( )
0 commit comments