Last active
August 29, 2015 14:04
-
-
Save ozkriff/6ed55a0d85452dba44db to your computer and use it in GitHub Desktop.
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
use std::ops::{Deref, DerefMut}; | |
#[macro_export] | |
macro_rules! new_type( | |
($new_type_name: ident, $wrapped_type: ty) => ( | |
#[derive(Ord, PartialOrd, PartialEq, Eq, Hash)] | |
pub struct $new_type_name { | |
wrapped: $wrapped_type, | |
} | |
impl $new_type_name { | |
pub fn new(wrap_me: $wrapped_type) -> $new_type_name { | |
$new_type_name{wrapped: wrap_me} | |
} | |
} | |
impl Deref for $new_type_name { | |
type Target = $wrapped_type; | |
fn deref<'a>(&'a self) -> &'a $wrapped_type { | |
&self.wrapped | |
} | |
} | |
impl DerefMut for $new_type_name { | |
// type Target = $wrapped_type; | |
fn deref_mut<'a>(&'a mut self) -> &'a mut $wrapped_type { | |
&mut self.wrapped | |
} | |
} | |
) | |
); | |
new_type!(Fuck, i32); | |
fn main() { | |
let f = Fuck::new(3); | |
let a = *f + 3; | |
println!("Hello, world! {}", a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment