Moving Mutex Ncurses arc downstream still doesn't implement dispatch

I am creating an ncurses frontend for a little thing that I am working on. For input, I want to read it without blocking. I figured I could wrap the WINDOW in Arc Mutex, but that doesn't seem to work as it still complains about submitting. Is it because the ncurses implementation is unsafe? How can I solve this? Ideally, I would have this work with a callback instead of tx, so I can cut the view dependency down to the stack, but I couldn't get this closure to send.

I am using this library: https://github.com/jeaye/ncurses-rs

Simplified code:

pub struct View {
    max_x: i32,
    max_y: i32,
    messages_window: WINDOW,
    input_window: Arc<Mutex<WINDOW>>
}

pub fn init(&mut self, input_tx: mpsc::Sender<DispatchMessage>) {
        let input_window = self.input_window.clone();
        thread::spawn(move || {
            loop {
                let input_window = input_window.lock().unwrap();
                draw_prompt(input_window);
                let input = prompt_input(input_window);
                input_tx.send(input_to_message(input)).unwrap();
            }
        });
}

fn prompt_input(input: WINDOW) -> String {
    let mut string = String::new();
    wgetstr(input, &mut string);
    string
}

fn draw_prompt(input: WINDOW) {
    wclear(input);
    let top = 0 as chtype;
    let bottom = ' ' as chtype;
    let empty = ' ' as chtype;
    wborder(input, empty,empty,top,bottom,empty,empty,empty,empty);
    mvwprintw(input, 1, 1, ">> ");
    wrefresh(input);
}

      

And the errors I am getting:

src/view.rs:40:33: 40:45 error: mismatched types:
 expected `*mut ncurses::ll::WINDOW_impl`,
    found `std::sync::mutex::MutexGuard<'_, *mut ncurses::ll::WINDOW_impl>`
(expected *-ptr,
    found struct `std::sync::mutex::MutexGuard`) [E0308]
src/view.rs:40                     draw_prompt(input_window);
                                               ^~~~~~~~~~~~
note: in expansion of closure expansion
src/view.rs:37:27: 44:14 note: expansion site
src/view.rs:40:33: 40:45 help: run `rustc --explain E0308` to see a detailed explanation
src/view.rs:41:46: 41:58 error: mismatched types:
 expected `*mut ncurses::ll::WINDOW_impl`,
    found `std::sync::mutex::MutexGuard<'_, *mut ncurses::ll::WINDOW_impl>`
(expected *-ptr,
    found struct `std::sync::mutex::MutexGuard`) [E0308]
src/view.rs:41                     let input = prompt_input(input_window);
                                                            ^~~~~~~~~~~~
note: in expansion of closure expansion
src/view.rs:37:27: 44:14 note: expansion site
src/view.rs:41:46: 41:58 help: run `rustc --explain E0308` to see a detailed explanation
src/view.rs:37:13: 37:26 error: the trait `core::marker::Send` is not implemented for the type `*mut ncurses::ll::WINDOW_impl` [E0277]
src/view.rs:37             thread::spawn(move || {
                           ^~~~~~~~~~~~~
src/view.rs:37:13: 37:26 note: `*mut ncurses::ll::WINDOW_impl` cannot be sent between threads safely
src/view.rs:37             thread::spawn(move || {
                           ^~~~~~~~~~~~~
error: aborting due to 3 previous errors

      

Manually dereferencing the window removes type errors, but I figured I'd leave it as it might be a sign of what is wrong.

+3


source to share


1 answer


Arc<T>

implements Send

where T

implements both Send

and Sync

. Mutex<T>

implements Send

and Sync

where T

implements Send

. Thus, it Arc<Mutex<T>>

only implements Send

if it does . Remember what "Types that can be passed across stream boundaries" means. allows its content to be accessed from multiple threads just by taking care of ownership and mutability issues, so if the underlying type cannot be pushed across thread boundaries it won't help. You may need to do all ncurses operations from one thread.T

Send

Send

Arc<Mutex<T>>



Raw pointers are not explicitly implemented Send

because there can be no guarantees. You can create types on top of these that explicitly implement Send

, thereby ensuring that the contained raw pointer is in fact safe to flow across stream boundaries.

+4


source







All Articles