From aec0b51d9cca576b38023c4f8a11f28452ea8d55 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Tue, 15 May 2012 14:51:33 -0700 Subject: [PATCH] Added a few more extension methods on vectors, and fixed a pretty printer bug. --- src/libcore/vec.rs | 16 ++++++++++++++++ src/librustsyntax/print/pprust.rs | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index d083bcae679..3c6a3d9f7b2 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -580,6 +580,16 @@ fn all(v: [T], f: fn(T) -> bool) -> bool { ret true; } +#[doc = " +Return true if a predicate matches all elements + +If the vector contains no elements then true is returned. +"] +fn alli(v: [T], f: fn(uint, T) -> bool) -> bool { + for eachi(v) {|i, elem| if !f(i, elem) { ret false; } } + ret true; +} + #[doc = " Return true if a predicate matches all elements in both vectors. @@ -1107,6 +1117,12 @@ impl extensions for [T] { let mut i = 0u; self.map { |e| i += 1u; f(i - 1u, e) } } + #[doc = "Returns true if the function returns true for all elements. + + If the vector is empty, true is returned."] + fn alli(f: fn(uint, T) -> bool) -> bool { + alli(self, f) + } #[doc = " Apply a function to each element of a vector and return a concatenation of each result vector diff --git a/src/librustsyntax/print/pprust.rs b/src/librustsyntax/print/pprust.rs index 70e3ec33c62..cc541cc4f4d 100644 --- a/src/librustsyntax/print/pprust.rs +++ b/src/librustsyntax/print/pprust.rs @@ -337,6 +337,10 @@ fn print_region(s: ps, region: @ast::region) { } fn print_type(s: ps, &&ty: @ast::ty) { + print_type_ex(s, ty, false); +} + +fn print_type_ex(s: ps, &&ty: @ast::ty, print_colons: bool) { maybe_print_comment(s, ty.span.lo); ibox(s, 0u); alt ty.node { @@ -384,7 +388,7 @@ fn print_type(s: ps, &&ty: @ast::ty) { ast::ty_fn(proto, d) { print_ty_fn(s, some(proto), d, none, none); } - ast::ty_path(path, _) { print_path(s, path, false); } + ast::ty_path(path, _) { print_path(s, path, print_colons); } ast::ty_constr(t, cs) { print_type(s, t); space(s.s); @@ -961,7 +965,7 @@ fn print_expr(s: ps, &&expr: @ast::expr) { print_op_maybe_parens(s, expr, parse::prec::as_prec); space(s.s); word_space(s, "as"); - print_type(s, ty); + print_type_ex(s, ty, true); } ast::expr_if(test, blk, elseopt) { print_if(s, test, blk, elseopt, false);