pub async fn create<T: Serialize>(form: &mut T, COLLECTION_NAME: &str) -> Result<T, ServiceError> {
let collection = db::get_collection(COLLECTION_NAME).await?;
let mut doc = db::encode(&form).unwrap();
collection.insert_one(doc, None).await?;
find(form.id.as_ref().unwrap(), COLLECTION_NAME).await
}
I'm trying to define a generic function of Type T. When I try to access the field from 'form' which is of type T.
I get the following error:
no field `id` on type `&mut T`
pub struct Form {
#[serde(rename = "_id")]
pub id: Option<ObjectId>,
pub name: String,
}
The generic type T will receive the following struct as a parameter.
There can be multiple structs with id
field