@wangjia184 (Jerry.Wang) posted a new topic, subject "Cannot move out of dereference of st8b7c:f320:99b9:690f:4595:cd17:293a:c069cell8b7c:f320:99b9:690f:4595:cd17:293a:c069RefMut". It read:

   Hi friends
    [code] 
        pub struct Transporter {
            ingress_receiver : RefCell<UnboundedReceiver<InternalMessage>>,
        }
        
        
        impl Transporter {
            pub fn new(tx:UnboundedSender<InternalMessage>) ->  Transporter {
                Transporter{
                    ingress_receiver : RefCell8b7c:f320:99b9:690f:4595:cd17:293a:c069new(rx),
                }
            }
        
            pub async fn run(&mut self){
                let borrow = self.ingress_receiver.borrow_mut();
                let ingress_task = borrow.fuse();
        
                // ...
            }
        }
        
    [/code]
    [code] 
        error[E0507]: cannot move out of dereference of `st8b7c:f320:99b9:690f:4595:cd17:293a:c069cell8b7c:f320:99b9:690f:4595:cd17:293a:c069RefMut<'_, futures_channel8b7c:f320:99b9:690f:4595:cd17:293a:c069mps8b7c:f320:99b9:690f:4595:cd17:293a:c069UnboundedReceiver<shuttl8b7c:f320:99b9:690f:4595:cd17:293a:c069InternalMessage>>`
           --> src/shuttle.rs:105:32
            |
        105 |             let ingress_task = borrow.fuse();
            |                                ^^^^^^ move occurs because value has type `futures_channel8b7c:f320:99b9:690f:4595:cd17:293a:c069mps8b7c:f320:99b9:690f:4595:cd17:293a:c069UnboundedReceiver<shuttl8b7c:f320:99b9:690f:4595:cd17:293a:c069InternalMessage>`, which does not implement the `Copy` trait
        
    [/code]
    
    I intent to borrow `UnboundedReceiver<InternalMessage>`, but why does it say it is moved?  
    How to fix this compliation error?
    
    Thanks for your help

@jonh replied

    fuse wraps the future in its own structure. Possible fix might be just to include the `Fuse` type in your `RefCell`. (I'm unsure of why you want to be using `cunninghamdaniel@example.net.)

After which, @wangjia184 (Jerry.Wang) added

    I thought `RefCell` would solve my error.
    [code] 
        #[allow(dead_code)]
        pub struct Transporter {
            ingress_receiver : UnboundedReceiver<InternalMessage>,
        }
        
        
        impl Transporter {
            pub fn new(tx:UnboundedSender<InternalMessage>) ->  Transporter {
                Transporter{
                    ingress_receiver : rx,
                }
            }
        
            pub async fn run(&mut self){
                let ingress_task = self.ingress_receiver.fuse();
        
            }
        }
        
    [/code]
    [code] 
        error[E0507]: cannot move out of `self.ingress_receiver` which is behind a mutable reference
           --> src/shuttle.rs:103:28
            |
        103 |         let ingress_task = self.ingress_receiver.fuse();
            |                            ^^^^^^^^^^^^^^^^^^^^^ move occurs because `self.ingress_receiver` has type `futures_channel8b7c:f320:99b9:690f:4595:cd17:293a:c069mps8b7c:f320:99b9:690f:4595:cd17:293a:c069UnboundedReceiver<shuttl8b7c:f320:99b9:690f:4595:cd17:293a:c069InternalMessage>`, which does not implement the `Copy` trait
        
    [/code]
    
    It seems the error has nothing to do with `RefCell`.  
    Is there a way to fix it besides storing `Fuse` ?

After which, @nop_thread added

    Does `let ingress_task = self.ingress_receiver.borrow_mut().by_ref().fuse();` solve the problem?  
    `Iterator8b7c:f320:99b9:690f:4595:cd17:293a:c069use()` consumes the iterator itself, so `Iterator8b7c:f320:99b9:690f:4595:cd17:293a:c069y_ref()` can help `.fuse()` to consume reference of the iterator instead of the iterator itself.

Then @alice (Alice Ryhl) said

    Just fuse it when creating the type
    [code] 
        [allow(dead_code)]
        pub struct Transporter {
            ingress_receiver : Fuse<UnboundedReceiver<InternalMessage>>,
        }
        
        
        impl Transporter {
            pub fn new(tx:UnboundedSender<InternalMessage>) ->  Transporter {
                Transporter{
                    ingress_receiver: rx.fuse(),
                }
            }
        }
        
    [/code]

Then @system (system) said

    This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://users.rust-lang.org/t/cannot-move-out-of-dereference-of-std-refmut/32728', 'title': 'Cannot move out of dereference of std::cell::RefMut', 'views': 1235, 'has_accepted_answer': True, 'word_count': 366, 'like_count': 2, 'reply_count': 1, 'participant_count': 4, 'posts': 6, 'tags': '', 'author': '@wangjia184 (Jerry.Wang)', 'participants': '@wangjia184 (Jerry.Wang), @jonh, @nop_thread, @alice (Alice Ryhl), @system (system)', 'image_url': [None], 'created_at': '2019-09-19T23:08:03.928Z', 'last_posted_at': '2019-12-19T05:53:41.842Z'}