Skip to content

Commit 0463168

Browse files
Allow slices to be used in field
1 parent e0d11f3 commit 0463168

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/libcore/fmt/builders.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
103103
impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
104104
/// Adds a new field to the generated struct output.
105105
#[stable(feature = "debug_builders", since = "1.2.0")]
106-
pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
106+
pub fn field<V: fmt::Debug>(&mut self, name: &str, value: &V) -> &mut DebugStruct<'a, 'b>
107+
where V: ?Sized {
107108
self.result = self.result.and_then(|_| {
108109
let prefix = if self.has_fields {
109110
","
@@ -195,7 +196,8 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
195196
impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
196197
/// Adds a new field to the generated tuple struct output.
197198
#[stable(feature = "debug_builders", since = "1.2.0")]
198-
pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
199+
pub fn field<V: fmt::Debug>(&mut self, value: &V) -> &mut DebugTuple<'a, 'b>
200+
where V: ?Sized {
199201
self.result = self.result.and_then(|_| {
200202
let (prefix, space) = if self.fields > 0 {
201203
(",", " ")

src/test/run-pass/fmt-arrays.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt;
12+
13+
struct Foo {
14+
v: [i32; 33],
15+
}
16+
17+
struct Bar([i32; 33]);
18+
19+
impl fmt::Debug for Foo {
20+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
21+
fmt.debug_struct("Foo")
22+
.field("v", &self.v as &[i32])
23+
.finish()
24+
}
25+
}
26+
27+
impl fmt::Debug for Bar {
28+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
29+
fmt.debug_tuple("Foo")
30+
.field(&self.0 as &[i32])
31+
.finish()
32+
}
33+
}
34+
35+
fn main() {
36+
println!("{:?} {:?}",
37+
Foo { v: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] },
38+
Bar([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));
39+
}

0 commit comments

Comments
 (0)