You only need Arc
rather than Rc
if you are accessing data in different threads, but when you obtain a weak references to the Arc
it is not thread-safe, so you can’t store it in any data structure that is accessed by the thread.
This seems to make the downgrade
method pretty pointless, because I can only get and drop a weak reference within my function, and within my function I am not likely to drop the last strong reference anyhow.
Lets say I wanted to implement a doubly linked thread-safe list, with strong forward references and weak back references, it looks like I can’t do this because the weak references are not thread-safe.
My code was compiling and working as expected until I tried to include my new struct in data that was passed to a thread, and then the compiler gave me an error saying that Weak
is not Send
.
— Answer —
This question was closed, so I can’t post this as an answer, but if you find yourself in this situation, I did figure out what happened.
I initially wrote my code using Rc
and all was working fine. When I wanted to pass the data to a thread, I switched my implementation from Rc
to Arc
, but my code still had rc::Weak
in the use
statement.
In short, there is a sync::Weak
and an rc:Weak
type. If you switch your implementation from Rc
to Arc
to support multi-threading, be sure to update your use
statement to refer to the thread-safe version of Weak
.
4