From 9c840210cf46801082b479787bd92da441016db5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 21 Feb 2021 23:32:09 +0000 Subject: [PATCH] Soup up deref_to_field and use more Signed-off-by: Ian Jackson --- src/bin/otter.rs | 16 ++-------------- src/global.rs | 8 +------- src/prelude.rs | 1 + src/utils.rs | 13 +++++++++++-- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 35b7bdb9..9571c2de 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -437,13 +437,7 @@ struct Conn { chan: MgmtChannel, } -impl Deref for Conn { - type Target = MgmtChannel; - fn deref(&self) -> &MgmtChannel { &self.chan } -} -impl DerefMut for Conn { - fn deref_mut(&mut self) -> &mut MgmtChannel { &mut self.chan } -} +deref_to_field_mut!{Conn, MgmtChannel, chan} impl Conn { #[throws(AE)] @@ -512,13 +506,7 @@ struct ConnForGame { pub game: InstanceName, pub how: MgmtGameUpdateMode, } -impl Deref for ConnForGame { - type Target = Conn; - fn deref(&self) -> &Conn { &self.conn } -} -impl DerefMut for ConnForGame { - fn deref_mut(&mut self) -> &mut Conn { &mut self.conn } -} +deref_to_field_mut!{ConnForGame, Conn, conn} impl ConnForGame { #[throws(AE)] diff --git a/src/global.rs b/src/global.rs index 60faeba7..04f05837 100644 --- a/src/global.rs +++ b/src/global.rs @@ -440,13 +440,7 @@ pub fn games_lock() -> RwLockWriteGuard<'static, GamesTable> { // ---------- Simple trait implementations ---------- -impl Deref for InstanceGuard<'_> { - type Target = Instance; - fn deref(&self) -> &Instance { &self.c.g } -} -impl DerefMut for InstanceGuard<'_> { - fn deref_mut(&mut self) -> &mut Instance { &mut self.c.g } -} +deref_to_field_mut!{InstanceGuard<'_>, Instance, c.g} impl FromStr for AccountScope { type Err = InvalidScopedName; diff --git a/src/prelude.rs b/src/prelude.rs index dfa31e89..13a14d53 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -92,6 +92,7 @@ pub use otter_base::zcoord::{self, ZCoord}; pub use otter_base::misc as base_misc; pub use base_misc::default; +pub use crate::{deref_to_field, deref_to_field_mut}; pub use crate::from_instance_lock_error; pub use crate::accounts::loaded_acl::{self, EffectiveACL, LoadedAcl, PermSet}; diff --git a/src/utils.rs b/src/utils.rs index 5354639e..02fb9dfe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -276,10 +276,19 @@ pub fn toml_merge<'u, #[macro_export] macro_rules! deref_to_field { - {$outer:ident, $inner:ty, $field:ident} => { + {$outer:ty, $inner:ty, $($field:tt)*} => { impl Deref for $outer { type Target = $inner; - fn deref(&self) -> &$inner { &self.$field } + fn deref(&self) -> &$inner { &self.$($field)* } + } + } +} +#[macro_export] +macro_rules! deref_to_field_mut { + {$outer:ty, $inner:ty, $($field:tt)*} => { + deref_to_field!{$outer, $inner, $($field)*} + impl DerefMut for $outer { + fn deref_mut(&mut self) -> &mut $inner { &mut self.$($field)* } } } } -- 2.30.2