summary refs log tree commit diff
path: root/library/alloc/src/sync.rs
AgeCommit message (Collapse)AuthorLines
2024-06-10replace version placeholderPietro Albini-3/+3
2024-06-05Rollup merge of #123168 - joshtriplett:size-of-prelude, r=AmanieuJubilee-2/+0
Add `size_of` and `size_of_val` and `align_of` and `align_of_val` to the prelude (Note: need to update the PR to add `align_of` and `align_of_val`, and remove the second commit with the myriad changes to appease the lint.) Many, many projects use `size_of` to get the size of a type. However, it's also often equally easy to hardcode a size (e.g. `8` instead of `size_of::<u64>()`). Minimizing friction in the use of `size_of` helps ensure that people use it and make code more self-documenting. The name `size_of` is unambiguous: the name alone, without any prefix or path, is self-explanatory and unmistakeable for any other functionality. Adding it to the prelude cannot produce any name conflicts, as any local definition will silently shadow the one from the prelude. Thus, we don't need to wait for a new edition prelude to add it.
2024-05-20Rollup merge of #125283 - zachs18:arc-default-shared, r=dtolnayMatthias Krüger-31/+56
Use a single static for all default slice Arcs. Also adds debug_asserts in Drop for Weak/Arc that the shared static is not being "dropped"/"deallocated". As per https://github.com/rust-lang/rust/pull/124640#pullrequestreview-2064962003 r? dtolnay
2024-05-20Rollup merge of #125093 - zachs18:rc-into-raw-with-allocator-only, ↵Matthias Krüger-0/+67
r=Mark-Simulacrum Add `fn into_raw_with_allocator` to Rc/Arc/Weak. Split out from #119761 Add `fn into_raw_with_allocator` for `Rc`/`rc::Weak`[^1]/`Arc`/`sync::Weak`. * Pairs with `from_raw_in` (which already exists on all 4 types). * Name matches `Box::into_raw_with_allocator`. * Associated fns on `Rc`/`Arc`, methods on `Weak`s. <details> <summary>Future PR/ACP</summary> As a follow-on to this PR, I plan to make a PR/ACP later to move `into_raw(_parts)` from `Container<_, A: Allocator>` to only `Container<_, Global>` (where `Container` = `Vec`/`Box`/`Rc`/`rc::Weak`/`Arc`/`sync::Weak`) so that users of non-`Global` allocators have to explicitly handle the allocator when using `into_raw`-like APIs. The current behaviors of stdlib containers are inconsistent with respect to what happens to the allocator when `into_raw` is called (which does not return the allocator) | Type | `into_raw` currently callable with | behavior of `into_raw`| | --- | --- | --- | | `Box` | any allocator | allocator is [dropped](https://doc.rust-lang.org/nightly/src/alloc/boxed.rs.html#1060) | | `Vec` | any allocator | allocator is [forgotten](https://doc.rust-lang.org/nightly/src/alloc/vec/mod.rs.html#884) | | `Arc`/`Rc`/`Weak` | any allocator | allocator is [forgotten](https://doc.rust-lang.org/src/alloc/sync.rs.html#1487)(Arc) [(sync::Weak)](https://doc.rust-lang.org/src/alloc/sync.rs.html#2726) [(Rc)](https://doc.rust-lang.org/src/alloc/rc.rs.html#1352) [(rc::Weak)](https://doc.rust-lang.org/src/alloc/rc.rs.html#2993) | In my opinion, neither implicitly dropping nor implicitly forgetting the allocator is ideal; dropping it could immediately invalidate the returned pointer, and forgetting it could unintentionally leak memory. My (to-be) proposed solution is to just forbid calling `into_raw(_parts)` on containers with non-`Global` allocators, and require calling `into_raw_with_allocator`(/`Vec::into_raw_parts_with_alloc`) </details> [^1]: Technically, `rc::Weak::into_raw_with_allocator` is not newly added, as it was modified and renamed from `rc::Weak::into_raw_and_alloc`.
2024-05-19Fix typo in assert messageZachary S-1/+1
2024-05-19cfg-out unused code under no_global_oom_handlingZachary S-0/+1
2024-05-19fmtZachary S-5/+6
2024-05-19Fix stacked borrows violationZachary S-1/+5
2024-05-19Use a single static for all default slice Arcs.Zachary S-29/+48
Also adds debug_asserts in Drop for Weak/Arc that the shared static is not being "dropped"/"deallocated".
2024-05-16Access alloc field directly in Arc/Rc::into_raw_with_allocator.Zachary S-2/+2
... since fn allocator doesn't exist yet.
2024-05-13Add fn into_raw_with_allocator to Rc/Arc/Weak.Zachary S-0/+67
2024-05-13Add `size_of`, `size_of_val`, `align_of`, and `align_of_val` to the preludeJosh Triplett-2/+0
Many, many projects use `size_of` to get the size of a type. However, it's also often equally easy to hardcode a size (e.g. `8` instead of `size_of::<u64>()`). Minimizing friction in the use of `size_of` helps ensure that people use it and make code more self-documenting. The name `size_of` is unambiguous: the name alone, without any prefix or path, is self-explanatory and unmistakeable for any other functionality. Adding it to the prelude cannot produce any name conflicts, as any local definition will silently shadow the one from the prelude. Thus, we don't need to wait for a new edition prelude to add it. Add `size_of_val`, `align_of`, and `align_of_val` as well, with similar justification: widely useful, self-explanatory, unmistakeable for anything else, won't produce conflicts.
2024-05-12Use shared statics for the ArcInner for Arc<str, CStr>::default, and for ↵Zachary S-1/+51
Arc<[T]>::default where alignof(T) <= 16.
2024-05-12Add note about possible allocation-sharing to Arc/Rc<str/[T]/CStr>::default.Zachary S-0/+4
2024-05-12added Default implsBilly Sheppard-0/+21
reorganised attrs removed OsStr impls added backticks
2024-05-10Relax A: Clone requirement on Rc/Arc::unwrap_or_clone.Zachary S-0/+2
2024-05-10Relax allocator requirements on some Rc APIs.Zachary S-7/+7
* Remove A: Clone bound from Rc::assume_init, Rc::downcast, and Rc::downcast_unchecked. * Make From<Rc<[T; N]>> for Rc<[T]> allocator-aware. Internal changes: * Made Arc::internal_into_inner_with_allocator method into Arc::into_inner_with_allocator associated fn. * Add private Rc::into_inner_with_allocator (to match Arc), so other fns don't have to juggle ManuallyDrop.
2024-04-12Auto merge of #120092 - zetanumbers:pin_in_static_allocator, r=Amanieubors-2/+8
Add `A: 'static` bound for `Arc/Rc::pin_in` Analogous to https://github.com/rust-lang/rust/pull/79327 Needed to preserve pin's [drop guarantee](https://doc.rust-lang.org/std/pin/index.html#drop-guarantee)
2024-04-07make a doctest less slow in MiriRalf Jung-1/+3
2024-03-25Require DerefPure for patternsMichael Goulet-1/+4
2024-03-19SeqCst->Relaxed in doc examples.Mara Bos-1/+1
SeqCst is unnecessary here.
2024-03-05Rollup merge of #121287 - zachs18:rc-into-raw-must-use, r=cuviperMatthias Krüger-1/+1
Clarify/add `must_use` message for Rc/Arc/Weak::into_raw. The current `#[must_use]` messages for `{sync,rc}::Weak::into_raw` ("`self` will be dropped if the result is not used") are misleading, as `self` is consumed and will *not* be dropped. This PR changes their `#[must_use]` message to the same as `Arc::into_raw`'s[ current `#[must_use]` message](https://github.com/rust-lang/rust/blob/d5735645753e990a72446094f703df9b5e421555/library/alloc/src/sync.rs#L1482) ("losing the pointer will leak memory"), and also adds it to `Rc::into_raw`, which is not currently `#[must_use]`.
2024-02-24library: use `addr_of!`Pavel Grigorenko-2/+2
2024-02-21rename ptr::invalid -> ptr::without_provenanceRalf Jung-2/+6
also introduce ptr::dangling matching NonNull::dangling
2024-02-18Clarify/add `must_use` message for Rc/Arc/Weak::into_raw.Zachary S-1/+1
2024-02-15Rollup merge of #120449 - udoprog:document-unsized-rc-arc-from-raw, r=m-ou-seGuillaume Gomez-13/+61
Document requirements for unsized {Rc,Arc}::from_raw This seems to be implied due to these types supporting operation-less unsized coercions. Taken together with the [established behavior of a wide to thin pointer cast](https://github.com/rust-lang/reference/pull/1451) it would enable unsafe downcasting of these containers. Note that the term "data pointer" is adopted from https://github.com/rust-lang/rfcs/pull/3559 See also this [internals thread](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2).
2024-01-30Rollup merge of #120445 - Nemo157:arc-plug, r=Mark-SimulacrumGuillaume Gomez-24/+20
Fix some `Arc` allocator leaks This doesn't matter for the stable `Global` allocator as it is a ZST singleton, but other allocators may rely on all instances being dropped.
2024-01-29Rollup merge of #120266 - steffahn:a_rc_into_inner_docs, r=Mark-SimulacrumDylan DPC-3/+7
Improve documentation for [A]Rc::into_inner General improvements, and also aims to better encourage the reader to actually check out Arc::try_unwrap. This addresses concerns from https://github.com/rust-lang/rust/issues/106894#issuecomment-1905627234. Rendered: ![Screenshot_20240123_114436](https://github.com/rust-lang/rust/assets/3986214/68896d62-13e0-4f3a-8073-91d8e77c5554) ![Screenshot_20240123_114455](https://github.com/rust-lang/rust/assets/3986214/dc58e4bd-dd7f-40b1-bc50-fd6200dde593)
2024-01-28Fix some `Arc` allocator leaksWim Looman-24/+20
This doesn't matter for the stable `Global` allocator as it is a ZST singleton, but other allocators may rely on all instances being dropped.
2024-01-28Fix doctestJohn-John Tedro-2/+3
2024-01-28Replicate documentation in {Rc,Arc}::from_raw_inJohn-John Tedro-7/+32
2024-01-28Fix doctestJohn-John Tedro-2/+2
2024-01-28Tidy upJohn-John Tedro-1/+1
2024-01-28Add examples for unsized {Rc,Arc}::from_rawJohn-John Tedro-0/+14
2024-01-28Document requirements for unsized {Rc,Arc}::from_rawJohn-John Tedro-6/+14
2024-01-23Auto merge of #119433 - taiki-e:rc-uninit-ref, r=Nilstriebbors-9/+9
rc,sync: Do not create references to uninitialized values Closes #119241 r? `@RalfJung`
2024-01-23Improve documentation for [A]Rc::into_innerFrank Steffahn-3/+7
General improvements, and also aims to better encourage the reader to actually check out Arc::try_unwrap.
2024-01-18Add `A: 'static` bound for `Arc/Rc::pin_in`zetanumbers-2/+8
2024-01-10Fix deallocation with wrong allocator in (A)Rc::from_box_inzachs18-1/+1
2024-01-08rc,sync: Do not create references to uninitialized valuesTaiki Endo-9/+9
2024-01-03Rollup merge of #119434 - taiki-e:rc-is-dangling, r=Mark-SimulacrumLeón Orell Valerian Liehr-1/+1
rc: Take *const T in is_dangling It is not important which one is used since `is_dangling` does not access memory, but `*const` removes the needs of `*const T` -> `*mut T` casts in `from_raw_in`.
2023-12-30Rollup merge of #119158 - JohnTheCoolingFan:arc-weak-clone-pretty, r=cuviperMatthias Krüger-14/+11
Clean up alloc::sync::Weak Clone implementation Since both return points (tail and early return) return the same expression and the only difference is whether inner is available, the code that does the atomic operations and checks on inner was moved into the if body and the only return is at the tail. Original comments preserved.
2023-12-30rc: Take *const T in is_danglingTaiki Endo-1/+1
It is not important which one is used since `is_dangling` does not access memory, but `*const` removes the needs of `*const T` -> `*mut T` casts in `from_raw_in`.
2023-12-22update version placeholdersPietro Albini-1/+1
2023-12-20Cleaned up alloc::sync::Weak Clone implementationJohnTheCoolingFan-14/+11
Since both return points (tail and early return) return the same expression and the only difference is whether inner is available, the code that does the atomic operations and checks on inner was moved into the if body and the only return is at the tail. Original comments preserved.
2023-12-10Auto merge of #116949 - hamza1311:stablize-arc_unwrap_or_clone, r=dtolnaybors-2/+1
Stablize arc_unwrap_or_clone Fixes: #93610 This likely needs FCP. I created this PR as it's stabilization is trivial and FCP can be just conducted here. Not sure how to ping the libs API team (last attempt didn't work apparently according to GH UI)
2023-12-07Auto merge of #117960 - zhiqiangxu:dry, r=workingjubileebors-5/+3
chore: avoid duplicate code in `Weak::inner`
2023-12-06Don't repeat yourselfzhiqiangxu-5/+3
2023-11-28Add proper cfgsr0cky-0/+1
2023-10-19Stablize arc_unwrap_or_cloneMuhammad Hamza-2/+1