about summary refs log tree commit diff
path: root/library/alloc/src/sync.rs
AgeCommit message (Collapse)AuthorLines
2025-09-30Rollup merge of #146886 - taiki-e:rc-inner-align, r=Mark-SimulacrumMatthias Krüger-7/+9
Add repr(align(2)) to RcInner and ArcInner `Rc` currently assumes that `RcInner` has at least 2-byte alignment, but on AVR, `usize` has 1-byte alignment (this is because the AVR has 1-byte register sizes, so having 2-byte alignment is generally useless), breaking this assumption. https://github.com/rust-lang/rust/blob/9f32ccf35fb877270bc44a86a126440f04d676d0/library/alloc/src/rc.rs#L3005-L3008 This PR adds `repr(align(2))` to force `RcInner` to always have at least 2-byte alignment. Note that `ArcInner` doesn't need `repr(align(2))` because atomic types have the alignment same as its size. This PR adds a comment about this.
2025-09-30Add repr(align(2)) to RcInner and ArcInnerTaiki Endo-7/+9
2025-09-26Update CURRENT_RUSTC_VERSION post-bumpMark Rousskov-1/+1
2025-09-22Remove unused #![feature(get_mut_unchecked)] in Rc and Arc examplesTaiki Endo-9/+0
2025-09-21Rollup merge of #144091 - thaliaarchi:stabilize-new-zeroed, r=Mark-SimulacrumStuart Cook-12/+2
Stabilize `new_zeroed_alloc` The corresponding `new_uninit` and `new_uninit_slice` functions were stabilized in rust-lang/rust#129401, but the zeroed counterparts were left for later out of a [desire](https://github.com/rust-lang/rust/issues/63291#issuecomment-2161039756) to stabilize only the minimal set. These functions are straightforward mirrors of the uninit functions and well-established. Since no blockers or design questions have surfaced in the past year, I think it's time to stabilize them. Tracking issue: rust-lang/rust#129396
2025-08-26remove deprecated Error::description in implsMarijn Schouten-5/+0
2025-07-19Stabilize `new_zeroed_alloc`Thalia Archibald-12/+2
2025-07-15Add `Default` impls for `Pin`ned `Box`, `Rc`, `Arc`Jules Bertholet-0/+13
2025-07-01Change `{Box,Arc,Rc,Weak}::into_raw` to only work with `A = Global`Amanieu d'Antras-61/+61
Also applies to `Vec::into_raw_parts`. The expectation is that you can round-trip these methods with `from_raw`, but this is only true when using the global allocator. With custom allocators you should instead be using `into_raw_with_allocator` and `from_raw_in`. The implementation of `Box::leak` is changed to use `Box::into_raw_with_allocator` and explicitly leak the allocator (which was already the existing behavior). This is because, for `leak` to be safe, the allocator must not free its underlying backing store. The `Allocator` trait only guarantees that allocated memory remains valid until the allocator is dropped.
2025-06-19Remove incorrect comments in `Weak`Tim (Theemathas) Chirananthavat-1/+0
It is currently possible to create a dangling `Weak` to a DST by calling `Weak::new()` for a sized type, then doing an unsized coercion. Therefore, the comments are wrong. These comments were added in <https://github.com/rust-lang/rust/pull/73845>. As far as I can tell, the guarantee in the comment was only previously used in the `as_ptr` method. However, the current implementation of `as_ptr` no longer relies on this guarantee.
2025-04-28Auto merge of #136316 - GrigorenkoPV:generic_atomic, r=Mark-Simulacrumbors-5/+5
Create `Atomic<T>` type alias (rebase) Rebase of #130543. Additional changes: - Switch from `allow` to `expect` for `private_bounds` on `AtomicPrimitive` - Unhide `AtomicPrimitive::AtomicInner` from docs, because rustdoc shows the definition `pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;` and generated links for it. - `NonZero` did not have this issue, because they kept the new alias private before the direction was changed. - Use `Atomic<_>` in more places, including inside `Once`'s `Futex`. This is possible thanks to https://github.com/rust-lang/rust-clippy/pull/14125 The rest will either get moved back to #130543 or #130543 will be closed in favor of this instead. --- * ACP: https://github.com/rust-lang/libs-team/issues/443#event-14293381061 * Tracking issue: #130539
2025-04-28Rollup merge of #138939 - SabrinaJewson:arc-is-unique, r=tgross35Chris Denton-8/+61
Add `Arc::is_unique` Adds ```rs impl<T> Arc<T> { pub fn is_unique(this: &Self) -> bool; } ``` Tracking issue: #138938 ACP: https://github.com/rust-lang/libs-team/issues/560
2025-04-27Add `Arc::is_unique`SabrinaJewson-8/+61
2025-04-27use generic Atomic type where possibleChristopher Durham-5/+5
in core/alloc/std only for now, and ignoring test files Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-03-22Remove `PartialEq::ne` for `UniqueArc`Frank King-19/+0
2025-03-22Add more APIs for UniqueArcFrank King-5/+250
2025-03-22Make UniqueArc invariant for soundnessFrank King-2/+6
2025-03-22Implement `UniqueArc`Frank King-1/+181
2025-03-17Rollup merge of #138341 - xizheyin:issue-138322, r=joboetJacob Pratt-3/+23
std: Mention clone-on-write mutation in Arc<T> Fixes #138322 r? libs
2025-03-11std: Mention clone-on-write mutation in Arc<T>xizheyin-3/+23
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-03-10fix copy typoLemonJ-2/+2
2025-03-10fix ptr inconsistency in Rc ArcLemonJ-11/+23
2025-03-08Rollup merge of #136642 - bjorn3:separate_alloctest_crate, r=cuviperJacob Pratt-2/+2
Put the alloc unit tests in a separate alloctests package Same rationale as https://github.com/rust-lang/rust/pull/135937. This PR has some extra complexity though as a decent amount of tests are testing internal implementation details rather than the public api. As such I opted to include the modules containing the types under test using `#[path]` into the alloctests package. This means that those modules still need `#[cfg(test)]`, but the rest of liballoc no longer need it.
2025-03-07Fully test the alloc crate through alloctestsbjorn3-2/+2
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2025-03-07Rollup merge of #134797 - spastorino:ergonomic-ref-counting-1, r=nikomatsakisMatthias Krüger-0/+7
Ergonomic ref counting This is an experimental first version of ergonomic ref counting. This first version implements most of the RFC but doesn't implement any of the optimizations. This was left for following iterations. RFC: https://github.com/rust-lang/rfcs/pull/3680 Tracking issue: https://github.com/rust-lang/rust/issues/132290 Project goal: https://github.com/rust-lang/rust-project-goals/issues/107 r? ```@nikomatsakis```
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-2/+2
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-03-06Add UseCloned trait related codeSantiago Pastorino-0/+7
2025-02-08Rustfmtbjorn3-5/+8
2025-01-21add missing allocator safety in alloc crateLemonJ-1/+1
2025-01-16Rollup merge of #134496 - DiuDiu777:fix-doc, r=ibraheemdevMatthias Krüger-2/+6
Update documentation for Arc::from_raw, Arc::increment_strong_count, and Arc::decrement_strong_count to clarify allocator requirement ### Related Issue: This update addresses parts of the issue raised in [#134242](https://github.com/rust-lang/rust/issues/134242), where Arc's documentation lacks `Global Allocator` safety descriptions for three APIs. And this was confirmed by ```@workingjubilee``` : > Wait, nevermind. I apparently forgot the `increment_strong_count` is implicitly A = Global. Ugh. Another reason these things are hard to track, unfortunately. ### PR Description This PR updates the document for the following APIs: - `Arc::from_raw` - `Arc::increment_strong_count` - `Arc::decrement_strong_count` These APIs currently lack an important piece of documentation: **the raw pointer must point to a block of memory allocated by the global allocator**. This crucial detail is specified in the source code but is not reflected in the documentation, which could lead to confusion or incorrect usage by users. ### Problem: The following example demonstrates the potential confusion caused by the lack of documentation: ```rust #![feature(allocator_api)] use std::alloc::{Allocator,AllocError, Layout}; use std::ptr::NonNull; use std::sync::Arc; struct LocalAllocator { memory: NonNull<u8>, size: usize, } impl LocalAllocator { fn new(size: usize) -> Self { Self { memory: unsafe { NonNull::new_unchecked(&mut 0u8 as *mut u8) }, size, } } } unsafe impl Allocator for LocalAllocator { fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, AllocError> { Ok(NonNull::slice_from_raw_parts(self.memory, self.size)) } unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { } } fn main() { let allocator = LocalAllocator::new(64); let arc = Arc::new_in(5, &allocator); // Here, allocator could be any non-global allocator let ptr = Arc::into_raw(arc); unsafe { Arc::increment_strong_count(ptr); let arc = Arc::from_raw(ptr); assert_eq!(2, Arc::strong_count(&arc)); // Failed here! } } ```
2025-01-16fix typo in library/alloc/src/sync.rsClearLove-1/+1
Co-authored-by: Ibraheem Ahmed <ibraheem@ibraheem.ca>
2025-01-10Use `NonNull::without_provenance` within the standard librarySamuel Tardieu-12/+3
This API removes the need for several `unsafe` blocks, and leads to clearer code.
2025-01-08Remove some unnecessary `.into()` callsEsteban Küber-1/+1
2024-12-26Rollup merge of #134379 - bjoernager:slice-as-array, r=dtolnayJacob Pratt-0/+20
Add `into_array` conversion destructors for `Box`, `Rc`, and `Arc`. Tracking issue: #133508 This PR adds the `into_array` destructor for `alloc::boxed::Box<[T]>`, `alloc::rc::Rc<[T]>`, and `alloc::sync::Arc<[T]>`. Note that this PR assumes the initial proposal of these functions returning `Option`. It is still an open question whether this should instead be `Result`. We can, however, easily change this in a follow-up PR with the necessary consensus.
2024-12-19Add missing safety descriptions to Arc's ↵LemonJ-2/+6
'from_raw','increment_strong_count','decrement_strong_count'
2024-12-18Add 'into_array' conversion destructors for 'Box', 'Rc', and 'Arc';Gabriel Bjørnager Jensen-0/+20
2024-12-17Use field init shorthand where possibleJosh Triplett-1/+1
Field init shorthand allows writing initializers like `tcx: tcx` as `tcx`. The compiler already uses it extensively. Fix the last few places where it isn't yet used.
2024-12-04Move some alloc tests to the alloctests cratebjorn3-3/+0
Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap.
2024-11-27update cfgsBoxy-1/+1
2024-11-27replace placeholder versionBoxy-2/+2
2024-11-12Make `CloneToUninit` dyn-compatibleZachary S-1/+1
2024-11-12Rollup merge of #132869 - ↵Matthias Krüger-1/+3
lolbinarycat:library-fix-too_long_first_doc_paragraph, r=tgross35 split up the first paragraph of doc comments for better summaries used `./x clippy -Aclippy::all '-Wclippy::too_long_first_doc_paragraph' library/core library/alloc` to find these issues.
2024-11-11Auto merge of #127589 - notriddle:notriddle/search-sem-3, r=GuillaumeGomezbors-0/+1
rustdoc-search: simplify rules for generics and type params **Heads up!**: This PR is a follow-up that depends on #124544. It adds 12dc24f46007f82b93ed85614347a42d47580afa, a change to the filtering behavior, and 9900ea48b566656fb12b5fcbd0a1b20aaa96e5ca, a minor ranking tweak. Part of https://github.com/rust-lang/rust-project-goals/issues/112 This PR overturns https://github.com/rust-lang/rust/pull/109802 ## Preview * no results: [`Box<[A]> -> Vec<B>`](http://notriddle.com/rustdoc-html-demo-12/search-sem-3/std/index.html?search=Box%3C%5BA%5D%3E%20-%3E%20Vec%3CB%3E) * results: [`Box<[A]> -> Vec<A>`](http://notriddle.com/rustdoc-html-demo-12/search-sem-3/std/index.html?search=Box%3C%5BA%5D%3E%20-%3E%20Vec%3CA%3E) * [`T -> U`](http://notriddle.com/rustdoc-html-demo-12/search-sem-3/std/index.html?search=T%20-%3E%20U) * [`Cx -> TyCtxt`](http://notriddle.com/rustdoc-html-demo-12/search-sem-3-compiler/rustdoc/index.html?search=Cx%20-%3E%20TyCtxt) ![image](https://github.com/user-attachments/assets/015ae28c-7469-4f7f-be03-157d28d7ec97) ## Description This commit is a response to feedback on the displayed type signatures results, by making generics act stricter. - Order within generics is significant. This means `Vec<Allocator>` now matches only with a true vector of allocators, instead of matching the second type param. It also makes unboxing within generics stricter, so `Result<A, B>` only matches if `B` is in the error type and `A` is in the success type. The top level of the function search is unaffected. - Generics are only "unboxed" if a type is explicitly opted into it. References and tuples are hardcoded to allow unboxing, and Box, Rc, Arc, Option, Result, and Future are opted in with an unstable attribute. Search result unboxing is the process that allows you to search for `i32 -> str` and get back a function with the type signature `&Future<i32> -> Box<str>`. - Instead of ranking by set overlap, it ranks by the number of items in the type signature. This makes it easier to find single type signatures like transmute. ## Find the discussion on * <https://rust-lang.zulipchat.com/#narrow/stream/393423-t-rustdoc.2Fmeetings/topic/meeting.202024-07-08/near/449965149> * <https://github.com/rust-lang/rust/pull/124544#issuecomment-2204272265> * <https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/deciding.20on.20semantics.20of.20generics.20in.20rustdoc.20search>
2024-11-10split up the first paragraph of doc comments for better summariesbinarycat-1/+3
2024-11-03Rollup merge of #129329 - eduardosm:rc-from-mut-slice, r=dtolnayMatthias Krüger-0/+40
Implement `From<&mut {slice}>` for `Box/Rc/Arc<{slice}>` ACP: https://github.com/rust-lang/libs-team/issues/424 New API: ```rust impl<T: Clone> From<&mut [T]> for Box<[T]> impl From<&mut str> for Box<str> impl From<&mut CStr> for Box<CStr> impl From<&mut OsStr> for Box<OsStr> impl From<&mut Path> for Box<Path> impl<T: Clone> From<&mut [T]> for Rc<[T]> impl From<&mut str> for Rc<str> impl From<&mut CStr> for Rc<CStr> impl From<&mut OsStr> for Rc<OsStr> impl From<&mut Path> for Rc<Path> impl<T: Clone> From<&mut [T]> for Arc<[T]> impl From<&mut str> for Arc<str> impl From<&mut CStr> for Arc<CStr> impl From<&mut OsStr> for Arc<OsStr> impl From<&mut Path> for Arc<Path> ``` Since they are trait implementations, I think these are insta-stable. As mentioned in https://github.com/rust-lang/libs-team/issues/424#issuecomment-2299415749, a crater run might be needed.
2024-10-30rustdoc-search: simplify rules for generics and type paramsMichael Howell-0/+1
This commit is a response to feedback on the displayed type signatures results, by making generics act stricter. Generics are tightened by making order significant. This means `Vec<Allocator>` now matches only with a true vector of allocators, instead of matching the second type param. It also makes unboxing within generics stricter, so `Result<A, B>` only matches if `B` is in the error type and `A` is in the success type. The top level of the function search is unaffected. Find the discussion on: * <https://rust-lang.zulipchat.com/#narrow/stream/393423-t-rustdoc.2Fmeetings/topic/meeting.202024-07-08/near/449965149> * <https://github.com/rust-lang/rust/pull/124544#issuecomment-2204272265> * <https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/deciding.20on.20semantics.20of.20generics.20in.20rustdoc.20search/near/476841363>
2024-10-29Implement `From<&mut {slice}>` for `Box/Rc/Arc<{slice}>`Eduardo Sánchez Muñoz-0/+40
2024-10-27Rc/Arc: don't leak the allocation if drop panicsLukas Markeffsky-7/+9
2024-10-25library: consistently use American spelling for 'behavior'Ralf Jung-2/+2
2024-10-24Rollup merge of #130225 - adetaylor:rename-old-receiver, r=wesleywiserStuart Cook-3/+3
Rename Receiver -> LegacyReceiver As part of the "arbitrary self types v2" project, we are going to replace the current `Receiver` trait with a new mechanism based on a new, different `Receiver` trait. This PR renames the old trait to get it out the way. Naming is hard. Options considered included: * HardCodedReceiver (because it should only be used for things in the standard library, and hence is sort-of hard coded) * LegacyReceiver * TargetLessReceiver * OldReceiver These are all bad names, but fortunately this will be temporary. Assuming the new mechanism proceeds to stabilization as intended, the legacy trait will be removed altogether. Although we expect this trait to be used only in the standard library, we suspect it may be in use elsehwere, so we're landing this change separately to identify any surprising breakages. It's known that this trait is used within the Rust for Linux project; a patch is in progress to remove their dependency. This is a part of the arbitrary self types v2 project, https://github.com/rust-lang/rfcs/pull/3519 https://github.com/rust-lang/rust/issues/44874 r? `@wesleywiser`