Hi, i'm currently playing around in rust in a pet project trying to get some data out of a postgres db.
So i have this code here:
#[derive(Default)]
struct ObjectCache {
pub db_objects: Option<Vec<ObjectConfig>>,
}
pub struct Setup {
db: Arc<Db>,
cache: RefCell<ObjectCache>,
}
impl Setup {
pub fn new(db: Arc<Db>) -> Setup {
Setup {
db: db,
cache: Default::default(),
}
}
pub fn list_db_objects(&self) -> Result<Vec<(String, u32)>, String> {
let objects = self.db.get_selected_objects(-1).unwrap();
self.cache.borrow_mut().db_objects = Some(objects);
let result = self.cache
.borrow()
.db_objects
.as_ref()
.unwrap()
.iter()
.map(|obj| (obj.name.clone(), obj.count))
.collect();
Ok(result)
}
}
This works fine, the only issue i have now is that I have to clone anything that i want to return from list_db_objects:
.map(|obj| (obj.name.clone(), obj.count))
is it possible to change the code to somehow just return a reference like this:
.map(|obj| (&obj.name, obj.count))
Thanks