2018-10-02 07:24:51 -07:00
|
|
|
//! Utility functions
|
|
|
|
//!
|
|
|
|
//! Functions not large enough to warrant their own crate or module, but flexible enough to be used
|
|
|
|
//! in multiple disjunct places in the library. May also contain backports, workarounds.
|
|
|
|
|
2018-10-24 04:01:52 -07:00
|
|
|
use std::fmt;
|
|
|
|
|
2018-11-03 04:31:36 -07:00
|
|
|
use hex_fmt::HexFmt;
|
|
|
|
|
|
|
|
/// Prints a byte slice as shortened hexadecimal in debug output.
|
2019-01-09 02:56:40 -08:00
|
|
|
pub fn fmt_hex<T: AsRef<[u8]>>(bytes: T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-11-03 04:31:36 -07:00
|
|
|
write!(f, "{:10}", HexFmt(bytes))
|
|
|
|
}
|
2018-11-22 04:00:02 -08:00
|
|
|
|
|
|
|
/// Given a number of nodes, returns the maximum number of faulty nodes that can be tolerated: the
|
|
|
|
/// greatest number less than one third of `n`.
|
2018-12-12 04:39:52 -08:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `n == 0`.
|
2018-11-22 04:00:02 -08:00
|
|
|
#[inline]
|
2018-12-12 04:39:52 -08:00
|
|
|
pub fn max_faulty(n: usize) -> usize {
|
|
|
|
assert!(n > 0, "A valid network requires at least one node.");
|
2018-11-22 04:00:02 -08:00
|
|
|
(n - 1) / 3
|
|
|
|
}
|