在封闭参数中键入不匹配+关闭寿命

发布于 2025-02-11 05:39:29 字数 4191 浏览 3 评论 0 原文

在紫杉中,我试图将回调绑定到窗口大小事件,触发 msg :: resize update。我遇到了:E0631在闭合参数中类型不

s

  • 匹配 “ rel =“ nofollow noreferrer”> https://github.com/jamesmcguigan/fractals/blob/blob/bd1fb20e138696fc2af9b121a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a132528336b6b6b6b622 andsrc/src/src/components/compon/compon/conters

use gloo_console::log;
use gloo_events::EventListener;
use yew::prelude::*;

pub struct CanvasQuestion {}
pub enum Msg { Resize }

impl Component for CanvasQuestion {
    type Message = Msg;
    type Properties = ();

    fn create(_ctx: &Context<Self>) -> Self {
        Self { }
    }

    fn view(&self, _ctx: &Context<Self>) -> Html {
        html! { <canvas id="canvas"/> }
    }

    fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
        if first_render {
            // WORKS
            ctx.link().send_message(Msg::Resize);     
            
            // found signature of `fn(yew::Event) 
            // let on_window_resize = |_event: Event| {  // BROKEN
            let on_window_resize = |_event: &Event| {    // WORKS
                ctx.link().send_message(Msg::Resize);
            };

            // expected signature of `for<'r> fn(&'r yew::Event)
            EventListener::new( &web_sys::window().unwrap(),
                                "resize", on_window_resize );
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::Resize => { true }  // rerender
        }
    }
}

storm

替换 on_window_resize = | _ event:event | on_window_resize = | _event:&amp; event | 修复了以下错误

error[E0631]: type mismatch in closure arguments
   --> src/components/canvas_question.rs:68:43
    |
64  |  let on_window_resize = |_event: Event| {
    |                         --------------- found signature of `fn(yew::Event) -> _`
...
67  |  EventListener::new( &web_sys::window().unwrap(),
    |  ------------------ required by a bound introduced by this call
68  |  "resize", on_window_resize );
    |            ^^^^^^^^^^^^^^^^ expected signature of `for<'r> fn(&'r yew::Event) -> _`
    |
note: required by a bound in `EventListener::new`
   --> /home/jamie/.cargo/registry/src/github.com-1ecc6299db9ec823/gloo-events-0.1.2/src/lib.rs:338:12
    |
338 |  F: FnMut(&Event) + 'static,
    |     ^^^^^^^^^^^^^ required by this bound in `EventListener::new`

,但会暴露出试图访问访问 ctx.link的Rust Lifetime问题()从闭合内部。

error[E0521]: borrowed data escapes outside of associated function
  --> src/components/fractal.rs:70:28
   |
51 |  fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
   |                         ---  - let's call the lifetime of this reference `'1`
   |                         |
   |                         `ctx` is a reference that is only valid in the associated function body
...
70 |               let listener = EventListener::new( &web_sys::window().unwrap(),
   |  ____________________________^
71 | |                                                "resize", on_window_resize );
   | |                                                                           ^
   | |                                                                           |
   | |___________________________________________________________________________`ctx` escapes the associated function body here
   |                                                                             argument requires that `'1` must outlive `'static`
  • 有没有一种方法来定义 ctx的静态寿命:上下文
  • ctx.link()触发内部消息传递的最佳方法吗?
  • 直接调用 self.update(msg :: resize)直接调用 self.update(self> self.update)会更容易吗?
  • ctx.link()是否需要在canvasquestion结构中存储为 self.link
  • 编写和绑定 on_window_resize()函数的正确方法是什么?

In Yew, I am attempting to bind a callback to the window resize event, triggering a Msg::Resize update. I have encountered: E0631 Type mismatch in closure arguments.`

Expanded version of the code can be found here:

This is the minimalist test case:

use gloo_console::log;
use gloo_events::EventListener;
use yew::prelude::*;

pub struct CanvasQuestion {}
pub enum Msg { Resize }

impl Component for CanvasQuestion {
    type Message = Msg;
    type Properties = ();

    fn create(_ctx: &Context<Self>) -> Self {
        Self { }
    }

    fn view(&self, _ctx: &Context<Self>) -> Html {
        html! { <canvas id="canvas"/> }
    }

    fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
        if first_render {
            // WORKS
            ctx.link().send_message(Msg::Resize);     
            
            // found signature of `fn(yew::Event) 
            // let on_window_resize = |_event: Event| {  // BROKEN
            let on_window_resize = |_event: &Event| {    // WORKS
                ctx.link().send_message(Msg::Resize);
            };

            // expected signature of `for<'r> fn(&'r yew::Event)
            EventListener::new( &web_sys::window().unwrap(),
                                "resize", on_window_resize );
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::Resize => { true }  // rerender
        }
    }
}

Update

Replacing on_window_resize = |_event: Event| with on_window_resize = |_event: &Event| fixes the below errors

error[E0631]: type mismatch in closure arguments
   --> src/components/canvas_question.rs:68:43
    |
64  |  let on_window_resize = |_event: Event| {
    |                         --------------- found signature of `fn(yew::Event) -> _`
...
67  |  EventListener::new( &web_sys::window().unwrap(),
    |  ------------------ required by a bound introduced by this call
68  |  "resize", on_window_resize );
    |            ^^^^^^^^^^^^^^^^ expected signature of `for<'r> fn(&'r yew::Event) -> _`
    |
note: required by a bound in `EventListener::new`
   --> /home/jamie/.cargo/registry/src/github.com-1ecc6299db9ec823/gloo-events-0.1.2/src/lib.rs:338:12
    |
338 |  F: FnMut(&Event) + 'static,
    |     ^^^^^^^^^^^^^ required by this bound in `EventListener::new`

But exposes rust lifetime issues trying to access ctx.link() from inside the closure.

error[E0521]: borrowed data escapes outside of associated function
  --> src/components/fractal.rs:70:28
   |
51 |  fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
   |                         ---  - let's call the lifetime of this reference `'1`
   |                         |
   |                         `ctx` is a reference that is only valid in the associated function body
...
70 |               let listener = EventListener::new( &web_sys::window().unwrap(),
   |  ____________________________^
71 | |                                                "resize", on_window_resize );
   | |                                                                           ^
   | |                                                                           |
   | |___________________________________________________________________________`ctx` escapes the associated function body here
   |                                                                             argument requires that `'1` must outlive `'static`
  • Is there a way of defining a 'static lifetime for ctx: Context?
  • Is ctx.link() the best way to trigger internal message passing?
  • Would it be easier to call self.update(Msg::Resize) directly instead?
  • Does ctx.link() need to be stored as self.link in the CanvasQuestion struct?
  • What is the correct way to write and bind the on_window_resize() function?

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

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

发布评论

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

评论(1

雨落星ぅ辰 2025-02-18 05:39:29

@dogbert | _EVENT的引用的评论| _Event:&amp; event | filect代码>错误[E0631]:在闭合参数中键入不匹配,butr在最终解决方案中不需要这一点。

RTFM发现手册具有我需要的代码模式,我需要

这使用移动 ctx.link()。callback()按值而不是参考闭合,从而避免了生命的问题。

pub struct CanvasQuestion {
    listener: Option<EventListener>,
}
impl Component for CanvasQuestion {
    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            listener: None,
        }
    }
    fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
        if first_render {
            ctx.link().send_message(Msg::Resize);
            let onresize = ctx.link().callback(|_: Event| Msg::Resize);
            let listener = EventListener::new(
                &web_sys::window().unwrap(),
                "resize",
                move |e| onresize.emit(e.clone())
            );
            self.listener = Some(listener);
        }
    }
}

@Dogbert's comment of passing a reference to |_event: &Event| fixed error[E0631]: type mismatch in closure arguments, butr this turned out not to be needed in the final solution.

RTFM discovered the manual had the code pattern I needed

This uses the move keyword to pass ctx.link().callback() into the closure by value rather than reference, thus avoiding the lifetimes issue.

pub struct CanvasQuestion {
    listener: Option<EventListener>,
}
impl Component for CanvasQuestion {
    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            listener: None,
        }
    }
    fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
        if first_render {
            ctx.link().send_message(Msg::Resize);
            let onresize = ctx.link().callback(|_: Event| Msg::Resize);
            let listener = EventListener::new(
                &web_sys::window().unwrap(),
                "resize",
                move |e| onresize.emit(e.clone())
            );
            self.listener = Some(listener);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文