Skip to content

Commit

Permalink
Merge branch 'master' into error-forward
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaBluejay committed Aug 15, 2023
2 parents 9250cca + de4f141 commit b3e425c
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 194 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- The `Into` derive now uses `#[into(<types>)]` instead of `#[into(types(<types>))]`
and ignores field type itself.
- Importing a derive macro now also import its corresponding trait.
- The `Error` derive is updated with changes to the `error_generic_member_access` unstable feature for nightly users. ([#200](https://github.com/JelteF/derive_more/pull/200), [#294](https://github.com/JelteF/derive_more/pull/294))

### Added

Expand Down
11 changes: 6 additions & 5 deletions impl/doc/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ Backtraces don't work though, because the `Backtrace` type is only available in
## Example usage

```rust
# #![cfg_attr(nightly, feature(error_generic_member_access, provide_any))]
# #![cfg_attr(nightly, feature(error_generic_member_access, error_in_core))]
// Nightly requires enabling these features:
// #![feature(error_generic_member_access, provide_any)]
// #![feature(error_generic_member_access, error_in_core)]
# #[cfg(not(nightly))] fn main() {}
# #[cfg(nightly)] fn main() {
# use std::{any, error::Error as _, backtrace::Backtrace};
# use core::error::{request_ref, request_value, Error as __};
# use std::backtrace::Backtrace;
#
# use derive_more::{Display, Error, From};

Expand Down Expand Up @@ -153,7 +154,7 @@ enum CompoundError {
}

assert!(Simple.source().is_none());
assert!(any::request_ref::<Backtrace>(&Simple).is_none());
assert!(request_ref::<Backtrace>(&Simple).is_none());
assert!(WithSource::default().source().is_some());
assert!(WithExplicitSource::default().source().is_some());
assert!(Tuple::default().source().is_some());
Expand All @@ -163,7 +164,7 @@ let with_source_and_backtrace = WithSourceAndBacktrace {
backtrace: Backtrace::capture(),
};
assert!(with_source_and_backtrace.source().is_some());
assert!(any::request_ref::<Backtrace>(&with_source_and_backtrace).is_some());
assert!(request_ref::<Backtrace>(&with_source_and_backtrace).is_some());

assert!(CompoundError::Simple.source().is_none());
assert!(CompoundError::from(Simple).source().is_some());
Expand Down
14 changes: 7 additions & 7 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn expand(

let provide = provide.map(|provide| {
quote! {
fn provide<'_demand>(&'_demand self, demand: &mut ::core::any::Demand<'_demand>) {
fn provide<'_request>(&'_request self, request: &mut ::core::error::Request<'_request>) {
#provide
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let source_provider = self.source.map(|source| {
let source_expr = &self.data.members[source];
quote! {
::derive_more::Error::provide(&#source_expr, demand);
::derive_more::Error::provide(&#source_expr, request);
}
});
let backtrace_provider = self
Expand All @@ -227,7 +227,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
.then(|| {
let backtrace_expr = &self.data.members[backtrace];
quote! {
demand.provide_ref::<::std::backtrace::Backtrace>(&#backtrace_expr);
request.provide_ref::<::std::backtrace::Backtrace>(&#backtrace_expr);
}
});

Expand All @@ -247,7 +247,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let pattern = self.data.matcher(&[source], &[quote! { source }]);
Some(quote! {
#pattern => {
::derive_more::Error::provide(source, demand);
::derive_more::Error::provide(source, request);
}
})
}
Expand All @@ -258,16 +258,16 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
);
Some(quote! {
#pattern => {
demand.provide_ref::<::std::backtrace::Backtrace>(backtrace);
::derive_more::Error::provide(source, demand);
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
::derive_more::Error::provide(source, request);
}
})
}
None => {
let pattern = self.data.matcher(&[backtrace], &[quote! { backtrace }]);
Some(quote! {
#pattern => {
demand.provide_ref::<::std::backtrace::Backtrace>(backtrace);
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn expand_struct(
.ident
.clone()
.map_or_else(|| syn::Member::Unnamed(i.into()), syn::Member::Named);
quote! { let #var = &self.#member; }
quote! { let #var = &&self.#member; }
});

let body = quote! {
Expand Down
64 changes: 61 additions & 3 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ mod enums {

mod generic {
#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::{boxed::Box, format};
use core::fmt;

use derive_more::Debug;
Expand Down Expand Up @@ -666,6 +666,22 @@ mod generic {
);
}

#[derive(Debug)]
struct NamedGenericStructUnsized<T: ?Sized> {
field: T,
}
#[test]
fn named_generic_struct_unsized() {
assert_eq!(
format!("{:?}", NamedGenericStructUnsized { field: 1 }),
"NamedGenericStructUnsized { field: 1 }",
);
assert_eq!(
format!("{:#?}", NamedGenericStructUnsized { field: 1 }),
"NamedGenericStructUnsized {\n field: 1,\n}",
);
}

#[derive(Debug)]
struct NamedGenericStructIgnored<T> {
#[debug(ignore)]
Expand Down Expand Up @@ -826,6 +842,20 @@ mod generic {
);
}

#[derive(Debug)]
struct UnnamedGenericStructUnsized<T: ?Sized>(T);
#[test]
fn unnamed_generic_struct_unsized() {
assert_eq!(
format!("{:?}", UnnamedGenericStructUnsized(2)),
"UnnamedGenericStructUnsized(2)",
);
assert_eq!(
format!("{:#?}", UnnamedGenericStructUnsized(2)),
"UnnamedGenericStructUnsized(\n 2,\n)",
);
}

#[derive(Debug)]
struct UnnamedGenericStructIgnored<T>(#[debug(skip)] T);
#[test]
Expand Down Expand Up @@ -910,11 +940,11 @@ mod generic {
fn generic_enum() {
assert_eq!(
format!("{:?}", GenericEnum::A::<_, u8> { field: 1 }),
"A { field: 1 }"
"A { field: 1 }",
);
assert_eq!(
format!("{:#?}", GenericEnum::A::<_, u8> { field: 1 }),
"A {\n field: 1,\n}"
"A {\n field: 1,\n}",
);
assert_eq!(format!("{:?}", GenericEnum::B::<u8, _>(2)), "B(2)");
assert_eq!(
Expand All @@ -923,6 +953,34 @@ mod generic {
);
}

#[derive(derive_more::Debug)]
enum GenericEnumUnsized<A: ?Sized, B: ?Sized + 'static> {
A { field: Box<A> },
B(&'static B),
}
#[test]
fn generic_enum_unsized() {
assert_eq!(
format!("{:?}", GenericEnumUnsized::A::<i32, u8> { field: 1.into() }),
"A { field: 1 }",
);
assert_eq!(
format!(
"{:#?}",
GenericEnumUnsized::A::<i32, u8> { field: 1.into() },
),
"A {\n field: 1,\n}",
);
assert_eq!(
format!("{:?}", GenericEnumUnsized::B::<u8, i32>(&2)),
"B(2)",
);
assert_eq!(
format!("{:#?}", GenericEnumUnsized::B::<u8, i32>(&2)),
"B(\n 2,\n)",
);
}

#[derive(Debug)]
enum InterpolatedGenericEnum<A, B> {
A {
Expand Down
Loading

0 comments on commit b3e425c

Please sign in to comment.