Есть такие структуры:
#[derive(RustcDecodable, RustcEncodable)]
#[derive(Debug, Clone)]
struct Mr {
id: MrUid,
...
status: Status,
...
}
struct MrStorageInternal(HashMap<MrUid, Mr>);
struct MrStorage(Mutex<MrStorageInternal>);
Я могу для них сделать так для удобства:
impl Deref for MrStorageInternal {
type Target = HashMap<MrUid, Mr>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Deref for MrStorage {
type Target = Mutex<MrStorageInternal>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
но тогда я не могу вызвать
fn update_or_create_mr(
storage: &mut MrStorageInternal,
...
)
вот так:
update_or_create_mr(
&mut *mr_storage.lock().unwrap(),
потому что
src/main.rs:409:13: 409:45 error: mismatched types:
expected `&mut MrStorageInternal`,
found `&mut std::collections::hash::map::HashMap<MrUid, MergeRequest>`
(expected struct `MrStorageInternal`,
found struct `std::collections::hash::map::HashMap`) [E0308]
src/main.rs:409 &mut *mr_storage.lock().unwrap(),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Если убрать реализацию Deref для MrStorageInternal, то я не могу прозрачно вызывать методы HashMap:
src/main.rs:247:44: 247:56 error: no method named `get_mut` found for type `&mut MrStorageInternal` in the current scope
src/main.rs:247 if let Some(mut existing_mr) = storage.get_mut(&id) {
^~~~~~~~~~~~
Как быть?