Support constructing cc_std::std::optional payloads in place. - #2042
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_982794431
branch
3 times, most recently
from
September 18, 2026 00:03
8d1cc81 to
4de9079
Compare
`std::optional<T>` can hold a `T` which cannot be produced by value in Rust,
most importantly the layout-compatible `cc_std::std::string`, which is `!Unpin`
and only constructible through a `Ctor`. Previously an `optional<T>` could only
be built from an owned `T`, via `optional::new` or `From<Option<T>>`, so such a
payload was unreachable.
Add in-place construction and mutation:
* `optional::ctor_some` and `optional::ctor_nullopt` return `Ctor`s which
construct the payload directly into the `optional`'s storage.
* `optional::emplace` and `optional::reset` are the pinned counterparts to
assigning through `as_mut` and to `take`. They construct and destroy the
payload where it lives rather than moving it, so they remain available when
the payload is pinned.
`ctor_some` is an inherent function rather than a `CtorNew` impl because `ctor`
provides a blanket `impl<T: Default> CtorNew<()> for T`, which an
`impl<T, C: Ctor<Output = T>> CtorNew<C> for optional<T>` would overlap.
`SomeCtor::ctor` clears `engaged` before running the payload's constructor, so a
payload constructor which fails or panics leaves behind a valid empty `optional`
rather than one whose destructor would read an uninitialized payload. `Drop` now
shares that logic, which also makes it safe against a panicking payload
destructor.
`trivial_optional` is deliberately unchanged: it is the destructor-free tier
used for fields of `Copy` structs, and a payload which is `!Unpin` or
ctor-only is never `Copy`.
PiperOrigin-RevId: 983514697
copybara-service
Bot
force-pushed
the
test_982794431
branch
from
September 18, 2026 00:21
4de9079 to
8e91224
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support constructing
cc_std::std::optionalpayloads in place.std::optional<T>can hold aTwhich cannot be produced by value in Rust,most importantly the layout-compatible
cc_std::std::string, which is!Unpinand only constructible through a
Ctor. Previously anoptional<T>could onlybe built from an owned
T, viaoptional::neworFrom<Option<T>>, so such apayload was unreachable.
Add in-place construction and mutation:
optional::ctor_someandoptional::ctor_nulloptreturnCtors whichconstruct the payload directly into the
optional's storage.optional::emplaceandoptional::resetare the pinned counterparts toassigning through
as_mutand totake. They construct and destroy thepayload where it lives rather than moving it, so they remain available when
the payload is pinned.
ctor_someis an inherent function rather than aCtorNewimpl becausectorprovides a blanket
impl<T: Default> CtorNew<()> for T, which animpl<T, C: Ctor<Output = T>> CtorNew<C> for optional<T>would overlap.SomeCtor::ctorclearsengagedbefore running the payload's constructor, so apayload constructor which fails or panics leaves behind a valid empty
optionalrather than one whose destructor would read an uninitialized payload.
Dropnowshares that logic, which also makes it safe against a panicking payload
destructor.
trivial_optionalis deliberately unchanged: it is the destructor-free tierused for fields of
Copystructs, and a payload which is!Unpinorctor-only is never
Copy.