Function Subspace :: sus :: construct :: try_into

template <class ToType, class FromType>
auto try_into(FromType&& from) -> auto
requires
sus::construct::TryInto<FromType, ToType>

Attempts to convert from the given value to a ToType.

Unlike into(), this function can not use type deduction to determine the receiving type as it needs to determine the Result type and allow the caller the chance to handle the error condition.

The TryFrom concept requires a try_from() method that returns a Result. That Result will be the return type of this function.

Examples

auto valid = sus::try_into<u8>(123_i32).unwrap_or_default();
sus_check(valid == 123);
auto invalid = sus::try_into<u8>(-1_i32).unwrap_or_default();
sus_check(invalid == 0);