-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce as_context macro for building a common pattern of using a type
as context for another type (e.g. IDs) for display purposes - Attempt WithContext cleanup
- Loading branch information
Showing
8 changed files
with
135 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[macro_export] | ||
macro_rules! as_context( | ||
{ $t: ty, $ctx: ty, $name: ident } => { | ||
#[derive(Debug)] | ||
pub struct $name<'a> { | ||
value: $t, | ||
ctx: &'a $ctx, | ||
} | ||
|
||
impl $name<'_> { | ||
pub fn context(&self) -> &$ctx { | ||
&self.ctx | ||
} | ||
|
||
pub fn child(&self, child: $t) -> Self { | ||
$name { value: child, ctx: self.ctx } | ||
} | ||
|
||
fn in_context<'a>(ctx: &'a $ctx, value: $t) -> $name<'a> { | ||
$name { value, ctx } | ||
} | ||
} | ||
|
||
impl std::ops::Deref for $name<'_> { | ||
type Target = $t; | ||
|
||
fn deref(&self) -> &$t { | ||
&self.value | ||
} | ||
} | ||
}; | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#[macro_use] | ||
pub mod contexts; | ||
|
||
#[macro_use] | ||
pub mod map_macros; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.