From f2b7320b6b800dd54c48ec52094bacbcc41a875b Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 9 Dec 2020 13:47:32 +0100 Subject: [PATCH] macro, function pointers and operator overloading --- function_pointers/Cargo.toml | 9 ++++++++ function_pointers/src/main.rs | 13 +++++++++++ hello_macro/Cargo.toml | 10 +++++++++ hello_macro/hello_macro_derive/Cargo.toml | 14 ++++++++++++ hello_macro/hello_macro_derive/src/lib.rs | 27 +++++++++++++++++++++++ hello_macro/src/lib.rs | 3 +++ hello_macro/src/main.rs | 9 ++++++++ operator_overloading/Cargo.toml | 9 ++++++++ operator_overloading/src/main.rs | 25 +++++++++++++++++++++ pancakes/Cargo.toml | 11 +++++++++ pancakes/src/main.rs | 9 ++++++++ 11 files changed, 139 insertions(+) create mode 100644 function_pointers/Cargo.toml create mode 100644 function_pointers/src/main.rs create mode 100644 hello_macro/Cargo.toml create mode 100644 hello_macro/hello_macro_derive/Cargo.toml create mode 100644 hello_macro/hello_macro_derive/src/lib.rs create mode 100644 hello_macro/src/lib.rs create mode 100644 hello_macro/src/main.rs create mode 100644 operator_overloading/Cargo.toml create mode 100644 operator_overloading/src/main.rs create mode 100644 pancakes/Cargo.toml create mode 100644 pancakes/src/main.rs diff --git a/function_pointers/Cargo.toml b/function_pointers/Cargo.toml new file mode 100644 index 0000000..befdb1a --- /dev/null +++ b/function_pointers/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "function_pointers" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/function_pointers/src/main.rs b/function_pointers/src/main.rs new file mode 100644 index 0000000..c59a677 --- /dev/null +++ b/function_pointers/src/main.rs @@ -0,0 +1,13 @@ +fn add_one(x: i32) -> i32 { + x + 1 +} + +fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 { + f(arg) + f(arg) +} + +fn main() { + let answer = do_twice(add_one, 5); + + println!("The answer is: {}", answer); +} \ No newline at end of file diff --git a/hello_macro/Cargo.toml b/hello_macro/Cargo.toml new file mode 100644 index 0000000..dc420a5 --- /dev/null +++ b/hello_macro/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "hello_macro" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +hello_macro_derive = { path = "./hello_macro_derive" } \ No newline at end of file diff --git a/hello_macro/hello_macro_derive/Cargo.toml b/hello_macro/hello_macro_derive/Cargo.toml new file mode 100644 index 0000000..dd015d7 --- /dev/null +++ b/hello_macro/hello_macro_derive/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "hello_macro_derive" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +syn = "1.0" +quote = "1.0" + +[lib] +proc-macro = true \ No newline at end of file diff --git a/hello_macro/hello_macro_derive/src/lib.rs b/hello_macro/hello_macro_derive/src/lib.rs new file mode 100644 index 0000000..ecf2f66 --- /dev/null +++ b/hello_macro/hello_macro_derive/src/lib.rs @@ -0,0 +1,27 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn; + +fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl HelloMacro for #name { + fn hello_macro() { + println!("Hello, Macro! My name is {}!", stringify!(#name)); + } + } + }; + gen.into() +} + +#[proc_macro_derive(HelloMacro)] +pub fn hello_macro_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_hello_macro(&ast) +} \ No newline at end of file diff --git a/hello_macro/src/lib.rs b/hello_macro/src/lib.rs new file mode 100644 index 0000000..dce090c --- /dev/null +++ b/hello_macro/src/lib.rs @@ -0,0 +1,3 @@ +pub trait HelloMacro { + fn hello_macro(); +} \ No newline at end of file diff --git a/hello_macro/src/main.rs b/hello_macro/src/main.rs new file mode 100644 index 0000000..0d20451 --- /dev/null +++ b/hello_macro/src/main.rs @@ -0,0 +1,9 @@ +use hello_macro::HelloMacro; +use hello_macro_derive::HelloMacro; + +#[derive(HelloMacro)] +struct Pancakes; + +fn main() { + Pancakes::hello_macro(); +} \ No newline at end of file diff --git a/operator_overloading/Cargo.toml b/operator_overloading/Cargo.toml new file mode 100644 index 0000000..b46ecd3 --- /dev/null +++ b/operator_overloading/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "operator_overloading" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/operator_overloading/src/main.rs b/operator_overloading/src/main.rs new file mode 100644 index 0000000..a5d0646 --- /dev/null +++ b/operator_overloading/src/main.rs @@ -0,0 +1,25 @@ +use std::ops::Add; + +#[derive(Debug, PartialEq)] +struct Point { + x: i32, + y: i32, +} + +impl Add for Point { + type Output = Point; + + fn add(self, other: Point) -> Point { + Point { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +fn main() { + assert_eq!( + Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, + Point { x: 3, y: 3 } + ); +} \ No newline at end of file diff --git a/pancakes/Cargo.toml b/pancakes/Cargo.toml new file mode 100644 index 0000000..fbc5c5e --- /dev/null +++ b/pancakes/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pancakes" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +hello_macro = { path = "../hello_macro" } +hello_macro_derive = { path = "../hello_macro/hello_macro_derive" } \ No newline at end of file diff --git a/pancakes/src/main.rs b/pancakes/src/main.rs new file mode 100644 index 0000000..0d20451 --- /dev/null +++ b/pancakes/src/main.rs @@ -0,0 +1,9 @@ +use hello_macro::HelloMacro; +use hello_macro_derive::HelloMacro; + +#[derive(HelloMacro)] +struct Pancakes; + +fn main() { + Pancakes::hello_macro(); +} \ No newline at end of file