Macro wiwi::with_cloned
source · macro_rules! with_cloned { +with_cloned in wiwi - Rust Macro wiwi::
source ·with_cloned macro_rules! with_cloned { ($($stuff:tt)*) => { ... }; }Available on crate featurewith-cloned-unstable
only.Expand description
-Executes code with cloned values, so the executed code can take ownership of the value without moving the original
@@ -45,9 +45,9 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);… this is not great, since you’d have to use
-_shared_data
to refer to -the cloned value.But alternatively, it can be done with a temporary scope:
+… this is not great, since you’d have to use
+_shared_data
with an +underscore to refer to the cloned value.It can also be done with a temporary scope:
-let join_handle = { let shared_data = shared_data.clone(); @@ -60,13 +60,15 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);
… not really what we would do, but still not optimal either way.
-In our short(…ish?) time writing rust code, I’ve already noticed I use -this pattern a lot when dealing with shared state across threads. There are -likely patterns that we don’t know about (…or do, but haven’t thought about -at time of writing this documentation :p), and also maybe not always specifically -
+Arc<Mutex<Data>>
. Needing to write all that boilerplate for a very simple -and straightforward operation, doesn’t feel very ergonomic or elegant.… not really what we would do (really only minor style preferences, nothing +actually relating to function), but still not optimal either way.
+In our short(…ish?) time writing rust code, I’ve already noticed I use this +pattern a lot when dealing with shared state across threads. There are likely +patterns that we don’t know about, that would do something similar here (clone +a value, then consume the clone, then access original). Or maybe we do know +about them, but we haven’t thought about it at time of writing this documentation +:p. Needing to write all that boilerplate code for an otherwise very simple +operation, doesn’t feel very ergonomic or elegant, and quickly gets boring.
This macro can help with that:
-let join_handle = with_cloned! { shared_data in @@ -82,19 +84,21 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);
In my opinion, a bit less boilerplate, and a bit nicer.
-The syntax of the macro is, first a list of the variables that should have a -cloned version available, followed by keyword
+in
, then any statements -you would like to run. Essentially, you can think of the stuff afterin
-as just a regular block. Those who know what swift closures look like may -recognise this syntax :pIt cut out most of the boilerplate, and just feels a lot nicer to use.
+The syntax of the macro is, first a (comma seperated) list of the variables +that should have a cloned version available, followed by keyword
in
, then +any statements you would like to run. Essentially, you can think of the stuff +afterin
as just a regular block. Those who know what swift closures look +like may recognise this syntax heheYou can also bind the cloned values mutably, as well as return values out of the macro:
let string = String::new(); let modified_string = with_cloned! { mut string in - // do whatever + // ^^^ + + // do whatever string.push_str("uwu"); assert_eq!(string, "uwu"); @@ -107,13 +111,13 @@ // returned the modified version from within the macro assert_eq!(modified_string, "uwu");
Ideally you could bind variables mutably or not on a per variable basis, -but I can’t seem to find a way to do so, unfortunately >< so it’s all or -nothing for now.
+but unfortunately, I can’t seem to find a way to do so elegantly >~< so it’s +all or nothing for now.Random, but the below code snippets are equivalent:
let cloned1 = value.clone(); -// clones it for the inner code, but the inner code just returns it -// ... so it's just a clone +// macro clones it for the inner code, but the inner code +// just returns it... so it is just a clone let cloned2 = with_cloned! { value in value }; assert_eq!(value, cloned1); diff --git a/wiwi/with_cloned/index.html b/wiwi/with_cloned/index.html index 885dc61bd..2cec073c5 100644 --- a/wiwi/with_cloned/index.html +++ b/wiwi/with_cloned/index.html @@ -1,2 +1,2 @@ -
wiwi::with_cloned - Rust Module wiwi::
source ·with_cloned Available on crate featurewith-cloned-unstable
only.Macros§
Executes code with cloned values, so the executed code +wiwi::with_cloned - Rust \ No newline at end of file diff --git a/wiwi/with_cloned/macro.with_cloned.html b/wiwi/with_cloned/macro.with_cloned.html index 423c76946..9d5449c24 100644 --- a/wiwi/with_cloned/macro.with_cloned.html +++ b/wiwi/with_cloned/macro.with_cloned.html @@ -1,4 +1,4 @@ - Module wiwi::
source ·with_cloned Available on crate featurewith-cloned-unstable
only.Macros§
Executes code with cloned values, so the executed code can take ownership of the value without moving the originalwith_cloned in wiwi::with_cloned - Rust Macro wiwi::
source ·with_cloned:: with_cloned macro_rules! with_cloned { +with_cloned in wiwi::with_cloned - Rust Macro wiwi::
source ·with_cloned:: with_cloned macro_rules! with_cloned { ($($stuff:tt)*) => { ... }; }Available on crate featurewith-cloned-unstable
only.Expand description
-Executes code with cloned values, so the executed code can take ownership of the value without moving the original
@@ -45,9 +45,9 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);… this is not great, since you’d have to use
-_shared_data
to refer to -the cloned value.But alternatively, it can be done with a temporary scope:
+… this is not great, since you’d have to use
+_shared_data
with an +underscore to refer to the cloned value.It can also be done with a temporary scope:
-let join_handle = { let shared_data = shared_data.clone(); @@ -60,13 +60,15 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);
… not really what we would do, but still not optimal either way.
-In our short(…ish?) time writing rust code, I’ve already noticed I use -this pattern a lot when dealing with shared state across threads. There are -likely patterns that we don’t know about (…or do, but haven’t thought about -at time of writing this documentation :p), and also maybe not always specifically -
+Arc<Mutex<Data>>
. Needing to write all that boilerplate for a very simple -and straightforward operation, doesn’t feel very ergonomic or elegant.… not really what we would do (really only minor style preferences, nothing +actually relating to function), but still not optimal either way.
+In our short(…ish?) time writing rust code, I’ve already noticed I use this +pattern a lot when dealing with shared state across threads. There are likely +patterns that we don’t know about, that would do something similar here (clone +a value, then consume the clone, then access original). Or maybe we do know +about them, but we haven’t thought about it at time of writing this documentation +:p. Needing to write all that boilerplate code for an otherwise very simple +operation, doesn’t feel very ergonomic or elegant, and quickly gets boring.
This macro can help with that:
-let join_handle = with_cloned! { shared_data in @@ -82,19 +84,21 @@ join_handle.join().unwrap(); assert_eq!(*shared_data.lock().unwrap(), 420);
In my opinion, a bit less boilerplate, and a bit nicer.
-The syntax of the macro is, first a list of the variables that should have a -cloned version available, followed by keyword
+in
, then any statements -you would like to run. Essentially, you can think of the stuff afterin
-as just a regular block. Those who know what swift closures look like may -recognise this syntax :pIt cut out most of the boilerplate, and just feels a lot nicer to use.
+The syntax of the macro is, first a (comma seperated) list of the variables +that should have a cloned version available, followed by keyword
in
, then +any statements you would like to run. Essentially, you can think of the stuff +afterin
as just a regular block. Those who know what swift closures look +like may recognise this syntax heheYou can also bind the cloned values mutably, as well as return values out of the macro:
let string = String::new(); let modified_string = with_cloned! { mut string in - // do whatever + // ^^^ + + // do whatever string.push_str("uwu"); assert_eq!(string, "uwu"); @@ -107,13 +111,13 @@ // returned the modified version from within the macro assert_eq!(modified_string, "uwu");
Ideally you could bind variables mutably or not on a per variable basis, -but I can’t seem to find a way to do so, unfortunately >< so it’s all or -nothing for now.
+but unfortunately, I can’t seem to find a way to do so elegantly >~< so it’s +all or nothing for now.Random, but the below code snippets are equivalent:
let cloned1 = value.clone(); -// clones it for the inner code, but the inner code just returns it -// ... so it's just a clone +// macro clones it for the inner code, but the inner code +// just returns it... so it is just a clone let cloned2 = with_cloned! { value in value }; assert_eq!(value, cloned1);