Skip to content

FR: derive Default also for Structs with non implementers of Default #3761

@thkoch2001

Description

@thkoch2001

Given a struct:

struct Outlink {
    rel: Option<String>,
    context: Context,
    url: Url,                // Url does not implement Default !!!
    redirect_count: usize,
    content_type: Option<String>,
    // ... more members implementing Default
}

I want to be able to:

#[derive(Default)]
struct Outlink {
    rel: Option<String>,
    context: Context,
    #[default_from=EXPRESSION]
    url: Url,
    redirect_count: usize,
    content_type: Option<String>,
    // ... more members implementing Default
}

Where EXPRESSION is any valid expression of type Url, e.g. Url.parse("file:///dummy").unwrap().

This satisfies at least two scenarios:

a) There is no need anymore for trivil constructors, as they can instead be written as:

    let url = ...;
    let outlink = Outlink {
        url .. Outlink::Default()
    }

b) Types that have no intrinsic default value but do have a sensible default value in the context of another type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions