Skip to content

Commit

Permalink
write more robust version of with_cloned
Browse files Browse the repository at this point in the history
now you can have individual mut bindings, and you can... I guess "destructure" one reference to clone in variables that have reference type uaaaaaaaaaaa this is so much more flexible and powerful doing it like this,
  • Loading branch information
meadowsys committed Dec 2, 2024
1 parent 52bb3e1 commit d916860
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions decl-macro/src/with_cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,58 @@ macro_rules! __with_cloned_impl {
};
}

#[macro_export]
macro_rules! with_cloned_2 {
($($stuff:tt)*) => {
// hide potential distracting implementation details in docs
$crate::__with_cloned_impl_2! { $($stuff)* }
}
}

#[doc(hidden)]
#[macro_export]
macro_rules! __with_cloned_impl_2 {
{ $(,)? in $($stuff:tt)* } => {
{
$($stuff)*
}
};

{ $(,)? _ $($rest:tt)* } => {
{
$crate::__with_cloned_impl_2! { $($rest)* }
}
};

{ $(,)? mut &$thing:ident $($rest:tt)* } => {
{
let mut $thing = ::core::clone::Clone::clone($thing);
$crate::__with_cloned_impl_2! { $($rest)* }
}
};

{ $(,)? mut $thing:ident $($rest:tt)* } => {
{
let mut $thing = ::core::clone::Clone::clone(&$thing);
$crate::__with_cloned_impl_2! { $($rest)* }
}
};

{ $(,)? &$thing:ident $($rest:tt)* } => {
{
let $thing = ::core::clone::Clone::clone($thing);
$crate::__with_cloned_impl_2! { $($rest)* }
}
};

{ $(,)? $thing:ident $($rest:tt)* } => {
{
let $thing = ::core::clone::Clone::clone(&$thing);
$crate::__with_cloned_impl_2! { $($rest)* }
}
};
}

// #[cfg(test)]
// mod tests {
// extern crate rand;
Expand Down

0 comments on commit d916860

Please sign in to comment.