Skip to content

implement fmt traits for collections #25319

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
11 changes: 11 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,16 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
}
}

impl_map_fmt! {
BTreeMap,
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
12 changes: 12 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,16 @@ impl<T: Debug> Debug for BTreeSet<T> {
}
}

impl_set_fmt! {
BTreeSet,
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
175 changes: 175 additions & 0 deletions src/libcollections/format_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// 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
{
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_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 @@ -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
11 changes: 11 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,16 @@ impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
}
}

impl_seq_fmt! {
LinkedList,
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, "}}")
}
}
)+
};
}
12 changes: 12 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ use super::range::RangeArgument;
// FIXME- fix places which assume the max vector allowed has memory usize::MAX.
static MAX_MEMORY_SIZE: usize = isize::MAX as usize;

use format_helpers::*;

/// A growable list type, written `Vec<T>` but pronounced 'vector.'
///
/// # Examples
Expand Down Expand Up @@ -1653,6 +1655,16 @@ impl<T: fmt::Debug> fmt::Debug for Vec<T> {
}
}

impl_seq_fmt! {
Vec,
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<T> AsRef<Vec<T>> for Vec<T> {
fn as_ref(&self) -> &Vec<T> {
Expand Down
23 changes: 11 additions & 12 deletions src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use core::hash::{Hash, Hasher};
use core::cmp;

use alloc::heap;
use format_helpers::*;


const INITIAL_CAPACITY: usize = 7; // 2^3 - 1
const MINIMUM_CAPACITY: usize = 1; // 2 - 1
Expand Down Expand Up @@ -1791,18 +1793,15 @@ impl<A> Extend<A> for VecDeque<A> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug> fmt::Debug for VecDeque<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "["));

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

write!(f, "]")
}
impl_seq_fmt! {
VecDeque,
Debug => seq_fmt_debug,
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
}

#[cfg(test)]
Expand Down
Loading