Rust driver: help writing a generic find method

Hey @Raymundo_63313!

The reason you’re seeing an error here is that the Cursor<T> type only implements Stream if the T implements DeserializeOwned, Unpin, Send, and Sync. You can see this requirement here: Cursor in mongodb - Rust. In your example, the I type is only required to implement MongoDbModel though, so all the trait requirements aren’t satisfied.

To fix this, you can update your MongoDbModel trait to inherit from those traits:

trait MongoDbModel: DeserializeOwned + Sync + Send + Unpin { ... }

Or, you can update the constraints of the get_all_vec function:

pub async fn get_all_vec<I>(
    db: &Database,
    filter: Option<Document>,
    options: Option<FindOptions>,
) -> Vec<I>
where
    I: MongoDbModel + DeserializeOwned + Unpin + Send + Sync,
{
    // impl here
}

As a side note, it may be possible for us to relax some of these trait requirements in a future version. I filed https://jira.mongodb.org/browse/RUST-1358 to track the work for investigating this. Thanks for bringing this to our attention!

1 Like