derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Add a `default` and `value` attribute parameters

Open JelteF opened this issue 5 years ago • 6 comments

This would allow the same flexibility that's provided by derive_new for From, TryFrom, FromStr and Constructor. It would also be nice to add new as a derive as well, so derive_more can be a drop-in replacement for derive_new.

JelteF avatar Nov 03 '19 23:11 JelteF

@ffuugoo @tyranron This is needed to make Error derives with a Backtrace really nice. Then you can do something like:

#[derive(From, Error, Display)]
struct SimpleError;

#[derive(From, Error, Display)]
#[display(fmt="Some error")]
struct BacktraceError {
   source: SimpleError,
	#[from(value=::std::backtrace::capture())]
   backtrace: Backtrace,
}

fn simple_failing_function() -> Result<(), SimpleError> {
    Err(SimpleError)
}

fn backtrace_failing_function() -> Result<(), BacktraceError> {
    simple_failing_function()?
}

JelteF avatar Mar 28 '20 12:03 JelteF

Possibly we would want this case to have a special syntax for usability, e.g. #[from(backtrace_capture)]

JelteF avatar Mar 28 '20 12:03 JelteF

@JelteF I'm agree with having derive_new in derive_more! 🤘

But regarding the syntax #[from(value=::std::backtrace::capture())], I still think it's more idiomatically to use the naming adopted by serde_derive: #[from(with = ::std::backtrace::capture())].

Also, it worth considering the syntax in smart-default. Having some implicit coercions, it's quite neat and handy in practice (we use it a lot aong with derive_more):

#[default = "localhost"]
host: Cow<'static, str>,

tyranron avatar Mar 30 '20 08:03 tyranron

This is a good idea, so how is the progress now?

jmjoy avatar May 17 '21 09:05 jmjoy

Removing this from the 1.0 milestone, since it doesn't require breaking changes.

JelteF avatar Jun 10 '22 08:06 JelteF

Maybe it would also be nice to be able to define Self::new as an alias to Default without needing to specify that on every field.

ModProg avatar Sep 07 '23 19:09 ModProg