Stream: wit-bindgen

Topic: [rust] param and result structs


view this post on Zulip bachrc (Mar 05 2023 at 16:42):

Hello, I am a little lost with param and result structs generated by wit-bindgen.
Actually, function take in parameters Param structs. Those param struct does not have any ownership on their data, but I don't know how I can construct those param structs before sending them.

view this post on Zulip bachrc (Mar 05 2023 at 16:59):

with param objects i only have those

error[E0515]: cannot return value referencing temporary value
  --> src/file_document.rs:25:9
   |
25 | /         Document {
26 | |             identifier: self.id.to_string().as_str(),
27 | |             fields: &[
28 | |                 Field { name: "filename", value: Text(self.filename.to_string().as_str()) },
29 | |                 Field { name: "path", value: Text(self.path.to_string_lossy().as_ref()) },

view this post on Zulip bachrc (Mar 05 2023 at 17:00):

I cannot construct my param object. Do you have any idea? I am totally stuck

view this post on Zulip bachrc (Mar 06 2023 at 01:42):

I'm sorry to ask such a "Rust-ish" question, but I really tried everything to do these commands

view this post on Zulip bachrc (Mar 06 2023 at 01:44):

If I inline the param struct in the function call it's okay, but I cannot inline everytime

view this post on Zulip Lann Martin (Mar 06 2023 at 14:58):

More context would help in answering your question (a full example source file)

view this post on Zulip bachrc (Mar 09 2023 at 12:08):

Sorry for the delay, I've been busy at work.
This is as simple as this code :

fn push_file_to_index(file: FileDocument) -> Result<()> {
    michel_api::new_document_for_index("files", DocumentParam::from(file));

    Ok(())
}

In another file, I wanted to implement the From trait to the wit generated DocumentParam :

impl<'a> From<FileDocument> for DocumentParam<'a> {
    fn from(value: FileDocument) -> Self {
        DocumentParam {
            identifier: &value.filename,
            fields: vec![
                FieldParam { name: "filename", value: Text(&value.filename.to_string().as_ref()) },
                FieldParam { name: "path", value: Text(&value.path.to_string_lossy().as_ref()) },
            ].deref(),
        }
    }
}

This is one of the things I tried, but I failed everywhere.

My FileDocument is a basic struct like this :

pub struct FileDocument {
    id: Uuid,
    filename: String,
    path: PathBuf
}

view this post on Zulip Lann Martin (Mar 09 2023 at 19:56):

There isn't a simple way to do exactly what you want. wit-bindgen param structs borrow their strings which in this case means you have to turn e.g. Uuid into a String that lives long enough to pass to the function call

view this post on Zulip Lann Martin (Mar 09 2023 at 19:59):

As a couple of options:

As you say this is more of a general Rust problem and there are lots of ways to accomplish this

view this post on Zulip bachrc (Mar 10 2023 at 09:44):

Thank you for your kind answer, I'm gonna work on this


Last updated: Nov 22 2024 at 17:03 UTC