Skip to content

Commit

Permalink
Fix TryInto derive for generics (#384)
Browse files Browse the repository at this point in the history
## Synopsis

Our `TryFrom` derive would fail when the type had any generics, this was
a bug introduced with our changes for 1.0.

## Solution

This fixes that by correctly adding the generic parameters to the
`Error` type of `TryFrom`.
  • Loading branch information
JelteF authored Jul 8, 2024
1 parent c7a3111 commit 647e3a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion impl/src/try_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
impl #impl_generics derive_more::core::convert::TryFrom<
#reference_with_lifetime #input_type #ty_generics
> for (#(#reference_with_lifetime #original_types),*) #where_clause {
type Error = derive_more::TryIntoError<#reference_with_lifetime #input_type>;
type Error = derive_more::TryIntoError<#reference_with_lifetime #input_type #ty_generics>;

#[inline]
fn try_from(
Expand Down
10 changes: 9 additions & 1 deletion tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use derive_more::{
Add, AddAssign, Constructor, Deref, DerefMut, Display, Error, From, FromStr, Index,
IndexMut, IntoIterator, Mul, MulAssign, Not, Sum,
IndexMut, IntoIterator, Mul, MulAssign, Not, Sum, TryInto,
};

#[derive(
Expand Down Expand Up @@ -262,3 +262,11 @@ struct StructLifetimeGenericBoundsConstDefault<
> {
inner: &'lt E,
}

#[derive(Debug, Display)]
struct Wrapper<'a, const Y: usize, U>(&'a [U; Y]);

#[derive(Debug, Display, TryInto)]
enum Foo<'lt: 'static, T: Clone, const X: usize> {
X(Wrapper<'lt, X, T>),
}
7 changes: 7 additions & 0 deletions tests/try_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ enum MixedInts {
Unit2,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Wrapper<'a, const Y: usize, U>(&'a [U; Y]);

enum Foo<'lt: 'static, T: Clone, const X: usize> {
X(Wrapper<'lt, X, T>),
}

#[test]
fn test_try_into() {
let mut i = MixedInts::SmallInt(42);
Expand Down

0 comments on commit 647e3a5

Please sign in to comment.