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 › math › big › int › Const › toInt Public Function toInt Doc Source Public Function std.math.big.int.Const.toInt
pub fn toInt (self : Const , comptime Int : type ) ConvertError !Int Params File math/big/int.zig: 2337
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377 pub fn toInt (self : Const , comptime Int : type ) ConvertError !Int {
switch (@typeInfo (Int )) {
.int => |info | {
// Make sure -0 is handled correctly.
if (self .eqlZero ()) return 0 ;
const Unsigned = @Int (.unsigned , info .bits );
if (!self .fitsInTwosComp (info .signedness , info .bits )) {
return error .TargetTooSmall ;
}
var r : Unsigned = 0 ;
if (@sizeOf (Unsigned ) <= @sizeOf (Limb )) {
r = @intCast (self .limbs [0 ]);
} else {
for (self .limbs [0 ..self .limbs .len ], 0 ..) |_ , ri | {
const limb = self .limbs [self .limbs .len - ri - 1 ];
r <<= limb_bits ;
r |= limb ;
}
}
if (info .signedness == .unsigned ) {
return if (self .positive ) @intCast (r ) else error .NegativeIntoUnsigned ;
} else {
if (self .positive ) {
return @intCast (r );
} else {
if (math .cast (Int , r )) |ok | {
return -ok ;
} else {
return minInt (Int );
}
}
}
},
else => @compileError ("expected int type, found '" ++ @typeName (Int ) ++ "'" ),
}
}
Convert
selftoInt.Returns an error if self cannot be narrowed into the requested type without truncation.