Skip to content

Commit 3b65aa5

Browse files
committed
chore: Make Features::extend() future-proof + add test
Modify Features::extend() to cause compile errors when a new feature is added. Also add a test for extending.
1 parent c369938 commit 3b65aa5

File tree

1 file changed

+74
-12
lines changed

1 file changed

+74
-12
lines changed

lib/types/src/features.rs

+74-12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ impl Features {
6363
}
6464
}
6565

66+
/// Create a new feature set with all features enabled.
67+
pub fn all() -> Self {
68+
Self {
69+
threads: true,
70+
reference_types: true,
71+
simd: true,
72+
bulk_memory: true,
73+
multi_value: true,
74+
tail_call: true,
75+
module_linking: true,
76+
multi_memory: true,
77+
memory64: true,
78+
exceptions: true,
79+
relaxed_simd: true,
80+
extended_const: true,
81+
}
82+
}
83+
84+
/// Create a new feature set with all features disabled.
85+
pub fn none() -> Self {
86+
Self {
87+
threads: false,
88+
reference_types: false,
89+
simd: false,
90+
bulk_memory: false,
91+
multi_value: false,
92+
tail_call: false,
93+
module_linking: false,
94+
multi_memory: false,
95+
memory64: false,
96+
exceptions: false,
97+
relaxed_simd: false,
98+
extended_const: false,
99+
}
100+
}
101+
66102
/// Configures whether the WebAssembly threads proposal will be enabled.
67103
///
68104
/// The [WebAssembly threads proposal][threads] is not currently fully
@@ -393,18 +429,36 @@ impl Features {
393429
/// Self will be modified to include all features that are required by
394430
/// either set.
395431
pub fn extend(&mut self, other: &Self) {
396-
self.threads |= other.threads;
397-
self.reference_types |= other.reference_types;
398-
self.simd |= other.simd;
399-
self.bulk_memory |= other.bulk_memory;
400-
self.multi_value |= other.multi_value;
401-
self.tail_call |= other.tail_call;
402-
self.module_linking |= other.module_linking;
403-
self.multi_memory |= other.multi_memory;
404-
self.memory64 |= other.memory64;
405-
self.exceptions |= other.exceptions;
406-
self.relaxed_simd |= other.relaxed_simd;
407-
self.extended_const |= other.extended_const;
432+
// Written this way to cause compile errors when new features are added.
433+
let Self {
434+
threads,
435+
reference_types,
436+
simd,
437+
bulk_memory,
438+
multi_value,
439+
tail_call,
440+
module_linking,
441+
multi_memory,
442+
memory64,
443+
exceptions,
444+
relaxed_simd,
445+
extended_const,
446+
} = other.clone();
447+
448+
*self = Self {
449+
threads: self.threads || threads,
450+
reference_types: self.reference_types || reference_types,
451+
simd: self.simd || simd,
452+
bulk_memory: self.bulk_memory || bulk_memory,
453+
multi_value: self.multi_value || multi_value,
454+
tail_call: self.tail_call || tail_call,
455+
module_linking: self.module_linking || module_linking,
456+
multi_memory: self.multi_memory || multi_memory,
457+
memory64: self.memory64 || memory64,
458+
exceptions: self.exceptions || exceptions,
459+
relaxed_simd: self.relaxed_simd || relaxed_simd,
460+
extended_const: self.extended_const || extended_const,
461+
};
408462
}
409463
}
410464

@@ -439,6 +493,14 @@ mod test_features {
439493
);
440494
}
441495

496+
#[test]
497+
fn features_extend() {
498+
let all = Features::all();
499+
let mut target = Features::none();
500+
target.extend(&all);
501+
assert_eq!(target, all);
502+
}
503+
442504
#[test]
443505
fn enable_threads() {
444506
let mut features = Features::new();

0 commit comments

Comments
 (0)