Checked conversion v0.1.0

Source
Converting between primitive types with a report of whether the value survived, with wrapping on purpose, or clamped to the destination's range.

IsSignedType<T>

pub func IsSignedType<T>(sample: T) -> bool

Whether the integer type of sample has values below zero. sample is read by value and only its type matters; its value is never used. Returns the answer by copy. A type's own all-ones pattern is what answers this: read as a signed type it is -1, and read as an unsigned one it is the maximum. Nothing has to be told which it has. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
Tthe integer or floating-point type, deduced from the arguments

Parameters

NameTypeDescription
sampleTany value of the type, read only to deduce it

Returns: whether the type can hold a negative value

ConvertChecked<From, To>

pub func ConvertChecked<From, To>(
    value: From,
    result: *var To
) -> bool

Converts a primitive value to another primitive type and reports whether the true value was representable. value is read by value. result receives the conversion the machine produced, which is the true value when this returns false and the wrapped, truncated or rounded one when it returns true; it is written in both cases, so a caller who wants the lossy answer does not have to convert again. Returns true when the conversion lost the value: a magnitude too large for the destination, a negative value converted to an unsigned type, a value above the signed range converted to a signed type of the same width, a float with a fractional part converted to an integer, or a float narrowed past its precision. A NaN and an infinity are both reported, since neither survives a round trip. Runs in constant time and allocates nothing. Never traps, so the caller decides what a lost value means. result must be non-null. This is not checked.

Source

Type parameters

NameDescription
Fromthe source type
Tothe destination type

Parameters

NameTypeDescription
valueFromthe value to act on
result*var Toreceives the conversion the machine produced, exact or lossy; never null

Returns: true when the value does not fit the destination; result then holds the lossy conversion

ConvertWrapping<From, To>

pub func ConvertWrapping<From, To>(
    value: From,
    result: *var To
)

Converts a primitive value to another primitive type, keeping whatever fits. value is read by value; result receives the destination type's own answer, wrapping an integer around its range, truncating a float toward zero, and rounding a narrowed float. This is what as does, named so that discarding the rest on purpose reads differently from discarding it by accident. Cannot fail: every value has a wrapped conversion. Runs in constant time and allocates nothing. result must be non-null. This is not checked.

Source

Type parameters

NameDescription
Fromthe source type
Tothe destination type

Parameters

NameTypeDescription
valueFromthe value to act on
result*var Toreceives the wrapped, truncated or rounded conversion; never null

ConvertSaturating<From, To>

pub func ConvertSaturating<From, To>(
    value: From,
    result: *var To
)

Converts a primitive value to an integer type, clamping to that type's range instead of wrapping. value is read by value; result receives the nearest representable value: the destination's maximum for a value too large, its minimum for one too small, and the exact conversion for one that fits. A NaN has no nearest value and yields zero. To must be an integer type. A floating destination has no limits to clamp against and is not supported; use ConvertChecked and decide what to do with the report. Cannot fail: the answer is always the nearest representable value. Runs in constant time and allocates nothing. result must be non-null. This is not checked.

Source

Type parameters

NameDescription
Fromthe source type
Tothe destination type

Parameters

NameTypeDescription
valueFromthe value to act on
result*var Toreceives the nearest representable value; never null