Question on mpsc::channel implementation


Hi Rust experts,

I am trying to understand how mpsc::channels work.

I found that Sender and Receiver is implemented similarly:

pub struct Sender<T> {
    inner: UnsafeCell<Flavor<T>>,

And Flavor is:

enum Flavor<T> {

With my limited understanding I see the role of Arc and UnsafeCell. Based on the above I assume that Sender and Receiver encapsulates a type like UnsafeCell<Arc<UnsafeCell<T>>>.

Can someone please explain what the second, outer UnsafeCell is doing here? Why can’t the Sender be like?:

pub struct Sender<T> {
    inner: Flavor<T>,

Thank you, David


The channel type is changed dynamically, based on how it is used, so the flavour needs to change. The desire is to have this all happen while shared (i.e. inside &self methods, like clone) and so interior mutability is necessary, which is what UnsafeCell provides.


Thank you, makes sense to me now.