Method try_next cannot be called on mongodb::Cursor<> due to unsatisfied trait bounds

Hello everyone.

Why RUST compiler could start complaining, that method try_next cannot be called on mongodb::Cursor<> due to unsatisfied trait bounds, when I added Vec<Rc> fields to my model?

I added this field:

#[serde(skip)]
pub out_relations: Vec<Rc<Relation>>,

And this code which used to be compiled without issues:

let items = db.collection::<Item>("items");
let mut cursor = items.find(Some(doc! { "project_id": pid }), None).await?;
while let Some(item) = cursor.try_next().await? {
  item_map.insert(item.id, item);
}

Started producing errors like TryStreamExt trait is not implemented.
Previously I had same Vectors but with ObjectId and with reference to another struct - it was fine.
Once I tried Rc<> - it stopped compiling.

Here is the same my question on Stack Overflow with more readable code and errors.

Hi @Yuri_Gor , this is a similar issue to that described in this thread. In short, Cursor<T> type only implements Stream if the T implements DeserializeOwned , Unpin , Send , and Sync . Because Rc is !Send and !Sync, adding the Rc makes it so the Cursor no longer implements Stream.

To work around this, we would suggest using an Arc rather than an Rc, since Arc implements Send and Sync.

Relatedly, this has come up in the past and we may be able to relax these trait bounds in the future to avoid errors like this altogether; see RUST-1358 for details.

Thank you! That explains everything.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.