1 // Copyright (c) 2016 The vulkano developers
2 // Licensed under the Apache License, Version 2.0
3 // <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
6 // at your option. All files in the project carrying such
7 // notice may not be copied, modified, or distributed except
8 // according to those terms.
9 
10 use std::{
11     error::Error,
12     fmt::{Display, Error as FmtError, Formatter},
13 };
14 
15 // Generated by build.rs
16 include!(concat!(env!("OUT_DIR"), "/features.rs"));
17 
18 /// An error that can happen when enabling a feature on a device.
19 #[derive(Clone, Copy, Debug)]
20 pub struct FeatureRestrictionError {
21     /// The feature in question.
22     pub feature: &'static str,
23     /// The restriction that was not met.
24     pub restriction: FeatureRestriction,
25 }
26 
27 impl Error for FeatureRestrictionError {}
28 
29 impl Display for FeatureRestrictionError {
fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>30     fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
31         write!(
32             f,
33             "a restriction for the feature {} was not met: {}",
34             self.feature, self.restriction,
35         )
36     }
37 }
38 
39 #[derive(Clone, Copy, Debug)]
40 pub enum FeatureRestriction {
41     /// Not supported by the physical device.
42     NotSupported,
43     /// Requires a feature to be enabled.
44     RequiresFeature(&'static str),
45     /// Requires a feature to be disabled.
46     ConflictsFeature(&'static str),
47     /// An extension requires this feature to be enabled.
48     RequiredByExtension(&'static str),
49 }
50 
51 impl Display for FeatureRestriction {
fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>52     fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
53         match *self {
54             FeatureRestriction::NotSupported => {
55                 write!(f, "not supported by the physical device")
56             }
57             FeatureRestriction::RequiresFeature(feat) => {
58                 write!(f, "requires feature {} to be enabled", feat)
59             }
60             FeatureRestriction::ConflictsFeature(feat) => {
61                 write!(f, "requires feature {} to be disabled", feat)
62             }
63             FeatureRestriction::RequiredByExtension(ext) => {
64                 write!(f, "required to be enabled by extension {}", ext)
65             }
66         }
67     }
68 }
69 
70 #[cfg(test)]
71 mod tests {
72     use super::Features;
73 
74     #[test]
into_iter()75     fn into_iter() {
76         let features = Features {
77             tessellation_shader: true,
78             ..Features::empty()
79         };
80         for (name, enabled) in features {
81             if name == "tessellationShader" {
82                 assert!(enabled);
83             } else {
84                 assert!(!enabled);
85             }
86         }
87     }
88 }
89