Skip to content

Support constructing cc_std::std::optional payloads in place. - #2042

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_982794431
Sep 18, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_982794431

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Sep 17, 2026

Copy link
Copy Markdown

Support constructing cc_std::std::optional payloads in place.

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 Ctors 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.

@copybara-service
copybara-service Bot force-pushed the test_982794431 branch 3 times, most recently from 8d1cc81 to 4de9079 Compare September 18, 2026 00:03
`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
copybara-service Bot merged commit 8e91224 into main Sep 18, 2026
11 checks passed
@copybara-service
copybara-service Bot deleted the test_982794431 branch September 18, 2026 00:22
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

Successfully merging this pull request may close these issues.

1 participant