Skip to content

Commit 39760d5

Browse files
committed
Update provider API docs
1 parent 31010ca commit 39760d5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

library/core/src/error.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,51 @@ pub trait Error: Debug + Display {
205205
/// assert!(request_ref::<MyLittleTeaPot>(dyn_error).is_none());
206206
/// }
207207
/// ```
208+
///
209+
/// # Implementation Conventions
210+
///
211+
/// <div class="warning">
212+
///
213+
/// **We recommend implementors avoid delegating implementations of `provide` to source error
214+
/// implementations.**
215+
///
216+
/// This method should expose context from the current piece of the source chain only, not from
217+
/// sources. Delegating `provide` implementations cause the same context to be provided by
218+
/// multiple errors in the chain of sources which can cause unintended duplication of
219+
/// information in error reports or require heuristics to deduplicate.
220+
///
221+
/// In otherwords, the following implementation pattern for `provide` is discouraged and should
222+
/// not be used for Error types exposed in public APIs to third parties.
223+
///
224+
/// </div>
225+
///
226+
/// ```rust
227+
/// # #![feature(error_generic_member_access)]
228+
/// # use core::fmt;
229+
/// # use core::error::{request_ref, Request};
230+
/// struct MyError {
231+
/// source: Error,
232+
/// }
233+
/// # #[derive(Debug)]
234+
/// # struct Error;
235+
/// # impl fmt::Display for Error {
236+
/// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237+
/// # write!(f, "Example Source Error")
238+
/// # }
239+
/// # }
240+
/// # impl fmt::Display for MyError {
241+
/// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
242+
/// # write!(f, "Example Error")
243+
/// # }
244+
/// # }
245+
/// # impl std::error::Error for Error { }
246+
///
247+
/// impl std::error::Error for MyError {
248+
/// fn provide<'a>(&'a self, request: &mut Request<'a>) {
249+
/// self.source.provide(request)
250+
/// }
251+
/// }
252+
/// ```
208253
#[unstable(feature = "error_generic_member_access", issue = "99301")]
209254
#[allow(unused_variables)]
210255
fn provide<'a>(&'a self, request: &mut Request<'a>) {}

0 commit comments

Comments
 (0)