Skip to content

feat: implement OnceLock#519

Open
chise0713 wants to merge 3 commits into
Amanieu:masterfrom
chise0713:once_lock
Open

feat: implement OnceLock#519
chise0713 wants to merge 3 commits into
Amanieu:masterfrom
chise0713:once_lock

Conversation

@chise0713
Copy link
Copy Markdown

@chise0713 chise0713 commented Apr 11, 2026

This PR is based on the OnceLock implementation from the standard library, with modifications to adapt it to our Once implementation.

Related: #440

Tests may benefit from further validation and edge case coverage.

Any feedback on the design and implementation would be appreciated.

- ensure failed init does not mark `DONE_BIT`
- align behavior with std OnceLock semantics

- improve test coverage

- add docs and comments
Comment thread src/once.rs Outdated
fn call_once_slow(
&self,
ignore_poison: bool,
from_once_lock: bool, // work around for `OnceLock::get_or_try_init`
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix incorrect Err handling in OnceLock::get_or_try_init
and align with std semantics, I extended Once's internal API
to allow manual control over its state.

This keeps the public API unchanged, but adds a OnceLock-specific
code path internally.

I'm not entirely confident this is the best approach, so I'd really
appreciate any feedback!

Comment thread src/once_lock.rs
value: UnsafeCell<MaybeUninit<T>>,
}

impl<T> OnceLock<T> {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation is mostly adapted from the standard library.

- eliminate invalid state combinations
- improve readability and intent clarity
@chise0713 chise0713 marked this pull request as ready for review April 19, 2026 06:05
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