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 default::Default to the (core) prelude #1035

Closed
strega-nil opened this issue Apr 5, 2015 · 1 comment
Closed

Add default::Default to the (core) prelude #1035

strega-nil opened this issue Apr 5, 2015 · 1 comment

Comments

@strega-nil
Copy link

With CTFE, added backwards compatibly at a later date, this would be very useful for static initialization. No longer, to initialize a StaticMutex, would you have to write

static ST: StaticMutex = STATIC_MUTEX_INIT;

You could instead write

static ST: StaticMutex = Default::default();

This is really useful for standard library writers, because it just looks nicer. It's a small feature, but one that doesn't take anything away from anybody.

You also can just derive(Default) for types that should start out zeroed (for example, my heap implementation currently):

const HEAP_INIT: Heap =
Heap {
    heap: 0 as *mut u8,
    hptr: 0 as *mut u8,
    size: 0,
};

static HEAP: Heap = HEAP_INIT;

static LOOP_BUFFERS: [Heap; 5] = [HEAP_INIT; 5];

Whereas with this change (and CTFE)

static HEAP: Heap = Default::default();

static LOOP_BUFFERS: [Heap; 5] = [Default::default(); 5];

And, with a future, also backwards compatible change which adds in a C++ style {} syntax

static HEAP: Heap = Heap {..};

static LOOP_BUFFERS: [Heap; 5] = [Heap {..}; 5];

or something like that.

@alexcrichton
Copy link
Member

Thanks for the issue! This is actually currently being proposed in #1030 as well, so I'm going to close in favor of that, and feel free to weigh in on the PR!

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

2 participants