Skip to content

Commit

Permalink
create generic functions to format collections
Browse files Browse the repository at this point in the history
  • Loading branch information
skullzzz committed Feb 6, 2015
1 parent 90c9176 commit af1f2aa
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 73 deletions.
33 changes: 11 additions & 22 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use core::{iter, fmt, mem};
use Bound::{self, Included, Excluded, Unbounded};

use ring_buf::RingBuf;
use format_helpers::*;

use self::Continuation::{Continue, Finished};
use self::StackOp::*;
Expand Down Expand Up @@ -873,30 +874,18 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
}
}

macro_rules! fmt_btree_map {
($($Trait:ident),*) => {
$(
impl<K: fmt::$Trait, V: fmt::$Trait> fmt::$Trait for BTreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "BTreeMap {{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(fmt::$Trait::fmt(k, f));
try!(write!(f, ": "));
try!(fmt::$Trait::fmt(v, f));
}

write!(f, "}}")
}
}
)*
}
impl_map_fmt! {
BTreeMap, "BTreeMap",
Debug => map_fmt_debug,
Display => map_fmt_display,
Octal => map_fmt_octal,
Binary => map_fmt_binary,
LowerHex => map_fmt_lower_hex,
UpperHex => map_fmt_upper_hex,
LowerExp => map_fmt_lower_exp,
UpperExp => map_fmt_upper_exp
}


fmt_btree_map! { Debug, Display, Octal, Binary, UpperHex, LowerHex, UpperExp, LowerExp }

#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
where Q: BorrowFrom<K> + Ord
Expand Down
31 changes: 12 additions & 19 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use core::iter::{self, FromIterator};
use core::mem;
use core::ptr;

use format_helpers::*;

/// A doubly-linked list.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct DList<T> {
Expand Down Expand Up @@ -875,27 +877,18 @@ impl<A: Clone> Clone for DList<A> {
}
}

macro_rules! fmt_dlist {
($($Trait:ident),*) => {
$(
impl<A: fmt::$Trait> fmt::$Trait for DList<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "DList ["));

for (i, e) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(fmt::$Trait::fmt(e, f));
}

write!(f, "]")
}
}
)*
}
impl_seq_fmt! {
DList, "DList",
Debug => seq_fmt_debug,
Display => seq_fmt_display,
Octal => seq_fmt_octal,
Binary => seq_fmt_binary,
LowerHex => seq_fmt_lower_hex,
UpperHex => seq_fmt_upper_hex,
LowerExp => seq_fmt_lower_exp,
UpperExp => seq_fmt_upper_exp
}

fmt_dlist! { Debug, Display, Octal, Binary, UpperHex, LowerHex, UpperExp, LowerExp }

#[stable(feature = "rust1", since = "1.0.0")]
impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for DList<A> {
fn hash(&self, state: &mut S) {
Expand Down
170 changes: 170 additions & 0 deletions src/libcollections/format_helpers.rs
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(())
}
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod bit;
mod btree;
pub mod dlist;
pub mod enum_set;
mod format_helpers;
pub mod ring_buf;
pub mod slice;
pub mod str;
Expand Down
55 changes: 55 additions & 0 deletions src/libcollections/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,58 @@ macro_rules! vec {
);
($($x:expr,)*) => (vec![$($x),*])
}

macro_rules! impl_seq_fmt {
($seq:ident, $annotation:expr, $($Trait:ident => $fmt_fun:ident),+) => {
$(
impl<T: fmt::$Trait> fmt::$Trait for $seq <T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{} ", $annotation));
try!(write!(f, "["));
try!($fmt_fun(self.iter(), f));
write!(f, "]")
}
}
)+
};

($seq:ident, $($Trait:ident => $fmt_fun:ident),+) => {
$(
impl<T: fmt::$Trait> fmt::$Trait for $seq<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "["));
try!($fmt_fun(self.iter(), f));
write!(f, "]")
}
}
)+
}
}

macro_rules! impl_map_fmt {
($map:ident, $annotation:expr, $($Trait:ident => $fmt_fun:ident),+) => {
$(
impl<K: fmt::$Trait, V: fmt::$Trait> fmt::$Trait for $map<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{} ", $annotation));
try!(write!(f, "{{"));
try!($fmt_fun(self.iter(), f));
write!(f, "}}")
}
}
)+
};

(Fixed $map:ident, $annotation:expr, $($Trait:ident => $fmt_fun:ident),+) => {
$(
impl<T: fmt::$Trait> fmt::$Trait for $map <T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{} ", $annotation));
try!(write!(f, "{{"));
try!($fmt_fun(self.iter(), f));
write!(f, "}}")
}
}
)+
}
}
24 changes: 12 additions & 12 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ use core::ptr;
use core::raw::Slice as RawSlice;
use core::uint;

use format_helpers::*;

/// A growable list type, written `Vec<T>` but pronounced 'vector.'
///
/// # Examples
Expand Down Expand Up @@ -1539,20 +1541,18 @@ impl<T> Default for Vec<T> {
}
}

macro_rules! fmt_vec {
($($Trait:ident),*) => {
$(
impl<T: fmt::$Trait> fmt::$Trait for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::$Trait::fmt(self.as_slice(), f)
}
}
)*
}
impl_seq_fmt! {
Vec,
Debug => seq_fmt_debug,
Display => seq_fmt_display,
Octal => seq_fmt_octal,
Binary => seq_fmt_binary,
LowerHex => seq_fmt_lower_hex,
UpperHex => seq_fmt_upper_hex,
LowerExp => seq_fmt_lower_exp,
UpperExp => seq_fmt_upper_exp
}

fmt_vec! { Debug, Display, Octal, Binary, UpperHex, LowerHex, UpperExp, LowerExp }

impl<'a> fmt::Writer for Vec<u8> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.push_all(s.as_bytes());
Expand Down
Loading

0 comments on commit af1f2aa

Please sign in to comment.