Function Subspace :: sus :: construct :: cast

template <class To, class From>
auto cast(const From& from) -> To
sus::construct::Cast<To, From>

An infallible conversion (cast) that may lose the original value in the process.

See the namespace level documentation for more about converting types.

If the input can not be represented in the output, some other value will be produced, which may lead to application bugs and memory unsafety if used incorrectly. This behaves like static_cast<To>() but does not cause Undefined Behaviour for any input and output type. without Undefined Behaviour.

The cast operation is supported for types To and From that satisfy Cast<To, From>.

It is best practice to place a // SAFETY: comment on use of sus::cast in order to explain why the code intends to change the value during the cast.


This converts -1 as an i64 into a u32, which both changes its meaning, becoming a large positive number, and truncates the high 32 bits losing the original bits.

// SAFETY: We're intending to convert negative numbers into large positive
// values for this example.
sus_check(u32::MAX == sus::cast<u32>(-1_i64));