Closed Bug 1645328 Opened 5 years ago Closed 5 years ago

Add in-place constructor to Maybe

Categories

(Core :: MFBT, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
mozilla79
Tracking Status
firefox79 --- fixed

People

(Reporter: mozbugz, Assigned: sg)

References

Details

Attachments

(1 file)

The only way to construct a Maybe with a value is through Some(...), which requires the type to be copyable or movable.
This prevents some use cases, like storing a non-copyable non-movable object.

To allow this, we could add an in-place constructor, similar to template <typename... Args> std::optional::optional(std::in_place_t, Args&&...).

I stumbled upon that a few times as well.

We essentially have the machine for that already through Maybe::emplace, we probably just need to add constructor that calls that.

emplace assumes (and even MOZ_DIAGNOSTIC_ASSERTs) that we already have Maybe in the default Nothing state (so mIsSome is set to false first), I was hoping the in-place constructor would skip that step and initialize all members to their final values at once.

Yes, that's right. The original initialization and the MOZ_DIAGNOSTIC_ASSERT might be typically optimized away though? But to be on the "safe" side, the cosntruction could be done right away, yes.

Assignee: nobody → sgiesecke
Status: NEW → ASSIGNED
Attachment #9156250 - Attachment description: Bug 1645328 - Add in-place constructors to Maybe. r=froydnj → Bug 1645328 - Add in-place constructor to Maybe. r=froydnj
Summary: Add in-place constructors to Maybe → Add in-place constructor to Maybe
Pushed by sgiesecke@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/5200dd8a0de5 Add in-place constructor to Maybe. r=froydnj

I fixed the patch now and am going to re-land it. Sorry, I introduced this defect by some last-minute changes :(

Flags: needinfo?(sgiesecke)
Pushed by sgiesecke@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/9497e6381139 Add in-place constructor to Maybe. r=froydnj
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla79
Regressions: 1672946
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: