惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
Docker
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
博客园_首页
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
V
V2EX
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

The Rust Programming Language Forum - Latest posts

What's everyone working on this week (21/2026)? Rust Function Call with one Param Iced + Canvas + Text: How do I create a style for canvas text "Value dropped while borrowed" as a lifetime mismatch Weird `use of moved value` behaviour Slightly surprising behavior of a while loop Clap: how to disable options after a certain positional argument How do i tell rust-analyzer what kind of bracket to use for a macro? Iterator + Borrow Checker: Work around borrowing already borrowed mutable variable Requesting data via mavlink SKILLS.md for Rust development 🧵 Stringlet UTF-8 Hack Option Niche? Iterator + Map - How do divide every element, including the last element, by the last element Why can't we store returned value of a function in a variable if one of the parameters goes out of scope My first crate :D, floop: A more convenient and less error prone replacement for loop `{ select! { .. }}` Sorting is slow because architectures are wrong — zan-sort redesigns the architecture, not the algorithm Iced Font - Create a custom monospace font to display dollar amounts Is the UnsafePinned RFC wrong about being able to return `&mut T` from `get_mut_unchecked`? Fixing Polars 0.37 compilation errors: Hashbrown 0.17 dependency conflict and decimal parsing Any plans for improving error diagnostic in Rust 2.0? How do I build completely offline? Is `&mut T -> &mut ManuallyDrop<T>` well-defined and sound? Would you use this? — fixtura, declarative fake data injection for tests Foreign trait restrictions on native types make generics hard to use Cargo Exclude Directive Compiler reasoning around a modulo counter Multiple mutable references to elements within one vector Handling non-`Send` data in a `Send` closure Review: Static Multi Pool Allocator Rmquickjs - High-level MicroQuickJS bindings for Rust
Arc::increment_strong_count design question (cross-post)
@geebee22 Ge · 2026-04-22 · via The Rust Programming Language Forum - Latest posts

1

Cross-post: This is a cross-post from internals forum. Since it got no replies for a week I think it is safe to cross-post it here, as there will be no discussion overlap.


I was reading docs of Arc::increment_strong_count and in the safety section I found requirement that raw pointer passed to the function must point to valid allocation created by global allocator.

The pointer must have been obtained through Arc::into_raw and must satisfy the same layout requirements specified in Arc::from_raw_in. The associated Arc instance must be valid (i.e. the strong count must be at least 1) for the duration of this method, and ptr must point to a block of memory allocated by the global allocator.

This requirement surprised me, so I looked into implementation and after going though Arc::increment_strong_count_in layer I found that the implementation basically just creates Arc from raw pointer and clones it:

// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
let arc = unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) };
// Now increase refcount, but don't drop new refcount either
let _arc_clone: mem::ManuallyDrop<_> = arc.clone();

Why does the implementation of Arc::increment_strong_count simply not walk pointers and increment strong count directly? Requiring alloc to be passed into Arc::increment_strong_count seams overly constraining, since at no point the allocator is used at all.

I understand why Arc::decrement_strong_count needs access to the allocator, as it needs to drop inner T and ArcInner when strong and weak counters go to 0. Was Arc::increment_strong_count designed in such way to keep symmetry with decrementing part? Or is there some safety invariant that I do not see, that would be violated if Arc::increment_strong_count would call fetch_add on the strong counter directly?

2

I saw your post, and guessed someone better qualified to comment than myself would say something.

But FWIW I think you are right. I may well be wrong though, I haven't implemented Arc yet and thought about it in any detail. I don't have plans to implement it either, as I don't think Arc allocations are typically frequent.

Also, on unstable nightly, the memory could have come from any allocator, not necessarily Global. But that is perhaps some kind of nit-pick.