-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create generic functions to format collections
- Loading branch information
Showing
7 changed files
with
270 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
use core::fmt::{self, Formatter}; | ||
use core::iter::{Iterator, IteratorExt}; | ||
use core::result::Result; | ||
|
||
pub fn seq_fmt_debug<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::Debug | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:?}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_display<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::Display | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_octal<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::Octal | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:o}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_binary<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::Binary | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:b}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_upper_hex<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::UpperHex | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:X}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_lower_hex<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::LowerHex | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:x}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_upper_exp<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::UpperExp | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:E}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn seq_fmt_lower_exp<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
where I::Item:fmt::LowerExp | ||
{ | ||
for (i, e) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:e}", e)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_debug<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::Debug, | ||
V:fmt::Debug | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:?}: {:?}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_display<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::Display, | ||
V:fmt::Display | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{}: {}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
pub fn map_fmt_octal<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::Octal, | ||
V:fmt::Octal | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:o}: {:o}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_binary<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::Binary, | ||
V:fmt::Binary | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:b}: {:b}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_upper_hex<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::UpperHex, | ||
V:fmt::UpperHex | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:X}: {:X}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_lower_hex<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::LowerHex, | ||
V:fmt::LowerHex | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:x}: {:x}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_upper_exp<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::UpperExp, | ||
V:fmt::UpperExp | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:E}: {:E}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} | ||
|
||
pub fn map_fmt_lower_exp<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result | ||
where K:fmt::LowerExp, | ||
V:fmt::LowerExp | ||
{ | ||
for (i, (k, v)) in s.enumerate() { | ||
if i != 0 { try!(write!(f, ", ")); } | ||
try!(write!(f, "{:e}: {:e}", k, v)); | ||
} | ||
Result::Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.