Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ where
Some(p) if p < N => {
let n = p.div_ceil(2);
encoder.put_bytes(bytes.into_iter().take(n));
&encoder.as_str()[..p]
encoder.as_str().get(..p).unwrap_or(encoder.as_str())
}
_ => {
encoder.put_bytes(bytes);
Expand Down Expand Up @@ -688,6 +688,21 @@ mod tests {
assert_eq!(format!("{:.65}", dummy), "2a".repeat(32));
}

#[test]
fn fmt_hex_max_short_bytes_with_precision() {
// `fmt_hex_max!` documents that `$bytes.len() <= $len` is acceptable. Ensure that
// if you have $bytes.len() strictly less than $len, and you attempt to use a
// precision value between the two lengths, nothing bad happens.
struct Short([u8; 2]);
impl fmt::Display for Short {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_hex_max!(f, 4, &self.0, Case::Lower)
}
}
let s = format!("{:.5}", Short([0x12, 0x34]));
assert_eq!(s, "1234");
}

struct TestHexUpperLower<'a>(&'a [u8], bool);

impl fmt::Display for TestHexUpperLower<'_> {
Expand Down