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.
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()) },
I cannot construct my param object. Do you have any idea? I am totally stuck
I'm sorry to ask such a "Rust-ish" question, but I really tried everything to do these commands
If I inline the param struct in the function call it's okay, but I cannot inline everytime
More context would help in answering your question (a full example source file)
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
}
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
As a couple of options:
FileDocument
to a e.g. Document
type which owns its fields (which wit-bindgen might already have created for you as DocumentResult
, depending on the rest of the wit) and then borrow DocumentParam
from _that_new_document_for_index
in a function that borrows directly from FileDocument
As you say this is more of a general Rust problem and there are lots of ways to accomplish this
Thank you for your kind answer, I'm gonna work on this
Last updated: Nov 22 2024 at 17:03 UTC