-
Hey guys, I'm struggling to get #[derive(Debug, Default, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct Upload {
pub message: String
}
impl Upload {
pub async fn dispatch(&self) -> Result<Leave, crate::ApiError> {
// do the actual API magic here
}
}
// ---
#[component]
pub fn upload_form() -> impl View {
let textedit = RwSignal::new(String::new());
let upload = Action::<Upload, Result<(), crate::ApiError>>::new(async move |upload: &Upload| {
upload.dispatch().await
});
view! {
<textarea value=textedit />
<button on:click=move |_| upload.dispatch(Upload {
message: textedit.get()
}) />
}
} The error I get is this:
Interrestingly, I tried to forego the middle closure by simply passing in // ---
let upload = Action::<Upload, Result<(), crate::ApiError>>::new(Upload::dispatch);
// ---
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The async closure feature maybe giving you a less-than-helpful helpful error here. Yours is the equivalent of this move |upload: &Upload| async move {
upload.dispatch().await
} So you can see the But the function signature is this pub fn new<F, Fu>(action_fn: F) -> Self
where
F: Fn(&I) -> Fu + 'static,
Fu: Future<Output = O> + 'static, i.e., So you need to do something like this move |upload: &Upload| {
let upload = upload.clone();
async move { upload.dispatch().await }
} so that the Alternately, you can design your pub fn dispatch(&self) -> impl Future<Output = ()> + 'static {
let len = self.message.len();
async {
_ = len;
// do something with the length
}
} The reason that you only get tl;dr: just |
Beta Was this translation helpful? Give feedback.
The async closure feature maybe giving you a less-than-helpful helpful error here. Yours is the equivalent of this
So you can see the
&Upload
reference is moved into theasync
block. This means that theasync
block has some lifetime&'a
, which is the lifetime of&'a Upload
.But the function signature is this
i.e.,
Fu
theFuture
needs to be'static
: it cannot hold references to anything outside itself, but needs to own its own data. Ultimately this is a hard requirement of handing theFuture
off towasm-bindgen-fu…