Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field-specific functionality to Into #291

Merged
merged 50 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2df8ad0
Allow empty `#[into]` attribute
MegaBluejay Aug 10, 2023
c8688d9
Abstract over fields type in check_legacy_syntax
MegaBluejay Aug 10, 2023
7f39536
Fix fields type abstraction to work with slices
MegaBluejay Aug 10, 2023
df13fc9
Implement field attribute parsing
MegaBluejay Aug 10, 2023
37671b5
Extract expand_attr function
MegaBluejay Aug 11, 2023
1aaa9a9
Expand both struct and field attrs
MegaBluejay Aug 11, 2023
203663a
Fix skip parsing
MegaBluejay Aug 11, 2023
9821fbf
Separate IntoArgs from StructAttribute
MegaBluejay Aug 11, 2023
dcf82c3
Add comment explaining all-fields logic
MegaBluejay Aug 11, 2023
47200dc
Add Into field attribute tests
MegaBluejay Aug 11, 2023
e38b899
Clean up naming
MegaBluejay Aug 11, 2023
358ff32
Add compile_fail tests for new errors
MegaBluejay Aug 11, 2023
167f9ed
Add comment explaining manual impls in tests
MegaBluejay Aug 14, 2023
4f8e0c5
Document Into field attribute usage
MegaBluejay Aug 14, 2023
b50602d
Clean up fields data processing with unzip3 utility function
MegaBluejay Aug 14, 2023
cdf8e2b
Merge branch 'master' into 287-fix-derive-into
MegaBluejay Aug 14, 2023
8bc380f
Add support for skipping fields for which separate conversions exist
MegaBluejay Aug 14, 2023
714e803
Update compile_fail test to reflect changes
MegaBluejay Aug 14, 2023
0686198
Update test with multi-attribute case
MegaBluejay Aug 14, 2023
be14ca7
Update changelog
MegaBluejay Aug 14, 2023
ee920b1
Document behavior of skip with other field attributes
MegaBluejay Aug 14, 2023
091790f
Improve parameter naming in expand_args
MegaBluejay Aug 14, 2023
2bc0ae8
Merge branch 'master' into 287-fix-derive-into
MegaBluejay Aug 16, 2023
a410e7b
Document Into macro parsing helpers, use less unqualified syn uses
MegaBluejay Aug 16, 2023
ec38941
Reorganize Into expand to not require unzip4 helper
MegaBluejay Aug 16, 2023
1697a6a
[skip ci] Fix typo
MegaBluejay Aug 17, 2023
ab45709
Refactor Into derive to use the Expansion struct pattern
MegaBluejay Aug 21, 2023
bdec31b
Merge branch 'master' into 287-fix-derive-into
MegaBluejay Aug 21, 2023
be4f7af
Merge branch 'master' into 287-fix-derive-into
MegaBluejay Aug 25, 2023
6945bc0
Fix comments/errors to reflect both skip and ignore are valid arguments
MegaBluejay Aug 25, 2023
7f3a6a4
Remove unused parser
MegaBluejay Aug 25, 2023
fb4c646
Use try_fold in parsing utils
MegaBluejay Aug 25, 2023
a358d0b
Remove extra newline
MegaBluejay Sep 4, 2023
2b9d47b
Merge branch 'master' into 287-fix-derive-into
MegaBluejay Oct 8, 2023
e10f7f4
Add needed parsing utils
MegaBluejay Oct 8, 2023
a0514eb
Finish attribute parsing
MegaBluejay Oct 8, 2023
74baa84
Fix conditional compilation
MegaBluejay Oct 8, 2023
61a93b1
Add missing merge
MegaBluejay Oct 8, 2023
eae11e7
Fix empty attribute parsing
MegaBluejay Oct 8, 2023
0dcf3d5
Put field and struct attributes in submodules
MegaBluejay Oct 8, 2023
7328496
Add some doc comments, add more structure in utils
MegaBluejay Oct 8, 2023
64974c7
Fix legacy syntax check and error messages
MegaBluejay Oct 8, 2023
e67d735
Rename IntoArgs into IntoConversions
MegaBluejay Oct 9, 2023
78b9fc1
Make minor comment corrections
MegaBluejay Oct 9, 2023
e0f6046
Add PR link to changelog
MegaBluejay Oct 9, 2023
beda92c
Make expanatory comments in tests match other tests more closely
MegaBluejay Oct 9, 2023
982547b
Merge branch 'master' into 287-fix-derive-into
tyranron Oct 11, 2023
53803d1
Correct docs
tyranron Oct 11, 2023
0240f80
Simplify implementation
tyranron Oct 12, 2023
d17be47
Bikeshed CHANGELOG
tyranron Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
and ignores field type itself.
- The `Into` derive now uses `#[into(<types>)]` instead of `#[into(types(<types>))]`
and ignores field type itself.
- The `Into` derive now generates separate impls for each field whenever the `#[into(...)]`
attribute is applied to it. ([#291](https://github.com/JelteF/derive_more/pull/291))
- 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),
Expand Down
84 changes: 84 additions & 0 deletions impl/doc/into.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,90 @@ assert_eq!(5_i128, Mass::<Gram>::new(5).into());
```


### Fields

The `#[into]` attribute can also be applied to specific fields of a struct.

```rust
# use derive_more::Into;
#
#[derive(Into)]
struct Data {
id: i32,
#[into]
raw: f64
}

assert_eq!(42.0, Data { id: 1, raw: 42.0 }.into());
```

In such cases, no conversion into a tuple of all fields is generated, unless
an explicit struct attribute is present.

```rust
# use derive_more::Into;
#
#[derive(Into)]
#[into]
struct Data {
id: i32,
#[into]
raw: f64
}

assert_eq!(42.0, Data { id: 1, raw: 42.0 }.into());
assert_eq!((1, 42.0), Data { id: 1, raw: 42.0 }.into());
```

The `#[into(<types>)]` syntax can be used on fields as well.

```rust
# use std::marker::PhantomData;
# use derive_more::Into;
# struct Whatever;
#
#[derive(Into, Clone)]
#[into(owned, ref((u8, str)), ref_mut)]
struct Foo {
#[into(owned(u64), ref)]
a: u8,
b: String,
#[into(skip)]
_c: PhantomData<Whatever>,
}

let mut foo = Foo { a: 1, b: "string".to_owned(), _c: PhantomData };

assert_eq!((1_u8, "string".to_owned()), foo.clone().into());
assert_eq!((&1_u8, "string"), <(&u8, &str)>::from(&foo));
assert_eq!((&mut 1_u8, &mut "string".to_owned()), <(&mut u8, &mut String)>::from(&mut foo));
assert_eq!(1_u64, foo.clone().into());
assert_eq!(&1_u8, <&u8>::from(&foo));
```

Fields, having specific conversions into them, can also be skipped for top-level
tuple conversions.

```rust
# use derive_more::Into;

#[derive(Into)]
#[into(ref((str, f64)))]
struct Foo {
#[into(ref)]
#[into(skip)]
a: u8,
b: String,
c: f64,
}

let foo = Foo { a: 1, b: "string".to_owned(), c: 3.0 };

assert_eq!(("string", &3.0), (&foo).into());
assert_eq!(&1_u8, <&u8>::from(&foo));
```




## Enums
Expand Down
Loading