Skip to content

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

Closed
@strega-nil

Description

@strega-nil

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions