Skip to content

Commit 92ae1cb

Browse files
committed
Update docs with forwarding details
1 parent f13572a commit 92ae1cb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

impl/doc/error.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ often [`From` as well](crate::From).
3232
called `Backtrace`. Then it would return that field as the `backtrace`.
3333
3. One of the fields is annotated with `#[error(backtrace)]`. Then it would
3434
return that field as the `backtrace`.
35+
4. The source field is annotated with `#[error(backtrace)]`. Then it would
36+
forward implementation to the source.
37+
38+
If the source field is annotated with `#[error(backtrace)]`, and another field
39+
is also annotated/was inferred, both backtraces will be provided.
3540

3641
### Ignoring fields for derives
3742

@@ -41,6 +46,11 @@ detecting `backtrace` and `source`. It's also possible to mark a field only
4146
ignored for one of these methods by using `#[error(not(backtrace))]` or
4247
`#[error(not(source))]`.
4348

49+
### Source Forwarding
50+
51+
A struct, enum, or enum variant can be annotated with `#[error(forward)]` to forward
52+
the `source()` implementation to the source field (inferred or explicitly annotated),
53+
instead of returning the field itself.
4454

4555
### What works in `no_std`?
4656

@@ -126,6 +136,17 @@ enum CompoundError {
126136
},
127137
Tuple(WithExplicitSource),
128138
WithoutSource(#[error(not(source))] Tuple),
139+
#[error(forward)]
140+
#[from(ignore)]
141+
ForwardedImplicitSource {
142+
source: WithSource,
143+
},
144+
#[error(forward)]
145+
#[from(ignore)]
146+
ForwardedExplicitSourceWithBacktrace {
147+
#[error(source, backtrace)]
148+
explicit_source: WithSourceAndBacktrace,
149+
}
129150
}
130151

131152
assert!(Simple.source().is_none());
@@ -146,5 +167,14 @@ assert!(CompoundError::from(Simple).source().is_some());
146167
assert!(CompoundError::from(WithSource::default()).source().is_some());
147168
assert!(CompoundError::from(WithExplicitSource::default()).source().is_some());
148169
assert!(CompoundError::from(Tuple::default()).source().is_none());
170+
171+
let forwarded = CompoundError::ForwardedImplicitSource { source: WithSource::default() };
172+
assert!(forwarded.source().is_some());
173+
assert!(forwarded.source().unwrap().is::<Simple>());
174+
175+
let forwarded_with_backtrace = CompoundError::ForwardedExplicitSourceWithBacktrace { explicit_source: WithSourceAndBacktrace { source: Simple, backtrace: Backtrace::capture() } };
176+
assert!(forwarded_with_backtrace.source().is_some());
177+
assert!(forwarded_with_backtrace.source().unwrap().is::<Simple>());
178+
assert!(any::request_ref::<Backtrace>(&forwarded_with_backtrace).is_some());
149179
# }
150180
```

0 commit comments

Comments
 (0)