]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/fmt_wrap.rs
Auto merge of #39019 - nikomatsakis:issue-38919, r=eddyb
[rust.git] / src / librustc_data_structures / fmt_wrap.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fmt;
12
13 // Provide some more formatting options for some data types (at the moment
14 // that's just `{:x}` for slices of u8).
15
16 pub struct FmtWrap<T>(pub T);
17
18 impl<'a> fmt::LowerHex for FmtWrap<&'a [u8]> {
19     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
20         for byte in self.0.iter() {
21             try!(write!(formatter, "{:02x}", byte));
22         }
23         Ok(())
24     }
25 }
26
27 #[test]
28 fn test_lower_hex() {
29     let bytes: &[u8] = &[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
30     assert_eq!("0123456789abcdef", &format!("{:x}", FmtWrap(bytes)));
31 }