Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › crypto › codecs › asn1 › Oid › StaticMap › initComptime Public Type Function initComptime Doc Source Public Type Function std.crypto.codecs.asn1.Oid.StaticMap.initComptime
pub fn initComptime (comptime key_pairs : anytype ) ReturnType File crypto/codecs/asn1/Oid.zig: 177
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 pub fn initComptime (comptime key_pairs : anytype ) ReturnType {
const struct_info = @typeInfo (@TypeOf (key_pairs )).@"struct" ;
const error_msg = "Each field of '" ++ @typeName (Enum ) ++ "' must map to exactly one OID" ;
if (enum_info .mode == .nonexhaustive or enum_info .field_names .len != struct_info .field_names .len ) {
@compileError (error_msg );
}
comptime var enum_to_oid = EnumToOid .initUndefined ();
const KeyPair = struct { []const u8 , Enum };
comptime var static_key_pairs : [enum_info .field_names .len ]KeyPair = undefined ;
comptime for (enum_info .field_names , enum_info .field_values , 0 ..) |f_name , f_value , i | {
if (!@hasField (@TypeOf (key_pairs ), f_name )) {
@compileError ("Field '" ++ f_name ++ "' missing Oid.StaticMap entry" );
}
const encoded = &encodeComptime (@field (key_pairs , f_name ));
const tag : Enum = @fromBackingInt (@intCast (f_value ));
static_key_pairs [i ] = .{ encoded , tag };
enum_to_oid .set (tag , encoded );
};
const oid_to_enum = std .StaticStringMap (Enum ).initComptime (static_key_pairs );
if (oid_to_enum .values ().len != enum_info .field_names .len ) @compileError (error_msg );
return ReturnType { .oid_to_enum = oid_to_enum , .enum_to_oid = enum_to_oid };
}