You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-standard-lazy-types.md
+43-2Lines changed: 43 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -245,8 +245,7 @@ Non thread-safe flavor can be added to `core` as well.
245
245
246
246
The thread-safe variant is implemented similarly to `std::sync::Once`.
247
247
Crucially, it has support for blocking: if many threads call `get_or_init` concurrently, only one will be able to execute the closure, while all other threads will block.
248
-
For this reason, `std::sync::OnceCell` can not be provided in core.
249
-
Even the minimal `OnceCell::<T>::set` API requires support for blocking, because one can't atomically set arbitrary `T`.
248
+
For this reason, most of `std::sync::OnceCell` API can not be provided in `core`.
250
249
251
250
# Drawbacks
252
251
[drawbacks]: #drawbacks
@@ -335,6 +334,48 @@ That is, we can store some initial state in the cell and consume it during initi
335
334
In practice, such flexibility seems to be rarely required.
336
335
Even if we add a type, similar to `OnceFlipCell`, having a dedicated `OnceCell` (which *could* be implemented on top of `OnceFlipCell`) type simplifies a common use-case.
337
336
337
+
## Variations of `set`
338
+
339
+
The RFC proposes "obvious" signature for the `set` method:
340
+
341
+
```rust
342
+
fnset(&self, value:T) ->Result<(), T>;
343
+
```
344
+
345
+
Note, however, that `set` establishes an invariant that the cell is initialized, so a more precise signature would be
346
+
347
+
```rust
348
+
fnset(&self, value:T) -> (&T, Option<T>);
349
+
```
350
+
351
+
To be able to return a reference, `set` might need to block a thread.
352
+
For example, if two threads call `set` concurrently, one of them needs to block while the other moves the value into the cell.
353
+
It is possible to provide a non-blocking alternative to `set`:
0 commit comments