Last active
July 18, 2017 16:17
-
-
Save kylewlacy/6bc3ca6eb6cf88d9edb1 to your computer and use it in GitHub Desktop.
Rust macro for creating a closure that implements the `Clone` trait. (Dual licensed under MIT/Unlicense; see below)
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
#![feature(unboxed_closures)] | |
#![feature(fn_traits)] | |
use std::time; | |
use std::thread; | |
use std::sync::{Arc, RwLock}; | |
macro_rules! fn_clone { | |
// Normalized form | |
( | |
@clone: [$($clone:ident: $clone_ty:ty),*], | |
@args: [$($arg:ident: $arg_ty:ty),*], | |
@ret: $ret:ty, | |
@body: $body:expr | |
) => { | |
{ | |
#[derive(Clone)] | |
struct AnonFn { | |
$($clone: $clone_ty,)* | |
} | |
impl FnOnce<($($arg_ty,)*)> for AnonFn { | |
type Output = $ret; | |
extern "rust-call" fn call_once(self, ($($arg,)*): ($($arg_ty,)*)) -> $ret { | |
let AnonFn { $($clone,)* } = self; | |
{ $body } | |
} | |
} | |
AnonFn { | |
$($clone: $clone.clone(),)* | |
} | |
} | |
}; | |
// fn_clone!([fizz: Fizz, buzz: Buzz] |foo: Foo, bar: Bar| -> Baz => { qux() }) | |
( | |
[$($clone:ident: $clone_ty:ty),*] | |
|$($arg:ident: $arg_ty:ty),*| -> $ret:ty => | |
$body:expr | |
) => { | |
fn_clone!( | |
@clone:[$($clone: $clone_ty),*], | |
@args: [$($arg: $arg_ty),*], | |
@ret: $ret, | |
@body: $body | |
) | |
}; | |
// fn_clone!([fizz: Fizz, buzz: Buzz] || -> Baz => { qux() }) | |
( | |
[$($clone:ident: $clone_ty:ty),*] | |
|| -> $ret:ty => | |
$body:expr | |
) => { | |
fn_clone!( | |
@clone:[$($clone: $clone_ty),*], | |
@args: [], | |
@ret: $ret, | |
@body: $body | |
) | |
}; | |
// fn_clone!([fizz: Fizz, buzz: Buzz] |Foo: Foo, bar: Bar| => { qux(); }) | |
( | |
[$($clone:ident: $clone_ty:ty),*] | |
|$($arg:ident: $arg_ty:ty),*| => | |
$body:expr | |
) => { | |
fn_clone!( | |
@clone: [$($clone: $clone_ty),*], | |
@args: [$($arg: $arg_ty),*], | |
@ret: (), | |
@body: $body | |
) | |
}; | |
// fn_clone!([fizz: Fizz, buzz: Buzz] || => { qux(); }) | |
( | |
[$($clone:ident: $clone_ty:ty),*] | |
|| => | |
$body:expr | |
) => { | |
fn_clone!( | |
@clone: [$($clone: $clone_ty),*], | |
@args: [], | |
@ret: (), | |
@body: $body | |
) | |
}; | |
} | |
fn main() { | |
let count = Arc::new(RwLock::new(0)); | |
let increment = fn_clone!([count: Arc<RwLock<i32>>] | |
|| => { | |
let mut count = count.write().unwrap(); | |
thread::sleep(time::Duration::from_secs(1)); | |
*count += 1; | |
println!("Count is {}", *count); | |
} | |
); | |
let decrement = fn_clone!([count: Arc<RwLock<i32>>] || => { | |
let mut count = count.write().unwrap(); | |
thread::sleep(time::Duration::from_secs(1)); | |
*count -= 1; | |
println!("Count is {}", *count); | |
} | |
); | |
let observe = fn_clone!([count: Arc<RwLock<i32>>] | |
|| => { | |
let count = count.read().unwrap(); | |
thread::sleep(time::Duration::from_secs(1)); | |
println!("Observed: count is {}", *count); | |
} | |
); | |
for _ in 0..10 { | |
thread::spawn(increment.clone()); | |
} | |
for _ in 0..10 { | |
thread::spawn(decrement.clone()); | |
} | |
for _ in 0..10 { | |
thread::spawn(observe.clone()); | |
} | |
loop { | |
let count = count.read().unwrap(); | |
thread::sleep(time::Duration::from_secs(1)); | |
println!("main: Count is {}", *count); | |
} | |
} |
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
The MIT License (MIT) | |
Copyright (c) 2016 Kyle Lacy | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
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 is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment