pub(crate) trait ForceInto { fn force_into<U>(self) -> U where Self: TryInto<U>;
}
impl<T> ForceInto for T { fn force_into<U>(self) -> U where Self: TryInto<U>,
{
<Selfas TryInto<U>>::try_into(self)
.ok()
.unwrap_or_else(die)
}
}
// Deterministically abort on arithmetic overflow, instead of wrapping and // continuing with invalid behavior. // // This is impossible or nearly impossible to hit as the arithmetic computations // in libyaml are all related to either: // // - small integer processing (ascii, hex digits) // - allocation sizing // // and the only allocations in libyaml are for fixed-sized objects and // geometrically growing buffers with a growth factor of 2. So in order for an // allocation computation to overflow usize, the previous allocation for that // container must have been filled to a size of usize::MAX/2, which is an // allocation that would have failed in the allocator. But we check for this to // be pedantic and to find out if it ever does happen. // // No-std abort is implemented using a double panic. On most platforms the // current mechanism for this is for core::intrinsics::abort to invoke an // invalid instruction. On Unix, the process will probably terminate with a // signal like SIGABRT, SIGILL, SIGTRAP, SIGSEGV or SIGBUS. The precise // behaviour is not guaranteed and not stable, but is safe. #[cold] pub(crate) fn die<T>() -> T { struct PanicAgain;
impl Drop for PanicAgain { fn drop(&mutself) {
panic!("arithmetic overflow");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.