'Cannot transfer a reference to a slice across await bounds
I'm struggling with the borrow checker when writing an interface for my services. The underlying meilisearch_sdk SDK requires that add_documents takes a reference to a slice, which cannot be transferred across await boundaries.
The line index.add_documents(&[document], Some(&id)).await produces the following error:
note: required for the cast to the object type `dyn futures::Future<Output = std::result::Result<meilisearch_sdk::tasks::Task, meilisearch_sdk::errors::Error>> + std::marker::Send`rustc
meili.rs(10, 143): future created by async block is not `Send`
meili.rs(13, 37): has type `&[D]` which is not `Send`
meili.rs(13, 60): await occurs here, with `&[document]` maybe used later
What is the correct way to approach / rectify this?
search.rs
#[async_trait]
pub trait SearchIndex {
type IndexResult;
type IndexError;
async fn index_document<D>(&self, index: String, document: D) -> Result<Self::IndexResult, Self::IndexError> where D: SearchableDocument;
async fn search_document(&self, index: String, query: String) -> Result<Self::IndexResult, Self::IndexError>;
}
index.rs
use meilisearch_sdk::{client::Client, errors::Error, tasks::Task};
use async_trait::async_trait;
use super::super::search::{SearchIndex, SearchableDocument};
#[async_trait]
impl SearchIndex for Client {
type IndexResult = Task;
type IndexError = Error;
async fn index_document<D>(&self, index: &String, document:D ) -> Result<Self::IndexResult, Self::IndexError> where D: SearchableDocument {
let index = self.index(index);
let id = document.get_id();
index.add_documents(&[document], Some(&id)).await
}
}
add_documents
pub trait SearchableDocument: DeserializeOwned + Serialize + Debug + Send{}
pub async fn add_documents<T: DeserializeOwned + Serialize + std::fmt::Debug>(
&self,
documents: &[T],
primary_key: Option<&str>,
) -> Result<Task, Error> {
self.add_or_replace(documents, primary_key).await
}
Cargo.toml
[dependencies]
serde = "1.0.136"
serde_json = "1.0.79"
rdkafka = "0.28.0"
tokio = { version = "1.17", features = ["rt-multi-thread", "macros"] }
meilisearch-sdk = "0.15.0"
futures = "0.3.21"
log = "0.4.16"
pretty_env_logger = "0.4.0"
async-trait = "0.1.53"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
