Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement fmt traits for collections #25319

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use Bound::{self, Included, Excluded, Unbounded};

use borrow::Borrow;
use vec_deque::VecDeque;
use format_helpers::*;

use self::Continuation::{Continue, Finished};
use self::StackOp::*;
Expand Down Expand Up @@ -930,6 +931,17 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
}
}

impl_map_fmt! {
BTreeMap,
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
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap<K, V>
where K: Borrow<Q>, Q: Ord
Expand Down
13 changes: 13 additions & 0 deletions src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use borrow::Borrow;
use btree_map::{BTreeMap, Keys};
use Bound;

use format_helpers::*;

// FIXME(conventions): implement bounded iterators

/// A set based on a B-Tree.
Expand Down Expand Up @@ -626,6 +628,17 @@ impl<T: Debug> Debug for BTreeSet<T> {
}
}

impl_set_fmt! {
BTreeSet,
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
}

impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> { Iter { iter: self.iter.clone() } }
}
Expand Down
257 changes: 257 additions & 0 deletions src/libcollections/format_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use core::fmt::{self, Formatter};
use core::iter::{Iterator};
use core::result::Result;

pub fn seq_fmt_debug<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result
where I::Item:fmt::Debug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a space after colon

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is pretty much everywhere in this PR)

{
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_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(())
}

pub fn vec_map_fmt_debug<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any obvious reason why vec_map is different from a normal Map?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you want to treat the keys special... I'm not sure that's the right call. In general I'd expect BTreeMap<usize, V> to be interchangeable with VecMap<V>. Any particular reason?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vec_map currently implements Debug this way, ie. write!(f, "{}: {:?}", k, v)
I wasn't sure if that was some convention I wasn't aware of so I didn't change the behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just change the behaviour. Debug and Display are identical for primitives as far as I know, anyway.

where K:fmt::Display,
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 vec_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 vec_map_fmt_octal<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result
where K:fmt::Display,
V:fmt::Octal
{
for (i, (k, v)) in s.enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {:o}", k, v));
}

Result::Ok(())
}

pub fn vec_map_fmt_binary<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result
where K:fmt::Display,
V:fmt::Binary
{
for (i, (k, v)) in s.enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {:b}", k, v));
}

Result::Ok(())
}

pub fn vec_map_fmt_upper_hex<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result
where K:fmt::Display,
V:fmt::UpperHex
{
for (i, (k, v)) in s.enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {:X}", k, v));
}

Result::Ok(())
}

pub fn vec_map_fmt_lower_hex<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result
where K:fmt::Display,
V:fmt::LowerHex
{
for (i, (k, v)) in s.enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {:x}", 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 @@ -81,6 +81,7 @@ mod btree;
pub mod borrow;
pub mod enum_set;
pub mod fmt;
mod format_helpers;
pub mod linked_list;
pub mod range;
pub mod slice;
Expand Down
12 changes: 12 additions & 0 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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 LinkedList<T> {
Expand Down Expand Up @@ -921,6 +922,17 @@ impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
}
}

impl_seq_fmt! {
LinkedList,
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
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Hash> Hash for LinkedList<A> {
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down
42 changes: 42 additions & 0 deletions src/libcollections/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,45 @@ macro_rules! vec {
macro_rules! format {
($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
}

macro_rules! impl_seq_fmt {
($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_set_fmt {
($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, $($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, "{{"));
try!($fmt_fun(self.iter(), f));
write!(f, "}}")
}
}
)+
};
}
Loading