Skip to content

Commit db79978

Browse files
rohlemsqueek502
authored andcommitted
add asserting variants to std.error_set
1 parent 8f39baf commit db79978

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/std/error_set.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test contains {
4141
/// members of BaseErrorSet, but not members of ToExcludeErrorSet.
4242
/// ToExcludeErrorSet does not have to
4343
/// be contained in, or even intersect BaseErrorSet.
44+
/// See also `ExcludingAssertAllContained`.
4445
pub fn Excluding(comptime BaseErrorSet: type, comptime ToExcludeErrorSet: type) type {
4546
if (BaseErrorSet == ToExcludeErrorSet) return error{}; //allows excluding anyerror from anyerror
4647

@@ -72,6 +73,7 @@ test Excluding {
7273
/// Returns an error set with all members of BaseErrorSet
7374
/// except for error_to_exclude.
7475
/// error_to_exclude does not have to be a member of BaseErrorSet.
76+
/// See also `WithoutAssertContained`.
7577
pub fn Without(comptime BaseErrorSet: type, comptime error_to_exclude: anyerror) type {
7678
const ErrorSetToExclude = @TypeOf(@field(anyerror, @errorName(error_to_exclude)));
7779
return comptime Excluding(BaseErrorSet, ErrorSetToExclude);
@@ -89,6 +91,7 @@ test Without {
8991
/// Returns an error set with all errors which are
9092
/// members of both ErrorSetA and ErrorSetB.
9193
/// The resulting error set may be empty.
94+
/// See also `IntersectAssertNonEmpty`.
9295
pub fn Intersect(comptime ErrorSetA: type, comptime ErrorSetB: type) type {
9396
if (ErrorSetA == anyerror) return ErrorSetB; //allows intersecting with anyerror
9497

@@ -116,3 +119,29 @@ test Intersect {
116119
assert(Intersect(error{}, error{}) == error{});
117120
}
118121
}
122+
123+
/// Returns the set of errors that are
124+
/// members of BaseErrorSet, but not members of ToExcludeErrorSet.
125+
/// Asserts that all members of ToExcludeErrorSet are members of BaseErrorSet.
126+
/// See also `Excluding`.
127+
pub fn ExcludingAssertAllContained(comptime BaseErrorSet: type, comptime ToExcludeErrorSet: type) type {
128+
comptime assert(containsAll(BaseErrorSet, ToExcludeErrorSet));
129+
return comptime Excluding(BaseErrorSet, ToExcludeErrorSet);
130+
}
131+
/// Returns an error set with all members of BaseErrorSet
132+
/// except for error_to_exclude.
133+
/// Asserts that error_to_exclude is a member of BaseErrorSet.
134+
/// See also `Without`.
135+
pub fn WithoutAssertContained(comptime BaseErrorSet: type, comptime error_to_exclude: anyerror) type {
136+
comptime assert(contains(BaseErrorSet, error_to_exclude));
137+
return comptime Without(BaseErrorSet, error_to_exclude);
138+
}
139+
/// Returns an error set with all errors which are
140+
/// members of both ErrorSetA and ErrorSetB.
141+
/// Asserts that the resulting error set is not empty.
142+
/// See also `Intersect`.
143+
pub fn IntersectAssertNonEmpty(comptime ErrorSetA: type, comptime ErrorSetB: type) type {
144+
const Result = Intersect(ErrorSetA, ErrorSetB);
145+
comptime assert(Result != error{});
146+
return Result;
147+
}

0 commit comments

Comments
 (0)