Debian Codegraph @

examples/proof-token.rs

raw symbols SCIP
1 // This example demonstrates using `Target` to create a "proof token type", a token that
2 // demonstrates that particular target features have already been detected, and that those features
3 // can be used safely.
4
5 #![allow(unused, unused_macros, unused_imports)]
6
7 use target_featuresrust-analyzer cargo target-features 0.1.6 crate/::Targetrust-analyzer cargo target-features 0.1.6 Target#;
8
9 /// Make sure proof tokens can't be improperly constructed
10 mod unconstructiblerust-analyzer cargo target-features 0.1.6 unconstructible/ {
11 pub struct Unconstructiblerust-analyzer cargo target-features 0.1.6 unconstructible/Unconstructible#(());
12 impl Unconstructiblerust-analyzer cargo target-features 0.1.6 unconstructible/Unconstructible# {
13 pub unsafe fn newrust-analyzer cargo target-features 0.1.6 unconstructible/impl#[Unconstructible]new().() -> Selfrust-analyzer cargo target-features 0.1.6 unconstructible/impl#[Unconstructible] {
14 Selfrust-analyzer cargo target-features 0.1.6 unconstructible/impl#[Unconstructible](())
15 }
16 }
17 }
18 use unconstructiblerust-analyzer cargo target-features 0.1.6 unconstructible/::Unconstructiblerust-analyzer cargo target-features 0.1.6 unconstructible/Unconstructible#;
19
20 /// Proof of target feature support.
21 ///
22 /// # Safety
23 /// The type must be implemented such that it's impossible to safely construct without ensuring the
24 /// specified target features are supported.
25 unsafe trait Proofrust-analyzer cargo target-features 0.1.6 Proof#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
26 /// The proven target
27 const TARGETrust-analyzer cargo target-features 0.1.6 Proof#TARGET.: Targetrust-analyzer cargo target-features 0.1.6 Target#;
28
29 /// Detect the support for the target features
30 fn detectrust-analyzer cargo target-features 0.1.6 Proof#detect().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Self>;
31
32 /// Assume the target features are supported
33 ///
34 /// # Safety
35 /// Calling this is undefined if the target features are not supported
36 unsafe fn assumerust-analyzer cargo target-features 0.1.6 Proof#assume().() -> Self;
37 }
38
39 /// Make a proof token type for a particular set of features
40 macro_rules! make_target_proofrust-analyzer cargo target-features 0.1.6 make_target_proof! {
41 { $vis:vis struct $proof:ident($($feature:tt),*); } => {
42 $vis struct $proof(Unconstructible);
43
44 unsafe impl Proof for $proof {
45 // Build on the already-known target features
46 const TARGET: Target = target_features::CURRENT_TARGET$(.with_feature_str($feature))*;
47
48 fn detect() -> Option<Self> {
49 if true $(&& is_x86_feature_detected!($feature))* {
50 unsafe { Some(Self::assume()) }
51 } else {
52 None
53 }
54 }
55
56 unsafe fn assume() -> Self {
57 Self(Unconstructible::new())
58 }
59 }
60 }
61 }
62
63 /// A function that can only be called with the "avx" feature, or panics otherwise.
64 #[cfg(target_arch = "x86_64")]
65 fn safe_avx_fnrust-analyzer cargo target-features 0.1.6 safe_avx_fn().<P: Proofrust-analyzer cargo target-features 0.1.6 Proof#>(_: P) {
66 #[target_feature(enable = "avx")]
67 unsafe fn unsafe_avx_fnrust-analyzer cargo target-features 0.1.6 unsafe_avx_fn().() {
68 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("called an avx function")
69 }
70
71 // Future improvements to const generics might make it possible to assert this at compile time.
72 // Since P::TARGET is const, this assert disappears if the required features are present.
73 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
74 P::TARGETrust-analyzer cargo target-features 0.1.6 Proof#TARGET..supports_feature_strrust-analyzer cargo target-features 0.1.6 impl#[Target]supports_feature_str().("avx"),
75 "avx feature not supported"
76 );
77 unsafe { unsafe_avx_fnrust-analyzer cargo target-features 0.1.6 unsafe_avx_fn().() }
78 }
79
80 #[cfg(target_arch = "x86_64")]
81 fn mainrust-analyzer cargo target-features 0.1.6 main().() {
82 // The function can be called with the exact features
83 make_target_proofrust-analyzer cargo target-features 0.1.6 make_target_proof!! {
84 struct Avxrust-analyzer cargo target-features 0.1.6 Avx#("avx");
85 }
86 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(proof) = Avxrust-analyzer cargo target-features 0.1.6 Avx#::detectrust-analyzer cargo target-features 0.1.6 impl#[Avx][Proof]detect().() {
87 safe_avx_fnrust-analyzer cargo target-features 0.1.6 safe_avx_fn().(proof);
88 }
89
90 // The function can also be called with a target that implies the required features
91 make_target_proofrust-analyzer cargo target-features 0.1.6 make_target_proof!! {
92 struct Avx2rust-analyzer cargo target-features 0.1.6 Avx2#("avx2");
93 }
94 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(proof) = Avx2rust-analyzer cargo target-features 0.1.6 Avx2#::detectrust-analyzer cargo target-features 0.1.6 impl#[Avx2][Proof]detect().() {
95 safe_avx_fnrust-analyzer cargo target-features 0.1.6 safe_avx_fn().(proof);
96 }
97
98 // This panics, unless compiled with something like `-Ctarget-feature=+avx`
99 make_target_proofrust-analyzer cargo target-features 0.1.6 make_target_proof!! {
100 struct Aesrust-analyzer cargo target-features 0.1.6 Aes#("aes");
101 }
102 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(proof) = Aesrust-analyzer cargo target-features 0.1.6 Aes#::detectrust-analyzer cargo target-features 0.1.6 impl#[Aes][Proof]detect().() {
103 safe_avx_fnrust-analyzer cargo target-features 0.1.6 safe_avx_fn().(proof);
104 }
105 }
106
107 #[cfg(not(target_arch = "x86_64"))]
108 fn main() {}