首页 > 解决方案 > Is there any way to convert a Rc into an Arc?

问题描述

I have a graph node:

pub struct GraphNode<T>
where
    T: Clone,
{
    pub f: GraphLikeFunc<T>,
    pub m: String,
    pub children: Vec<Rc<GraphNode<T>>>,
    id: Uuid,
}

I'm trying to convert it to:

pub struct ConcurrentGraphNode<T>
where
    T: Clone,
{
    pub f: GraphLikeFunc<T>,
    pub m: String,
    pub children: Vec<Arc<ConcurrentGraphNode<T>>>,
    id: Uuid,
}

标签: concurrencyrust

解决方案


You cannot do such a thing without rebuilding the whole graph. The main difference between Rc and Arc is that Rc does not implement Send and Sync while Arc does.

Those guarantees are checked at compile-time, so there is no way to switch directly between those two at runtime: you have to consume your GraphNode to build a ConcurrentGraphNode from scratch.


推荐阅读