Add macro examples

This commit is contained in:
Timothy Warren 2019-02-11 10:14:27 -05:00
parent 59bc706d0a
commit 12b2de3d68
9 changed files with 221 additions and 85 deletions

View File

@ -41,6 +41,8 @@
<cargoProject FILE="$PROJECT_DIR$/advanced_traits/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/advanced_types/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/advanced_functions/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/macros/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/pancakes/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

View File

@ -165,6 +165,14 @@
<sourceFolder url="file://$MODULE_DIR$/advanced_functions/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/advanced_functions/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/advanced_functions/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/hello_macro/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/macros/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/macros/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/macros/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/pancakes/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/pancakes/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/pancakes/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/pancakes/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
@ -183,6 +191,7 @@
<excludeFolder url="file://$MODULE_DIR$/generics/target" />
<excludeFolder url="file://$MODULE_DIR$/guessing_game/target" />
<excludeFolder url="file://$MODULE_DIR$/gui/target" />
<excludeFolder url="file://$MODULE_DIR$/hello_macro/target" />
<excludeFolder url="file://$MODULE_DIR$/interior_mutability/target" />
<excludeFolder url="file://$MODULE_DIR$/lifetimes/target" />
<excludeFolder url="file://$MODULE_DIR$/loops/target" />
@ -191,6 +200,7 @@
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
<excludeFolder url="file://$MODULE_DIR$/mutex/target" />
<excludeFolder url="file://$MODULE_DIR$/oop/target" />
<excludeFolder url="file://$MODULE_DIR$/pancakes/target" />
<excludeFolder url="file://$MODULE_DIR$/pattern_matching/target" />
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
<excludeFolder url="file://$MODULE_DIR$/reference_counting/target" />

View File

@ -14,23 +14,55 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/advanced_functions/src/main.rs">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/pancakes/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="544">
<caret line="34" column="25" selection-start-line="34" selection-start-column="25" selection-end-line="34" selection-end-column="25" />
<state relative-caret-position="144">
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
<folding>
<element signature="e#26#27#0" expanded="true" />
<element signature="e#38#39#0" expanded="true" />
<element signature="e#89#90#0" expanded="true" />
<element signature="e#112#113#0" expanded="true" />
<element signature="e#271#272#0" expanded="true" />
<element signature="e#291#292#0" expanded="true" />
<element signature="e#116#117#0" expanded="true" />
<element signature="e#147#148#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/hello_macro/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="288">
<caret line="18" column="5" selection-start-line="18" selection-start-column="5" selection-end-line="18" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="336">
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="192">
<caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/pancakes/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="144">
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
@ -60,14 +92,6 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/errors/src/main.rs" />
<option value="$PROJECT_DIR$/.gitignore" />
<option value="$PROJECT_DIR$/largest_number/src/main.rs" />
<option value="$PROJECT_DIR$/generics/Cargo.toml" />
<option value="$PROJECT_DIR$/generics/src/trait.rs" />
<option value="$PROJECT_DIR$/generics/src/traits.rs" />
<option value="$PROJECT_DIR$/generics/src/main.rs" />
<option value="$PROJECT_DIR$/generics/src/point.rs" />
<option value="$PROJECT_DIR$/generics/src/lib.rs" />
<option value="$PROJECT_DIR$/lifetimes/src/main.rs" />
<option value="$PROJECT_DIR$/adder/src/lib.rs" />
@ -111,6 +135,14 @@
<option value="$PROJECT_DIR$/advanced_traits/src/main.rs" />
<option value="$PROJECT_DIR$/advanced_types/src/main.rs" />
<option value="$PROJECT_DIR$/advanced_functions/src/main.rs" />
<option value="$PROJECT_DIR$/macros/Cargo.toml" />
<option value="$PROJECT_DIR$/macros/hello_macro_derive/Cargo.toml" />
<option value="$PROJECT_DIR$/macros/hello_macro_derive/src/lib.rs" />
<option value="$PROJECT_DIR$/macros/src/lib.rs" />
<option value="$PROJECT_DIR$/macros/src/main.rs" />
<option value="$PROJECT_DIR$/pancakes/src/main.rs" />
<option value="$PROJECT_DIR$/pancakes/Cargo.toml" />
<option value="$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs" />
</list>
</option>
</component>
@ -167,14 +199,26 @@
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="advanced_functions" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="advanced_functions" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="hello_macro_derive" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="hello_macro_derive" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="pancakes" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand>
<select />
</subPane>
@ -183,7 +227,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549895652600" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549898035962" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" />
@ -211,7 +255,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Cargo Command.Run advanced_functions">
<component name="RunManager" selected="Cargo Command.Run pancakes">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" />
@ -270,13 +314,13 @@
<envs />
<method v="2" />
</configuration>
<configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<configuration name="Run pancakes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package advanced_lifetimes --lib tests" />
<option name="command" value="run --package pancakes --bin pancakes" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_lifetimes" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/pancakes" />
<envs />
<method v="2" />
</configuration>
@ -289,11 +333,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo Command.Run pancakes" />
<item itemvalue="Cargo Command.Run advanced_functions" />
<item itemvalue="Cargo Command.Run advanced_types" />
<item itemvalue="Cargo Command.Run advanced_traits" />
<item itemvalue="Cargo Command.Run advanced_lifetimes" />
<item itemvalue="Cargo Command.Test lib::tests" />
</list>
</recent_temporary>
</component>
@ -333,8 +377,8 @@
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49586156" visible="true" weight="0.2540132" />
<window_info id="Structure" order="1" sideWeight="0.50413847" side_tool="true" visible="true" weight="0.2540132" />
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.4957483" visible="true" weight="0.2540132" />
<window_info id="Structure" order="1" sideWeight="0.5042517" side_tool="true" visible="true" weight="0.2540132" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" />
@ -342,7 +386,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.2571269" />
<window_info anchor="bottom" id="Run" order="2" weight="0.2571269" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -350,7 +394,7 @@
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
<window_info anchor="bottom" id="Terminal" order="10" weight="0.34264952" />
<window_info anchor="bottom" id="Terminal" order="10" visible="true" weight="0.34264952" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@ -364,59 +408,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/modules/src/performance_group.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
<caret line="6" column="1" selection-start-line="6" selection-start-column="1" selection-end-line="6" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/modules/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
<caret line="6" lean-forward="true" selection-start-line="6" selection-end-line="6" />
<folding>
<element signature="e#366#367#0" expanded="true" />
<element signature="e#390#391#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/guessing_game/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="32">
<caret line="2" column="5" selection-start-line="2" selection-start-column="5" selection-end-line="2" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/errors/hello.txt">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="720">
<caret line="45" column="16" selection-start-line="45" selection-start-column="16" selection-end-line="45" selection-end-column="16" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/errors/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1024">
<caret line="64" column="1" lean-forward="true" selection-start-line="64" selection-start-column="1" selection-end-line="64" selection-end-column="1" />
<folding>
<element signature="e#285#286#0" expanded="true" />
<element signature="e#323#324#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/generics/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="7" lean-forward="true" selection-start-line="7" selection-end-line="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="128">
@ -807,8 +798,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/advanced_functions/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="544">
<caret line="34" column="25" selection-start-line="34" selection-start-column="25" selection-end-line="34" selection-end-column="25" />
<state relative-caret-position="608">
<caret line="38" selection-start-line="38" selection-end-line="38" />
<folding>
<element signature="e#26#27#0" expanded="true" />
<element signature="e#38#39#0" expanded="true" />
@ -820,5 +811,54 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/macros/src/main.rs">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/hello_macro/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="7" lean-forward="true" selection-start-line="7" selection-end-line="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/pancakes/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="144">
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="192">
<caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/pancakes/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="144">
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
<folding>
<element signature="e#116#117#0" expanded="true" />
<element signature="e#147#148#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/hello_macro/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="288">
<caret line="18" column="5" selection-start-line="18" selection-start-column="5" selection-end-line="18" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="336">
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
</state>
</provider>
</entry>
</component>
</project>

7
hello_macro/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "hello_macro"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]

View File

@ -0,0 +1,12 @@
[package]
name = "hello_macro_derive"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[lib]
proc-macro = true
[dependencies]
syn = "0.14.4"
quote = "0.6.3"

View File

@ -0,0 +1,27 @@
extern crate proc_macro;
use crate::proc_macro::TokenStream;
use quote::quote;
use syn;
#[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)
}
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()
}

20
hello_macro/src/lib.rs Normal file
View File

@ -0,0 +1,20 @@
pub trait HelloMacro {
fn hello_macro();
}
/// Simplified version of the vec! macro
/// This is an example of a declarative macro
#[macro_export] // This makes the macro usable
macro_rules! vec {
( $( $x:expr ),* ) => { // This matches the argument list
{
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
temp_vec
}
};
}

9
pancakes/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "pancakes"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
[dependencies]
hello_macro = { path = "../hello_macro" }
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }

9
pancakes/src/main.rs Normal file
View File

@ -0,0 +1,9 @@
use hello_macro::HelloMacro;
use hello_macro_derive::HelloMacro;
#[derive(HelloMacro)]
struct Pancakes;
fn main() {
Pancakes::hello_macro();
}