use orx_self_or::SoR; use std::collections::HashMap; #[derive(PartialEq, Debug)] struct User { user_name: String, roles: Vec, } impl User { fn new(user_name: String, roles: Vec) -> Self { Self { user_name, roles } } } struct UserService where M: SoR>, { // M can either be (i) &mut HashMap<_, _> or (ii) HashMap<_, _> users: M, } impl UserService where M: SoR>, { fn user(&self, user_name: &str) -> Option<&User> { self.users.get_ref().get(user_name) } } fn main() { let users: HashMap = [ ( "john".to_string(), User::new("john".to_string(), vec!["admin".to_string()]), ), ( "doe".to_string(), User::new("doe".to_string(), vec!["guest".to_string()]), ), ] .into_iter() .collect(); // HOLDING REF: user service having a shared reference to "users" let service = UserService { users: &users }; assert_eq!( service.user("john"), Some(&User::new("john".to_string(), vec!["admin".to_string()])) ); // OWNING: user service owning "users" let service = UserService { users }; assert_eq!( service.user("john"), Some(&User::new("john".to_string(), vec!["admin".to_string()])) ); }