@@ -4,11 +4,11 @@ use crate::{AnyError, StackError, StackErrorExt, meta, stack_error};
44
55/// Provides extension methods to add context to [`StackError`]s.
66pub trait StackResultExt < T , E > {
7- /// Wraps the result's error value with additional context.
7+ /// Converts the result's error value to [`AnyError`] while providing additional context.
88 #[ track_caller]
99 fn context ( self , context : impl fmt:: Display ) -> Result < T , AnyError > ;
1010
11- /// Wraps the result's error value with lazily-evaluated additional context.
11+ /// Converts the result's error value to [`AnyError`] while providing lazily-evaluated additional context.
1212 ///
1313 /// The `context` closure is only invoked if an error occurs.
1414 #[ track_caller]
@@ -42,12 +42,17 @@ impl<T, E: StackError + 'static> StackResultExt<T, E> for Result<T, E> {
4242}
4343
4444/// Provides extension methods to add context to std errors.
45+ ///
46+ /// You should only call these methods on results that contain errors which do not implement [`StackError`].
47+ /// For results with `StackError`s, instead use the methods from [`StackResultExt`]. The latter will
48+ /// preserve call-site metadata, whereas using the methods from the [`StdResultExt`] will lose the
49+ /// call-site metadata when called on a `StackError` result.
4550pub trait StdResultExt < T , E > {
46- /// Wraps the result's error value with additional context.
51+ /// Converts the result's error value to [`AnyError`] while providing additional context.
4752 #[ track_caller]
4853 fn std_context ( self , context : impl fmt:: Display ) -> Result < T , AnyError > ;
4954
50- /// Wraps the result's error value with lazily-evaluated additional context.
55+ /// Converts the result's error value to [`AnyError`] while providing lazily-evaluated additional context.
5156 ///
5257 /// The `context` closure is only invoked if an error occurs.
5358 #[ track_caller]
@@ -57,6 +62,11 @@ pub trait StdResultExt<T, E> {
5762 C : fmt:: Display ;
5863
5964 /// Converts the result's error into [`AnyError`].
65+ ///
66+ /// You should make sure to only call this on results that contain an error which does *not*
67+ /// implement [`StackError`]. If it *does* implement [`StackError`] you can simply convert
68+ /// the result's error to [`AnyError`] with the `?` operator. If you use `anyerr` on
69+ /// `StackError`s, you will lose access to the error's call-site metadata.
6070 #[ track_caller]
6171 fn anyerr ( self ) -> Result < T , AnyError > ;
6272}
0 commit comments