about summary refs log tree commit diff
path: root/library/alloc/src/sync.rs
AgeCommit message (Collapse)AuthorLines
2023-03-23Stabilize `arc_into_inner` and `rc_into_inner`.Frank Steffahn-20/+3
Includes resolving the FIXMEs in the documentation, and some very minor documentation improvements.
2023-03-15unequal → not equalgimbles-2/+2
2023-03-11Rollup merge of #108708 - noamtashma:issue-108706-fix, r=m-ou-seMatthias Krüger-3/+17
Prevent overflow through Arc::downgrade Fixes #108706
2023-03-06issue-108706-fixNoam Ta Shma-3/+17
2023-03-03Match unmatched backticks in library/est31-1/+1
2023-01-29fix typo in {Rc, Arc}::get_mut_unchecked docsy21-1/+1
2023-01-23Rollup merge of #106854 - steffahn:drop_linear_arc_rebased, r=Mark-SimulacrumDylan DPC-0/+145
Add `Arc::into_inner` for safely discarding `Arc`s without calling the destructor on the inner type. ACP: rust-lang/libs-team#162 Reviving #79665. I want to get this merged this time; this does not contain changes (apart from very minor changes in comments/docs). See #79665 for further description of the PR. The only “unresolved” points that led to that PR being closed, AFAICT, were * The desire to also implement a `Rc::into_inner` function * however, this can very well also happen as a subsequent PR * Possible need for further discussion on the naming “`into_inner`” (?) * `into_inner` seems fine to me; also, this PR introduces unstable API, and names can be changed later, too * ~~I don't know if a tracking issue for the feature flag is supposed to be opened before or after this PR gets merged (if *before*, then I can add the issue number to the `#[unstable…]` attribute)~~ There is a [tracking issue](https://github.com/rust-lang/rust/issues/106894) now. I say “unresolved” in quotation marks because from my point of view, if reviewers agree, the PR can be merged immediately and as-is :-)
2023-01-22Add Arc::into_inner for safely discarding Arcs without calling the ↵Frank Steffahn-0/+145
destructor on the inner type. Mainly rebased and squashed from PR rust-lang/rust#79665, furthermore includes minor changes in comments.
2023-01-14Remove various double spaces in source comments.André Vennberg-4/+4
2023-01-08Auto merge of #90291 - geeklint:loosen_weak_debug_bound, r=dtolnaybors-1/+1
Loosen the bound on the Debug implementation of Weak. Both `rc::Weak<T>` and `sync::Weak<T>` currently require `T: Debug` in their own `Debug` implementations, but they don't currently use it; they only ever print a fixed string. A general implementation of Debug for Weak that actually attempts to upgrade and rely on the contents is unlikely in the future because it may have unbounded recursion in the presence of reference cycles, which Weak is commonly used in. (This was the justification for why the current implementation [was implemented the way it is](https://github.com/rust-lang/rust/pull/19388/commits/f0976e2cf3f6b0027f118b791e0888b29fbb41a7)). When I brought it up [on the forum](https://internals.rust-lang.org/t/could-the-bound-on-weak-debug-be-relaxed/15504), it was suggested that, even if an implementation is specialized in the future that relies on the data stored within the Weak, it would likely rely on specialization anyway, and could therefore easily specialize on the Debug bound as well.
2022-12-19Update coerce_unsized tracking issue from #27732 to #18598Anders Kaseorg-2/+2
Issue #27732 was closed as a duplicate of #18598. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-11-20Rollup merge of #101310 - zachs18:rc_get_unchecked_mut_docs_soundness, ↵Matthias Krüger-4/+37
r=Mark-Simulacrum Clarify and restrict when `{Arc,Rc}::get_unchecked_mut` is allowed. (Tracking issue for `{Arc,Rc}::get_unchecked_mut`: #63292) (I'm using `Rc` in this comment, but it applies for `Arc` all the same). As currently documented, `Rc::get_unchecked_mut` can lead to unsoundness when multiple `Rc`/`Weak` pointers to the same allocation exist. The current documentation only requires that other `Rc`/`Weak` pointers to the same allocation "must not be dereferenced for the duration of the returned borrow". This can lead to unsoundness in (at least) two ways: variance, and `Rc<str>`/`Rc<[u8]>` aliasing. ([playground link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d7e2d091c389f463d121630ab0a37320)). This PR changes the documentation of `Rc::get_unchecked_mut` to restrict usage to when all `Rc<T>`/`Weak<T>` have the exact same `T` (including lifetimes). I believe this is sufficient to prevent unsoundness, while still allowing `get_unchecked_mut` to be called on an aliased `Rc` as long as the safety contract is upheld by the caller. ## Alternatives * A less strict, but still sound alternative would be to say that the caller must only write values which are valid for all aliased `Rc`/`Weak` inner types. (This was [mentioned](https://github.com/rust-lang/rust/issues/63292#issuecomment-568284090) in the tracking issue). This may be too complicated to clearly express in the documentation. * A more strict alternative would be to say that there must not be any aliased `Rc`/`Weak` pointers, i.e. it is required that get_mut would return `Some(_)`. (This was also mentioned in the tracking issue). There is at least one codebase that this would cause to become unsound ([here](https://github.com/kaimast/lsm-rs/blob/be5a164d770d850d905e510e2966ad4b1cc9aa5e/src/memtable.rs#L166), where additional locking is used to ensure unique access to an aliased `Rc<T>`; I saw this because it was linked on the tracking issue).
2022-11-18Change undefined-behavior doctests from ignore to no_run.Zachary S-2/+2
2022-11-18Apply suggestions from code review zachs18-1/+1
Fix spelling error.
2022-11-18Add examples to show when `{Arc,Rc}::get_mut_unchecked` is disallowed.Zachary S-0/+32
2022-11-18Clarify and restrict when `{Arc,Rc}::get_mut_unchecked` is allowed.Zachary S-4/+5
2022-11-18Remove Vec/Rc storage reuse optclubby789-54/+6
2022-11-14Add `Vec` storage optimization to `Arc` and add testsclubby789-19/+66
2022-10-26ptr::eq: clarify that comparing dyn Trait is fragileRalf Jung-5/+5
2022-10-21Reduce mutability in std-use of with_metadata_ofAndreas Molzer-1/+1
2022-10-03Rollup merge of #98218 - kpreid:nostdarc, r=joshtriplettMatthias Krüger-0/+9
Document the conditional existence of `alloc::sync` and `alloc::task`. `alloc` declares ```rust #[cfg(target_has_atomic = "ptr")] pub mod sync; ``` but there is no public documentation of this condition. This PR fixes that, so that users of `alloc` can understand how to make their code compile everywhere `alloc` does, if they are writing a library with impls for `Arc`. The wording is copied from `std::sync::atomic::AtomicPtr`, with additional advice on how to `#[cfg]` for it. I feel quite uncertain about whether the paragraph I added to `Arc`'s documentation should actually be there, as it is a distraction for anyone using `std`. On the other hand, maybe more reminders that no_std exists would benefit the ecosystem. Note: `target_has_atomic` is [stabilized](https://github.com/rust-lang/rust/issues/32976) but [not yet documented in the reference](https://github.com/rust-lang/reference/pull/1171).
2022-10-02Rollup merge of #102098 - xfix:weak-upgrade-fetch-update, r=Mark-SimulacrumDylan DPC-23/+16
Use fetch_update in sync::Weak::upgrade Using `fetch_update` makes it more clear that it's CAS loop then manually implementing one.
2022-09-26remove cfg(bootstrap)Pietro Albini-1/+0
2022-09-21Use fetch_update in sync::Weak::upgradeKonrad Borowski-23/+16
2022-08-22Move error trait into coreJane Losare-Lusby-0/+22
2022-07-10Use `byte_sub` in [a]rc implMaybe Waffle-6/+5
2022-06-21clarify Arc::clone overflow check commentRalf Jung-8/+9
2022-06-20Rollup merge of #96609 - ibraheemdev:arc-downcast-unchecked, r=m-ou-seDylan DPC-3/+42
Add `{Arc, Rc}::downcast_unchecked` Part of #90850.
2022-06-17Document the conditional existence of `alloc::sync` and `alloc::task`.Kevin Reid-0/+9
The wording is copied from `std::sync::atomic::AtomicPtr`, with additional advice on how to `#[cfg]` for it.
2022-06-01Update sync.rsDylan DPC-1/+1
2022-06-01Update sync.rsDylan DPC-3/+3
2022-06-01Spelling correction.Ximon Eighteen-1/+1
2022-05-29Use Box::new() instead of box syntax in alloc testsest31-4/+4
2022-05-06Auto merge of #95183 - ibraheemdev:arc-count-acquire, r=Amanieubors-6/+6
Weaken needlessly restrictive orderings on `Arc::*_count` There is no apparent reason for these to be `SeqCst`. For reference, [the Boost C++ implementation relies on acquire semantics](https://github.com/boostorg/smart_ptr/blob/f2cc84a23c64b8a73c9b72b34799d0854d7e0787/include/boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp#L137-L140).
2022-05-05Rollup merge of #95843 - GuillaumeGomez:improve-new-cyclic-doc, r=m-ou-seMatthias Krüger-9/+22
Improve Rc::new_cyclic and Arc::new_cyclic documentation Fixes https://github.com/rust-lang/rust/issues/95672. cc `@CAD97` (since I used your explanations)
2022-05-04Improve Rc::new_cyclic and Arc::new_cyclic documentationGuillaume Gomez-9/+22
2022-05-01add `{Arc, Rc}::downcast_unchecked`Ibraheem Ahmed-3/+42
2022-04-30Bump shared_from_str to Rust 1.62.0David Tolnay-1/+1
2022-04-15Implement str to [u8] conversion for refcounted containersJohn-John Tedro-0/+19
2022-04-07hide an #[allow] directive from the Arc::new_cyclic doc exampleJack O'Connor-1/+1
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-1/+1
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-28Auto merge of #95249 - HeroicKatora:set-ptr-value, r=dtolnaybors-3/+4
Refactor set_ptr_value as with_metadata_of Replaces `set_ptr_value` (#75091) with methods of reversed argument order: ```rust impl<T: ?Sized> *mut T { pub fn with_metadata_of<U: ?Sized>(self, val: *mut U) -> *mut U; } impl<T: ?Sized> *const T { pub fn with_metadata_of<U: ?Sized>(self, val: *const U) -> *const U; } ``` By reversing the arguments we achieve several clarifications: - The function closely resembles `cast` with an argument to initialize the metadata. This is easier to teach and answers a long outstanding question that had restricted cast to `Sized` pointee targets. See multiples reviews of <https://github.com/rust-lang/rust/pull/47631> - The 'object identity', in the form of provenance, is now preserved from the receiver argument to the result. This helps explain the method as a builder-style, instead of some kind of setter that would modify something in-place. Ensuring that the result has the identity of the `self` argument is also beneficial for an intuition of effects. - An outstanding concern, 'Correct argument type', is avoided by not committing to any specific argument type. This is consistent with cast which does not require its receiver to be a 'raw address'. Hopefully the usage examples in `sync/rc.rs` serve as sufficient examples of the style to convince the reader of the readability improvements of this style, when compared to the previous order of arguments. I want to take the opportunity to motivate inclusion of this method _separate_ from metadata API, separate from `feature(ptr_metadata)`. It does _not_ involve the `Pointee` trait in any form. This may be regarded as a very, very light form that does not commit to any details of the pointee trait, or its associated metadata. There are several use cases for which this is already sufficient and no further inspection of metadata is necessary. - Storing the coercion of `*mut T` into `*mut dyn Trait` as a way to dynamically cast some an arbitrary instance of the same type to a dyn trait instance. In particular, one can have a field of type `Option<*mut dyn io::Seek>` to memorize if a particular writer is seekable. Then a method `fn(self: &T) -> Option<&dyn Seek>` can be provided, which does _not_ involve the static trait bound `T: Seek`. This makes it possible to create an API that is capable of utilizing seekable streams and non-seekable streams (instead of a possible less efficient manner such as more buffering) through the same entry-point. - Enabling more generic forms of unsizing for no-`std` smart pointers. Using the stable APIs only few concrete cases are available. One can unsize arrays to `[T]` by `ptr::slice_from_raw_parts` but unsizing a custom smart pointer to, e.g., `dyn Iterator`, `dyn Future`, `dyn Debug`, can't easily be done generically. Exposing `with_metadata_of` would allow smart pointers to offer their own `unsafe` escape hatch with similar parameters where the caller provides the unsized metadata. This is particularly interesting for embedded where `dyn`-trait usage can drastically reduce code size.
2022-03-23Refactor set_ptr_value as with_metadata_ofAndreas Molzer-3/+4
By reversing the arguments we achieve several clarifications: - The function closely resembles `cast` but with an argument to initialized the metadata. This is easier to teach and answers an long outstanding question that had restricted cast to `Sized` targets initially. See multiples reviews of <https://github.com/rust-lang/rust/pull/47631> - The 'object identity', in the form or provenance, is now preserved from the call receiver to the result. This helps explain the method as a builder-style, instead of some kind of setter that would modify something in-place. Ensuring that the result has the identity of the `self` argument is also beneficial for an intuition of effects. - An outstanding concern, 'Correct argument type', is avoided by not committing to any specific argument type. This is consistent with cast which does not require its receiver to be a raw address.
2022-03-21weaken needlessly restrictive orderings on `Arc::*_count`Ibraheem Ahmed-6/+6
2022-03-18Make Weak::new constCAD97-2/+3
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-2/+2
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-03Add tracking issue and impl for `Rc`.Richard Dodd-1/+1
2022-02-03impl `Arc::unwrap_or_clone`Richard Dodd-0/+35
The function gets the inner value, cloning only if necessary.
2022-01-23Rollup merge of #90666 - bdbai:arc_new_cyclic, r=m-ou-seMatthias Krüger-14/+35
Stabilize arc_new_cyclic This stabilizes feature `arc_new_cyclic` as the implementation has been merged for one year and there is no unresolved questions. The FCP is not started yet. Closes #75861 . ``@rustbot`` label +T-libs-api
2022-01-22Update stabilization version of arc_new_cyclicMara Bos-1/+1