@@ -5,17 +5,29 @@ pub const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
55
66/// A well-formed rust-gpu target.
77///
8- /// The constructors [`Self::from_target`] or [`Self::from_env`] only check whether the target is well-formed, not
9- /// whether it is valid. Since `spirv-builder` is backwards compatible with older rust-gpu compilers, only the compiler
10- /// itself knows what targets it can and cannot support. This also allows adding new targets to the compiler without
11- /// having to update `spirv-builder` and `cargo-gpu`.
8+ /// The constructors only check whether the target is well-formed, not whether it is valid. Since `spirv-builder` is
9+ /// backwards compatible with older rust-gpu compilers, only the compiler itself knows what targets it can and cannot
10+ /// support. This also allows adding new targets to the compiler without having to update `spirv-builder` and
11+ /// `cargo-gpu`.
12+ ///
13+ /// Differentiates between a full target (e.g. `spirv-unknown-vulkan1.3`) and a target env (e.g. `vulkan1.3`). Use
14+ /// [`Self::parse_target`] and [`Self::target`] to parse or format a full target, or use [`Self::parse_env`] and
15+ /// [`Self::env`] when dealing with just target envs. The convenience function [`Self::parse`] accepts both targets and
16+ /// target envs. Does not implement `Display`, since it is unclear whether the user wants a target or target env, though
17+ /// a `Debug` implementation is provided.
1218#[ derive( Clone ) ]
1319pub struct SpirvTarget {
1420 target : String ,
1521}
1622
1723impl SpirvTarget {
18- pub fn from_target ( target : & str ) -> Result < Self , TargetError > {
24+ /// Try to parse either a full target or a target env
25+ pub fn parse ( target_or_env : & str ) -> Result < Self , TargetError > {
26+ Self :: parse_target ( target_or_env) . or_else ( |_| Self :: parse_env ( target_or_env) )
27+ }
28+
29+ /// Parse a full target, e.g. `spirv-unknown-vulkan1.3`
30+ pub fn parse_target ( target : & str ) -> Result < Self , TargetError > {
1931 let _target_env = target. strip_prefix ( SPIRV_TARGET_PREFIX ) . ok_or_else ( || {
2032 TargetError :: NonSpirvTarget {
2133 target : target. to_string ( ) ,
@@ -26,17 +38,20 @@ impl SpirvTarget {
2638 } )
2739 }
2840
29- pub fn from_env ( target_env : & str ) -> Result < Self , TargetError > {
41+ /// Parse a target env, e.g. `vulkan1.3`
42+ pub fn parse_env ( target_env : & str ) -> Result < Self , TargetError > {
3043 Ok ( Self {
3144 target : format ! ( "{SPIRV_TARGET_PREFIX}{target_env}" ) ,
3245 } )
3346 }
3447
48+ /// returns the full target, e.g. `spirv-unknown-vulkan1.3`
3549 pub fn target ( & self ) -> & str {
3650 & self . target
3751 }
3852
39- pub fn target_env ( & self ) -> & str {
53+ /// returns the target env, e.g. `vulkan1.3`
54+ pub fn env ( & self ) -> & str {
4055 & self . target [ SPIRV_TARGET_PREFIX . len ( ) ..]
4156 }
4257}
0 commit comments