@@ -72,14 +72,24 @@ pub type Result = result::Result<(), Error>;
72
72
/// The error type which is returned from formatting a message into a stream.
73
73
///
74
74
/// This type does not support transmission of an error other than that an error
75
- /// occurred. Any extra information must be arranged to be transmitted through
76
- /// some other means.
77
- ///
78
- /// An important thing to remember is that the type `fmt::Error` should not be
75
+ /// occurred. This is because, despite the existence of this error,
76
+ /// string formatting is considered an infallible operation.
77
+ /// `fmt()` implementors should not return this `Error` unless they received it from their
78
+ /// [`Formatter`]. The only time your code should create a new instance of this
79
+ /// error is when implementing `fmt::Write`, in order to cancel the formatting operation when
80
+ /// writing to the underlying stream fails.
81
+ ///
82
+ /// Any extra information must be arranged to be transmitted through some other means,
83
+ /// such as storing it in a field to be consulted after the formatting operation has been
84
+ /// cancelled. (For example, this is how [`std::io::Write::write_fmt()`] propagates IO errors
85
+ /// during writing.)
86
+ ///
87
+ /// This type, `fmt::Error`, should not be
79
88
/// confused with [`std::io::Error`] or [`std::error::Error`], which you may also
80
89
/// have in scope.
81
90
///
82
91
/// [`std::io::Error`]: ../../std/io/struct.Error.html
92
+ /// [`std::io::Write::write_fmt()`]: ../../std/io/trait.Write.html#method.write_fmt
83
93
/// [`std::error::Error`]: ../../std/error/trait.Error.html
84
94
///
85
95
/// # Examples
@@ -118,8 +128,10 @@ pub trait Write {
118
128
/// This function will return an instance of [`std::fmt::Error`][Error] on error.
119
129
///
120
130
/// The purpose of that error is to abort the formatting operation when the underlying
121
- /// destination encounters some error preventing it from accepting more text; it should
122
- /// generally be propagated rather than handled, at least when implementing formatting traits.
131
+ /// destination encounters some error preventing it from accepting more text;
132
+ /// in particular, it does not communicate any information about *what* error occurred.
133
+ /// It should generally be propagated rather than handled, at least when implementing
134
+ /// formatting traits.
123
135
///
124
136
/// # Examples
125
137
///
@@ -586,7 +598,7 @@ impl Display for Arguments<'_> {
586
598
#[ rustc_diagnostic_item = "Debug" ]
587
599
#[ rustc_trivial_field_reads]
588
600
pub trait Debug {
589
- /// Formats the value using the given formatter.
601
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
590
602
///
591
603
/// # Examples
592
604
///
@@ -703,7 +715,7 @@ pub use macros::Debug;
703
715
#[ rustc_diagnostic_item = "Display" ]
704
716
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
705
717
pub trait Display {
706
- /// Formats the value using the given formatter.
718
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
707
719
///
708
720
/// # Examples
709
721
///
@@ -777,7 +789,7 @@ pub trait Display {
777
789
/// ```
778
790
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
779
791
pub trait Octal {
780
- /// Formats the value using the given formatter.
792
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
781
793
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
782
794
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
783
795
}
@@ -836,7 +848,7 @@ pub trait Octal {
836
848
/// ```
837
849
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
838
850
pub trait Binary {
839
- /// Formats the value using the given formatter.
851
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
840
852
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
841
853
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
842
854
}
@@ -891,7 +903,7 @@ pub trait Binary {
891
903
/// ```
892
904
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
893
905
pub trait LowerHex {
894
- /// Formats the value using the given formatter.
906
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
895
907
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
896
908
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
897
909
}
@@ -946,7 +958,7 @@ pub trait LowerHex {
946
958
/// ```
947
959
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
948
960
pub trait UpperHex {
949
- /// Formats the value using the given formatter.
961
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
950
962
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
951
963
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
952
964
}
@@ -997,7 +1009,7 @@ pub trait UpperHex {
997
1009
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
998
1010
#[ rustc_diagnostic_item = "Pointer" ]
999
1011
pub trait Pointer {
1000
- /// Formats the value using the given formatter.
1012
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
1001
1013
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1002
1014
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
1003
1015
}
@@ -1048,7 +1060,7 @@ pub trait Pointer {
1048
1060
/// ```
1049
1061
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1050
1062
pub trait LowerExp {
1051
- /// Formats the value using the given formatter.
1063
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
1052
1064
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1053
1065
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
1054
1066
}
@@ -1099,7 +1111,7 @@ pub trait LowerExp {
1099
1111
/// ```
1100
1112
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1101
1113
pub trait UpperExp {
1102
- /// Formats the value using the given formatter.
1114
+ # [ doc = include_str ! ( "fmt_trait_method_doc.md" ) ]
1103
1115
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1104
1116
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
1105
1117
}
0 commit comments