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 a default and value attribute parameters #102

Open
JelteF opened this issue Nov 3, 2019 · 6 comments
Open

Add a default and value attribute parameters #102

JelteF opened this issue Nov 3, 2019 · 6 comments
Milestone

Comments

@JelteF
Copy link
Owner

JelteF commented Nov 3, 2019

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
Copy link
Owner Author

JelteF commented Mar 28, 2020

@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
Copy link
Owner Author

JelteF commented Mar 28, 2020

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

@tyranron
Copy link
Collaborator

@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>,

@jmjoy
Copy link

jmjoy commented May 17, 2021

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

@JelteF JelteF removed this from the 1.0.0 milestone Jun 10, 2022
@JelteF
Copy link
Owner Author

JelteF commented Jun 10, 2022

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

@ModProg
Copy link
Contributor

ModProg commented Sep 7, 2023

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.

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

No branches or pull requests

4 participants