Skip to main content

t

Macro t 

Source
macro_rules! t {
    ($ty: ident) => { ... };
    ($($_: tt)*) => { ... };
}
Expand description

Returns a concrete instantiation of the Integer type with the specified const-generic parameters from a type descriptor.

t! takes a type descriptor (which is an identifier fragment) and simply outputs Integer<S, N, B, OM>, where the values of the const-generic parameters S, N, B and OM are determined from the type descriptor.

A type descriptor has the following format: <sign><bit_width><overflow_mode>?:

  • <sign> is either I (specifying a signed integer) or U (specifying an unsigned integer).

  • <bit_width> is a decimal integer specifying the bit width of the integer type. The bit width must be at least 2 and at most 2^32 - 1.

  • <overflow_mode> is optional, and if specified must be one of:

    If <overflow_mode> is omitted, the default overflow mode (OverflowMode::DEFAULT) is used.

If the given type descriptor is not in this format, a compile error will be triggered when the type is used (when the type is unused, no compile-error will be triggered).

If you want to create Integer values rather than types, use the n! macro.

ยงExamples

use bnum::prelude::*;
 
type MyInt = t!(I259p); // signed 259-bit integer with panicking overflow mode
type MyUint = t!(U633); // unsigned 633-bit integer with default overflow mode
type MyInt2 = t!(I538s); // signed 538-bit integer with saturating overflow mode
type MyUint2 = t!(U24w); // unsigned 24-bit integer with wrapping overflow mode

The following examples will fail to compile, since the type descriptor is invalid. Note the type must be used in order to trigger the compile error.

โ“˜
use bnum::prelude::*;
 
type InvalidType = t!(A256); // invalid sign descriptor
 
dbg!(InvalidType::BITS);
โ“˜
use bnum::prelude::*;
 
type InvalidType2 = t!(I1); // bit width too small
 
dbg!(InvalidType2::BITS);
โ“˜
use bnum::prelude::*;
 
type InvalidType3 = t!(U1024x); // invalid overflow mode descriptor
 
dbg!(InvalidType3::BITS);