使用移动后,由于要求相互矛盾,我无法推断出适当的寿命。

发布于 2025-01-23 19:07:29 字数 3061 浏览 5 评论 0原文

我的结构问题加入。

use web_sys::{Document, Element, window};
use wasm_bindgen::{UnwrapThrowExt, JsCast};
use std::cell::RefCell;
use wasm_bindgen::closure::Closure;
use web_sys::console::log_1;


pub struct Draggable2 {
    doc: Document,
    pub(crate) body: Element,
    prev: Option<Element>,
    pub(crate) curr: Option<Element>,
    dragged: Option<Element>,
}

impl Draggable2 {
    pub(crate) fn new() {
        let doc = window().unwrap_throw().document().unwrap_throw();
        let body = doc.query_selector("body").unwrap_throw().unwrap_throw();
        let _self = &mut Draggable2 { doc, body, prev: None, curr: None, dragged: None };
        _self.drag_handler();
    }
}

的Clouse内部使用self.curr时,

use web_sys::console::log_1;

use crate::editor::plugins::draggable_2::draggable_2::Draggable2;
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;

impl<'a> Draggable2 {
    pub fn drag_handler(&'a mut self) {

        let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
            log_1(&format!("{:#?}", self.curr).into());
self.curr = get_element(event.psge_x(),event.page_y())
        }) as Box<dyn FnMut(_)>);

        self.body.add_event_listener_with_callback("drop", &closure.as_ref().unchecked_ref());
        closure.forget();
    }
}

当我使用移动完整错误消息

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:46
   |
13 |           let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |  ______________________________________________^
14 | |             log_1(&format!("{:#?}", self.curr).into());
15 | |         }) as Box<dyn FnMut(_)>);
   | |_________^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
  --> src/editor/plugins/draggable_2/drag_handler.rs:7:6
   |
7  | impl<'a> Draggable2 {
   |      ^^
note: ...so that the types are compatible
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:46
   |
13 |           let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |  ______________________________________________^
14 | |             log_1(&format!("{:#?}", self.curr).into());
15 | |         }) as Box<dyn FnMut(_)>);
   | |_________^
   = note: expected `(&mut Draggable2,)`
              found `(&'a mut Draggable2,)`
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:23
   |
13 |         let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |                       ^^^^^^^^^^^^^
   = note: expected `<dyn FnMut(DragEvent) as WasmClosure>`
              found `<(dyn FnMut(DragEvent) + 'static) as WasmClosure>`

my struct

use web_sys::{Document, Element, window};
use wasm_bindgen::{UnwrapThrowExt, JsCast};
use std::cell::RefCell;
use wasm_bindgen::closure::Closure;
use web_sys::console::log_1;


pub struct Draggable2 {
    doc: Document,
    pub(crate) body: Element,
    prev: Option<Element>,
    pub(crate) curr: Option<Element>,
    dragged: Option<Element>,
}

impl Draggable2 {
    pub(crate) fn new() {
        let doc = window().unwrap_throw().document().unwrap_throw();
        let body = doc.query_selector("body").unwrap_throw().unwrap_throw();
        let _self = &mut Draggable2 { doc, body, prev: None, curr: None, dragged: None };
        _self.drag_handler();
    }
}

problem accor when I use self.curr inside the clouse with move

use web_sys::console::log_1;

use crate::editor::plugins::draggable_2::draggable_2::Draggable2;
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;

impl<'a> Draggable2 {
    pub fn drag_handler(&'a mut self) {

        let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
            log_1(&format!("{:#?}", self.curr).into());
self.curr = get_element(event.psge_x(),event.page_y())
        }) as Box<dyn FnMut(_)>);

        self.body.add_event_listener_with_callback("drop", &closure.as_ref().unchecked_ref());
        closure.forget();
    }
}

full error message.

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:46
   |
13 |           let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |  ______________________________________________^
14 | |             log_1(&format!("{:#?}", self.curr).into());
15 | |         }) as Box<dyn FnMut(_)>);
   | |_________^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
  --> src/editor/plugins/draggable_2/drag_handler.rs:7:6
   |
7  | impl<'a> Draggable2 {
   |      ^^
note: ...so that the types are compatible
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:46
   |
13 |           let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |  ______________________________________________^
14 | |             log_1(&format!("{:#?}", self.curr).into());
15 | |         }) as Box<dyn FnMut(_)>);
   | |_________^
   = note: expected `(&mut Draggable2,)`
              found `(&'a mut Draggable2,)`
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/editor/plugins/draggable_2/drag_handler.rs:13:23
   |
13 |         let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
   |                       ^^^^^^^^^^^^^
   = note: expected `<dyn FnMut(DragEvent) as WasmClosure>`
              found `<(dyn FnMut(DragEvent) + 'static) as WasmClosure>`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2025-01-30 19:07:29

因为self是一个参考,self.curr尽管移动注释之前,也不会移动到关闭中。因此,关闭必须捕获参考,即&amp; self.curr。此参考的寿命为'a,因此封闭也继承了该寿命。但是关闭:: wrap期望用'static的寿命封闭,因此“永远”如此有效。

您的问题不包含有关如何解决此问题的足够信息。可能是,将克隆转移到关闭中就足够了

    let captured_curr = self.curr.clone();
    let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
        log_1(&format!("{:#?}", captured_curr).into());
    }) as Box<dyn FnMut(_)>);

,或者您必须以某种方式与闭合共享当前值,例如,通过将self.curr 的类型更改为共享rc&lt; refcell&lt;选项&lt; element&gt;

Because self is a reference, self.curr will not get moved into the closure, despite the move annotation before it. So the closure has to capture a reference, i.e. &self.curr. The lifetime of this reference is 'a, so the closure also inherits that lifetime. But Closure::wrap expects a closure with a lifetime of 'static, so valid "forever".

Your question doesn't contain enough information on how to solve this. It might be that moving a clone into the closure is sufficient

    let captured_curr = self.curr.clone();
    let closure = Closure::wrap(Box::new(move |event: web_sys::DragEvent| {
        log_1(&format!("{:#?}", captured_curr).into());
    }) as Box<dyn FnMut(_)>);

or you have to somehow share the current value with the closure, for example by changing the type of self.curr to a shareable Rc<RefCell<Option<Element>>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文