derive-new icon indicating copy to clipboard operation
derive-new copied to clipboard

Feature Request: ::new(_) should support .into()

Open zutils opened this issue 5 years ago • 2 comments

Many times I have MyStruct::new("something".into()). I would rather not keep typing into(). Perhaps there is a way that new can take an Into<T>.

zutils avatar Jul 16 '19 21:07 zutils

I came to make the same suggestion.

However, I also prefer precision and explicitness, so I wouldn't recommend always supporting Into on every new param. Here are some brainstorms about design:

A per-struct style configuration

The default style equivalent to the current direct/concrete argument types, then a style called "Into" is introduced:

#[derive(new)]
#[new(style="Into")]
struct Account {
  name: String,
  info: Box<Address>,
}

let acct = Account::new("alice", Address::new(…));

Per-field configuration

#[derive(new)]
struct Account {
  #[new(style="Into")]
  name: String,
  info: Box<Address>,
}

let acct = Account::new("alice", Address::new(…)); // Error: expected `Box<Address>` found `Address`

My preference is to support both struct-scoped and per-field configurations. Struct-scoped reduces boilerplate, whereas per-field allows fined-grained precision.

nathan-at-least avatar Jul 12 '22 14:07 nathan-at-least

I implemented the feature in #65 (first time contributor to this crate). Please let me know what you think.

OmarTawfik avatar Dec 21 '23 08:12 OmarTawfik