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

implement into and into_iter #65

Closed
wants to merge 3 commits into from

Conversation

OmarTawfik
Copy link
Contributor

@OmarTawfik OmarTawfik commented Dec 15, 2023

Thank you for the awesome crate!
Here is a feature suggestion, that is sorely missed in our projects. It has also been requested twice already (#43 and #62).

#[new(into)]

To make type conversion easier, #[new(into)] attribute changes the parameter type to impl Into<T>, and populates the field with value.into(). This is useful for containers like strings, accepting a String or a &str:

#[derive(new)]
struct Foo {
    #[new(into)]
    x: String,
}

let _ = Foo::new("Hello".to_string());
let _ = Foo::new(format!("Hello"));
let _ = Foo::new("Hello");

It is also useful for wrapping types like Box<T> and Rc<T>, without having to wrap at every callsite:

#[derive(new)]
struct Foo {
    #[new(into)]
    x: Rc<Bar>,
}

let _ = Foo::new(Rc::new(bar));
let _ = Foo::new(bar.into());
let _ = Foo::new(bar);

#[new(into_iter = "T")]

For iterators/collections, #[new(into_iter = "T")] attribute changes the parameter type to impl IntoIterator<Item = T>, and populates the field with value.into_iter().collect(). This allows initializing with a variety of types, like [T], Option<T>, Vec<T>, sets, maps, and even other iterators:

#[derive(new)]
struct Foo {
    #[new(into_iter = "bool")]
    x: Vec<bool>,
}

let _ = Foo::new(vec![true, false]);
let _ = Foo::new([true, false]);
let _ = Foo::new(Some(true));

About this PR

I split this into three parts to make reviewing easier:

  • 08e5390 fixes broken integration tests (I suspect CI is out of date).
  • 3e310d9 fixes formatting for a couple of files (using the default cargo fmt).
  • 8f60880 implements the feature, adding tests, and documentation to both README.md and lib.rs.

Please let me know if I can do anything else.

@OmarTawfik
Copy link
Contributor Author

cc @nrc

Closes #43
Closes #62

@DenisGorbachev
Copy link

@nrc Is it possible to merge this PR please? This would be a time-saver for a lot of projects

@polarathene
Copy link

@nrc friendly ping

Copy link
Owner

@nrc nrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is a great improvement, sorry it took so long to review

@nrc
Copy link
Owner

nrc commented Aug 29, 2024

Merged manually to resolve conflicts

@nrc nrc closed this Aug 29, 2024
@OmarTawfik OmarTawfik deleted the impl-into branch August 29, 2024 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants