Quick macro to do newtype deref implementation

This commit is contained in:
Timothy Warren 2022-12-16 12:20:31 -05:00
parent 87912b25c4
commit 556ab5669d
2 changed files with 22 additions and 13 deletions

View File

@ -1,2 +1,22 @@
pub mod grid;
pub use grid::*;
#[macro_export]
macro_rules! deref {
($($struct: ty, $target: ty),* ) => {
$(
impl ::core::ops::Deref for $struct {
type Target = $target;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ::core::ops::DerefMut for $struct {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
)*
}
}

View File

@ -1,7 +1,7 @@
use aoc_shared::deref;
use aoc_shared::grid::Grid as BaseGrid;
use aoc_shared::grid::Grid2d;
use std::collections::HashSet;
use std::ops::{Deref, DerefMut};
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Hash)]
enum VisibleDirection {
@ -53,18 +53,7 @@ impl Tree {
// 2. Implement the Deref trait for the wrapped struct
#[derive(Debug)]
pub struct Grid<T>(BaseGrid<T>);
impl<T> Deref for Grid<T> {
type Target = BaseGrid<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Grid<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
deref!(Grid<Tree>, BaseGrid<Tree>);
impl Grid<Tree> {
pub fn new(width: usize) -> Self {