我对Rust的所有权有意见

q3qa4bjr  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(160)

大家好,我在rust中遇到了一个错误,我尝试在www.example.com lib中调用callbackn函数socket.io,但我不能。rust是调用中的警告错误。我已经尝试了很多方法来调用self var,但错误仍然存在。

我试着这样做,但不工作

self.socket =Some(
            SocketBuilder::new(
                self.host.clone()
            )        
            .on("connect", |_,_|{
                /*if self.debug{
                    println!("conectado!")
                }*/
            })    
            .on("REBOOT", | _,_| {
                self::Reiniciar()     
            })
            .on("message", callback) 

            .on("error", |err, _| eprintln!("Error: {:#?}", err))        
            .connect()        
            .expect("Connection failed")

**self::Reiniciar()**中出现警告错误

我将代码self更改为Game,现在警告另一个错误

self.socket =Some(
            SocketBuilder::new(
                self.host.clone()
            )        
            .on("connect", |_,_|{
                /*if self.debug{
                    println!("conectado!")
                }*/
            })    
            .on("REBOOT", | _,_| {
                Game::Reiniciar()     
            })
            .on("message", callback) 

            .on("error", |err, _| eprintln!("Error: {:#?}", err))        
            .connect()        
            .expect("Connection failed")
        );

我不知道我做错了什么,或者需要做一些事情,现在我正在阅读生 rust 的文档,并没有找到任何解决方案,这个错误。

完整代码

impl  Game {

    fn Reiniciar(&mut self){
        let document = self.html.document.clone();
        document.get_element_by_id("tabuleiro").unwrap().set_inner_html("");

        let cards = document.query_selector_all("#hand > div:not(#wait) ")
            .unwrap()
            .dyn_into::<web_sys::NodeList>()
            .unwrap();

        let mut i = 0;
        while i < 101 {

            cards.item(i)
            .unwrap()
            .dyn_into::<web_sys::HtmlElement>()
            .unwrap()
            .parent_node()
            .unwrap()
            .remove_child( 
                &cards.item(i).unwrap()
            );

            i += 1
        }
    }

    /**
     * THis function  listem the server to make actions on client application
     */
    fn listen(&mut self){

        let callback = | payload: Payload, _socket: Socket| {        
            match payload {            
                 Payload::String(str) => 
                     println!("{}", str[1..str.len() - 1].to_string()),
                 Payload::Binary(bin_data) => 
                     println!("{:?}",bin_data),
            }    
        }; 
  

        self.socket =Some(
            SocketBuilder::new(
                self.host.clone()
            )        
            .on("connect", |_,_|{
                /*if self.debug{
                    println!("conectado!")
                }*/
            })    
            .on("REBOOT", | _,_| {
                Game::Reiniciar()     
            })
            .on("message", callback) 

            .on("error", |err, _| eprintln!("Error: {:#?}", err))        
            .connect()        
            .expect("Connection failed")
        );
    }

    fn new<T: Into<String>, U: Into<bool>>(host: T, debug: U) ->Self{
        let html = html::html::Html::new();

        Self {
            ultimo: -1,
            jogador: -1,
            debug: debug.into(),
            host: host.into(),
            socket: None,
            html
        }
    }

}

我尝试更改对Game::Reiniciar(&mut self)的调用

self.socket =Some(
            SocketBuilder::new(
                self.host.clone()
            )        
            .on("connect", |_,_|{
                /*if self.debug{
                    println!("conectado!")
                }*/
            })    
            .on("REBOOT", | _,_| {
                Game::Reiniciar(&mut self)
            })
            .on("message", callback) 

            .on("error", |err, _| eprintln!("Error: {:#?}", err))        
            .connect()        
            .expect("Connection failed")
        );

它是关于Reiniciar函数调用的警告错误。

error[E0277]: `*mut u8` cannot be sent between threads safely
   --> src/lib.rs:102:27
    |
102 |               .on("REBOOT", | _,_| {
    |                --           ^-----
    |                |            |
    |  ______________|____________within this `[closure@src/lib.rs:102:27: 102:33]`
    | |              |
    | |              required by a bound introduced by this call
103 | |                 Game::Reiniciar(&mut self)
104 | |             })
    | |_____________^ `*mut u8` cannot be sent between threads safely
    |
    = help: within `[closure@src/lib.rs:102:27: 102:33]`, the trait `Send` is not implemented for `*mut u8`
    = note: required because it appears within the type `PhantomData<*mut u8>`
    = note: required because it appears within the type `JsValue`
    = note: required because it appears within the type `Object`
    = note: required because it appears within the type `EventTarget`
    = note: required because it appears within the type `Window`

Image about this error
我试图在wasm编译此代码。但我停止在这个错误。

bnlyeluc

bnlyeluc1#

当调用函数作为方法时,需要使用.而不是::(路径运算符):

self.Reiniciar()

你也可以从路径调用函数,但是你需要传递self作为参数:

Game::Reiniciar(&mut self)

相关问题