@@ -163,9 +163,10 @@ method of the signature:
163
163
164
164
```rust
165
165
# use std;
166
+ # mod fmt { pub type Result = (); }
166
167
# struct T;
167
168
# trait SomeName<T> {
168
- fn fmt(value: &T, f: &mut std::fmt::Formatter);
169
+ fn fmt(value: &T, f: &mut std::fmt::Formatter) -> fmt::Result ;
169
170
# }
170
171
```
171
172
@@ -174,7 +175,14 @@ emit output into the `f.buf` stream. It is up to each format trait
174
175
implementation to correctly adhere to the requested formatting parameters. The
175
176
values of these parameters will be listed in the fields of the `Formatter`
176
177
struct. In order to help with this, the `Formatter` struct also provides some
177
- helper methods. An example of implementing the formatting traits would look
178
+ helper methods.
179
+
180
+ Additionally, the return value of this function is `fmt::Result` which is a
181
+ typedef to `Result<(), IoError>` (also known as `IoError<()>`). Formatting
182
+ implementations should ensure that they return errors from `write!` correctly
183
+ (propagating errors upward).
184
+
185
+ An example of implementing the formatting traits would look
178
186
like:
179
187
180
188
```rust
@@ -187,7 +195,7 @@ struct Vector2D {
187
195
}
188
196
189
197
impl fmt::Show for Vector2D {
190
- fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) {
198
+ fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result {
191
199
// The `f.buf` value is of the type `&mut io::Writer`, which is what th
192
200
// write! macro is expecting. Note that this formatting ignores the
193
201
// various flags provided to format strings.
@@ -198,7 +206,7 @@ impl fmt::Show for Vector2D {
198
206
// Different traits allow different forms of output of a type. The meaning of
199
207
// this format is to print the magnitude of a vector.
200
208
impl fmt::Binary for Vector2D {
201
- fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) {
209
+ fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result {
202
210
let magnitude = (obj.x * obj.x + obj.y * obj.y) as f64;
203
211
let magnitude = magnitude.sqrt();
204
212
@@ -207,7 +215,7 @@ impl fmt::Binary for Vector2D {
207
215
// for details, and the function `pad` can be used to pad strings.
208
216
let decimals = f.precision.unwrap_or(3);
209
217
let string = f64::to_str_exact(magnitude, decimals);
210
- f.pad_integral(string.as_bytes(), "", true);
218
+ f.pad_integral(string.as_bytes(), "", true)
211
219
}
212
220
}
213
221
@@ -242,6 +250,7 @@ strings and instead directly write the output. Under the hood, this function is
242
250
actually invoking the `write` function defined in this module. Example usage is:
243
251
244
252
```rust
253
+ # #[allow(unused_must_use)];
245
254
use std::io;
246
255
247
256
let mut w = io::MemWriter::new();
@@ -655,11 +664,12 @@ uniform_fn_call_workaround! {
655
664
/// # Example
656
665
///
657
666
/// ```rust
667
+ /// # #[allow(unused_must_use)];
658
668
/// use std::fmt;
659
669
/// use std::io;
660
670
///
661
671
/// let w = &mut io::stdout() as &mut io::Writer;
662
- /// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world");
672
+ /// format_args!(|args| { fmt::write(w, args); }, "Hello, {}!", "world");
663
673
/// ```
664
674
pub fn write ( output : & mut io:: Writer , args : & Arguments ) -> Result {
665
675
unsafe { write_unsafe ( output, args. fmt , args. args ) }
0 commit comments