@@ -400,70 +400,20 @@ macro_rules! bitflags_array {
400400 ) *
401401 ) *
402402 }
403- } ;
404- }
405403
406- #[ cfg( feature = "serde" ) ]
407- #[ test]
408- fn check_hex ( ) {
409- let mut hex = alloc:: string:: String :: new ( ) ;
410- FeatureBits :: ALL . write_hex ( & mut hex) . unwrap ( ) ;
411- assert_eq ! (
412- FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
413- FeatureBits :: ALL
414- ) ;
415- hex. clear ( ) ;
416- FeatureBits :: EMPTY . write_hex ( & mut hex) . unwrap ( ) ;
417- assert_eq ! (
418- FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
419- FeatureBits :: EMPTY
420- ) ;
421- for feature in Features :: FLAGS {
422- hex. clear ( ) ;
423- feature. value ( ) . bits ( ) . write_hex ( & mut hex) . unwrap ( ) ;
424- assert_eq ! (
425- FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
426- feature. value( ) . bits( ) ,
427- "{hex}"
428- ) ;
429- }
430- }
431-
432- #[ test]
433- fn check_features_display ( ) {
434- use alloc:: format;
435- let feature = Features :: CLEAR_TEXTURE ;
436- assert_eq ! ( format!( "{}" , feature) , "CLEAR_TEXTURE" ) ;
437- let feature = Features :: CLEAR_TEXTURE | Features :: BGRA8UNORM_STORAGE ;
438- assert_eq ! ( format!( "{}" , feature) , "CLEAR_TEXTURE | BGRA8UNORM_STORAGE" ) ;
439- }
440-
441- #[ test]
442- fn check_features_bits ( ) {
443- let bits = Features :: all ( ) . bits ( ) ;
444- assert_eq ! ( Features :: from_bits_retain( bits) , Features :: all( ) ) ;
445- let bits = Features :: empty ( ) . bits ( ) ;
446- assert_eq ! ( Features :: from_bits_retain( bits) , Features :: empty( ) ) ;
447- for feature in Features :: FLAGS {
448- let bits = feature. value ( ) . bits ( ) ;
449- assert_eq ! ( Features :: from_bits_retain( bits) , * feature. value( ) ) ;
450- }
451- let bits = Features :: all ( ) . bits ( ) ;
452- assert_eq ! ( Features :: from_bits_truncate( bits) , Features :: all( ) ) ;
453- let bits = Features :: empty ( ) . bits ( ) ;
454- assert_eq ! ( Features :: from_bits_truncate( bits) , Features :: empty( ) ) ;
455- for feature in Features :: FLAGS {
456- let bits = feature. value ( ) . bits ( ) ;
457- assert_eq ! ( Features :: from_bits_truncate( bits) , * feature. value( ) ) ;
458- }
459- let bits = Features :: all ( ) . bits ( ) ;
460- assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , Features :: all( ) ) ;
461- let bits = Features :: empty ( ) . bits ( ) ;
462- assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , Features :: empty( ) ) ;
463- for feature in Features :: FLAGS {
464- let bits = feature. value ( ) . bits ( ) ;
465- assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , * feature. value( ) ) ;
466- }
404+ $(
405+ impl From <$inner_name> for Features {
406+ // We need this for structs with only a member.
407+ #[ allow( clippy:: needless_update) ]
408+ fn from( $lower_inner_name: $inner_name) -> Self {
409+ Self {
410+ $lower_inner_name,
411+ ..Self :: empty( )
412+ }
413+ }
414+ }
415+ ) *
416+ } ;
467417}
468418
469419impl From < FeatureBits > for Features {
@@ -1357,3 +1307,113 @@ impl Features {
13571307 formats
13581308 }
13591309}
1310+
1311+ #[ cfg( test) ]
1312+ mod tests {
1313+ use crate :: { Features , FeaturesWGPU , FeaturesWebGPU } ;
1314+
1315+ #[ cfg( feature = "serde" ) ]
1316+ #[ test]
1317+ fn check_hex ( ) {
1318+ use crate :: FeatureBits ;
1319+
1320+ use bitflags:: {
1321+ parser:: { ParseHex as _, WriteHex as _} ,
1322+ Bits as _,
1323+ } ;
1324+
1325+ let mut hex = alloc:: string:: String :: new ( ) ;
1326+ FeatureBits :: ALL . write_hex ( & mut hex) . unwrap ( ) ;
1327+ assert_eq ! (
1328+ FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
1329+ FeatureBits :: ALL
1330+ ) ;
1331+
1332+ hex. clear ( ) ;
1333+ FeatureBits :: EMPTY . write_hex ( & mut hex) . unwrap ( ) ;
1334+ assert_eq ! (
1335+ FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
1336+ FeatureBits :: EMPTY
1337+ ) ;
1338+
1339+ for feature in Features :: FLAGS {
1340+ hex. clear ( ) ;
1341+ feature. value ( ) . bits ( ) . write_hex ( & mut hex) . unwrap ( ) ;
1342+ assert_eq ! (
1343+ FeatureBits :: parse_hex( hex. as_str( ) ) . unwrap( ) ,
1344+ feature. value( ) . bits( ) ,
1345+ "{hex}"
1346+ ) ;
1347+ }
1348+ }
1349+
1350+ #[ test]
1351+ fn check_features_display ( ) {
1352+ use alloc:: format;
1353+
1354+ let feature = Features :: CLEAR_TEXTURE ;
1355+ assert_eq ! ( format!( "{}" , feature) , "CLEAR_TEXTURE" ) ;
1356+
1357+ let feature = Features :: CLEAR_TEXTURE | Features :: BGRA8UNORM_STORAGE ;
1358+ assert_eq ! ( format!( "{}" , feature) , "CLEAR_TEXTURE | BGRA8UNORM_STORAGE" ) ;
1359+ }
1360+
1361+ #[ test]
1362+ fn check_features_bits ( ) {
1363+ let bits = Features :: all ( ) . bits ( ) ;
1364+ assert_eq ! ( Features :: from_bits_retain( bits) , Features :: all( ) ) ;
1365+
1366+ let bits = Features :: empty ( ) . bits ( ) ;
1367+ assert_eq ! ( Features :: from_bits_retain( bits) , Features :: empty( ) ) ;
1368+
1369+ for feature in Features :: FLAGS {
1370+ let bits = feature. value ( ) . bits ( ) ;
1371+ assert_eq ! ( Features :: from_bits_retain( bits) , * feature. value( ) ) ;
1372+ }
1373+
1374+ let bits = Features :: all ( ) . bits ( ) ;
1375+ assert_eq ! ( Features :: from_bits_truncate( bits) , Features :: all( ) ) ;
1376+
1377+ let bits = Features :: empty ( ) . bits ( ) ;
1378+ assert_eq ! ( Features :: from_bits_truncate( bits) , Features :: empty( ) ) ;
1379+
1380+ for feature in Features :: FLAGS {
1381+ let bits = feature. value ( ) . bits ( ) ;
1382+ assert_eq ! ( Features :: from_bits_truncate( bits) , * feature. value( ) ) ;
1383+ }
1384+
1385+ let bits = Features :: all ( ) . bits ( ) ;
1386+ assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , Features :: all( ) ) ;
1387+
1388+ let bits = Features :: empty ( ) . bits ( ) ;
1389+ assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , Features :: empty( ) ) ;
1390+
1391+ for feature in Features :: FLAGS {
1392+ let bits = feature. value ( ) . bits ( ) ;
1393+ assert_eq ! ( Features :: from_bits( bits) . unwrap( ) , * feature. value( ) ) ;
1394+ }
1395+ }
1396+
1397+ #[ test]
1398+ fn create_features_from_parts ( ) {
1399+ let features: Features = FeaturesWGPU :: TEXTURE_ATOMIC . into ( ) ;
1400+ assert_eq ! ( features, Features :: TEXTURE_ATOMIC ) ;
1401+
1402+ let features: Features = FeaturesWebGPU :: TIMESTAMP_QUERY . into ( ) ;
1403+ assert_eq ! ( features, Features :: TIMESTAMP_QUERY ) ;
1404+
1405+ let features: Features = Features :: from ( FeaturesWGPU :: TEXTURE_ATOMIC )
1406+ | Features :: from ( FeaturesWebGPU :: TIMESTAMP_QUERY ) ;
1407+ assert_eq ! (
1408+ features,
1409+ Features :: TEXTURE_ATOMIC | Features :: TIMESTAMP_QUERY
1410+ ) ;
1411+ assert_eq ! (
1412+ features,
1413+ Features :: from_internal_flags(
1414+ FeaturesWGPU :: TEXTURE_ATOMIC ,
1415+ FeaturesWebGPU :: TIMESTAMP_QUERY
1416+ )
1417+ ) ;
1418+ }
1419+ }
0 commit comments