rust/library/alloc/src
Matthias Krüger 87b3671ce9
Rollup merge of #134496 - DiuDiu777:fix-doc, r=ibraheemdev
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-16 17:00:45 +01:00
..
boxed replace placeholder version 2024-11-27 12:10:21 +00:00
collections Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
ffi docs: inline alloc::ffi::c_str types to alloc::ffi 2024-12-28 15:42:39 +01:00
raw_vec Polymorphize RawVec 2024-08-09 20:06:26 -04:00
rc Use c"lit" for CStrings without unwrap 2024-12-02 18:16:36 +00:00
testing Reformat use declarations. 2024-07-29 08:26:52 +10:00
vec Add another Vec::splice example 2025-01-13 17:37:09 -06:00
alloc.rs turn rustc_box into an intrinsic 2025-01-03 12:01:31 +01:00
borrow.rs remove const_cow_is_borrowed feature gate 2024-10-12 19:48:28 +02:00
boxed.rs update cfg(bootstrap) 2025-01-08 21:26:39 +01:00
fmt.rs Added struct fmt::FormattingOptions 2024-12-05 21:48:01 +01:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Rollup merge of #135347 - samueltardieu:push-qvyxtxsqyxyr, r=jhpratt 2025-01-11 01:55:09 -05:00
macros.rs Update the explanation for why we use box_new in vec! 2025-01-12 13:17:16 -05:00
raw_vec.rs Update a bunch of library types for MCP807 2025-01-09 23:47:11 -08:00
rc.rs Make UniqueRc invariant for soundness 2025-01-11 22:36:25 +01:00
slice.rs Change GetManyMutError to match T-libs-api decision 2024-11-28 23:34:47 +02:00
str.rs Inline str::repeat 2024-11-06 18:54:50 +00:00
string.rs Impl String::into_chars 2024-12-22 19:48:36 +08:00
sync.rs Rollup merge of #134496 - DiuDiu777:fix-doc, r=ibraheemdev 2025-01-16 17:00:45 +01:00
task.rs Stabilize noop_waker 2024-12-05 14:14:17 -08:00