What is the difference between Arc and AtomicPtr? Up until now, I thought the only pointer types in Rust are:
a) Box
b) Rc
c) Arc
d) raw "unsafe" ptr
Now, in one of the question on this forum I see that somebody is using AtomicPtr.
Shouldn't Arc be exactly for that purpose? That is, guaranteed atomic access to the memory it points to?
Thanks
The AtomicPtr
type is not really like the other pointer types. It's more comparable to AtomicUsize
and such. You can only really use AtomicPtr
in unsafe code. As for Arc
, it does something completely different than AtomicPtr
.
I believe the swapping functionality in arc-swap
was implemented by putting the raw pointers returned by Arc::into_raw
into an AtomicPtr
.
Not sure about the terminology here.
I previously thought that Box
, Rc
, Arc
, Pin
, etc. are called "smart pointers" instead of pointers. But the respective pages in the docs use the term "pointer". So maybe all of the following are "pointers"?
- raw pointers (
*const
and*mut
) - references (
&
and&mut
) - smart pointers (
Box
,Rc
,Arc
,Pin
, etc.)
Ah, looks like I was right, the subsection on pointer types in the reference distinguishes between:
However, it doesn't give a clear definition of what a "smart pointer" actually is. I think everything that implements Deref
(except references, which also implement Deref
) are to be considered "smart pointers", because the documentation on Deref
says:
Implementing
Deref
for smart pointers makes accessing the data behind them convenient, which is why they implementDeref
. On the other hand, the rules regardingDeref
andDerefMut
were designed specifically to accommodate smart pointers. Because of this,Deref
should only be implemented for smart pointers to avoid confusion.
And Deref
is implemented for a lot of types, for example:
But also for:
I guess that makes String
a smart pointer (to a str
) too, or am I wrong?
Yes, you are wrong.
Smart pointer is a pointer
Completely wrong. You basically have completely wrong understanding of what smart pointer is. Smart pointer unlike "raw/unsafe pointer" is a pointer that frees you from manually managing resources, in a nut shell.
Do you have a reference for that?
Reference for what?
That the types I listed which implement Deref
are not smart pointers (except the reference and mutable reference type).
It is up to you to prove what your theory/guess is.
No it's not. Besides, I included a lot of links to the official reference and/or documentation in my post.
Yes, it is. And the links you provided do not prove your guess. They just prove that those things exist. And "guess what" - that is not the same thing.
Then String is indeed a smart pointer. But you said it isn't.
Wait up a minute:
A pointer, in assembly language and languages lie C, C++, Pascal, etc, is a simple memory address. Basically an integer or unsigned integer big enough to hold all the bits of a memory address.
Meanwhile a "smart pointer" is more than that. A Rust String contains a pointer and a length and a capacity and whatever else. Other smart pointers may include reference counts or whatever else. Smart pointers in other languages do likewise.
I conclude that a "smart pointer" contains a pointer. It is not correct to say it is a pointer. Despite the fact that the language allows the underlying pointer to be dereferenced.
Even ordinary references may be more than just that. E.g. an &[T]
has both a pointer and length, and a &dyn SomeTrait
has both a pointer to the object, and another pointer to a vtable.
With that logic, you can stretch it to say that vector is also a smart pointer.
Good point.
Mind, I don't think of a reference as pointer either.
I guess it's just a pedantic difference. Like how async code has "tasks" not "threads". Sometimes it matters to be precise, often not.
Difference between task and thread is fundamental, not pedantic. Similar to difference between OS and Software.
I would say String is not a Smart Pointer because as well as carrying that pointer, length and capacity in some structure it also allocates the memory used for the content of the string. There is a pointer in there somewhere. Smart or otherwise. But String is about the data it contains.
Yes it is.