chiark / gitweb /
Soup up deref_to_field and use more
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 23:32:09 +0000 (23:32 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 23:32:09 +0000 (23:32 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/global.rs
src/prelude.rs
src/utils.rs

index 35b7bdb9a239c34194259959137bc09db4d5b81d..9571c2dedc5c811445979c65c96302062a50eb1e 100644 (file)
@@ -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)]
index 60faeba7b96b5db4972ee5d49db6e094e40c436e..04f05837abbb0409808b0b4fc702bf843b098816 100644 (file)
@@ -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;
index dfa31e899d1280005d02d3b7e7964bc2f84ed160..13a14d53a863d34f3db6da410e82d80e40c2b7c0 100644 (file)
@@ -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};
index 5354639e9558c6eb122d16f1618ad6d5af260cac..02fb9dfe5761def68f9b1cfa49cfcba042951a2c 100644 (file)
@@ -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)* }
     }
   }
 }