derive_more
derive_more copied to clipboard
Add a `default` and `value` attribute parameters
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.
@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()?
}
Possibly we would want this case to have a special syntax for usability, e.g. #[from(backtrace_capture)]
@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>,
This is a good idea, so how is the progress now?
Removing this from the 1.0 milestone, since it doesn't require breaking changes.
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.