Add smart_pointers example

This commit is contained in:
Timothy Warren 2019-02-05 10:10:47 -05:00
parent ca6840cec2
commit 9b0fff8c9f
5 changed files with 123 additions and 117 deletions

View File

@ -24,6 +24,7 @@
<cargoProject FILE="$PROJECT_DIR$/closures/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/closures/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/minigrep_v2/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/minigrep_v2/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/add/Cargo.toml" /> <cargoProject FILE="$PROJECT_DIR$/add/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/smart_pointers/Cargo.toml" />
</component> </component>
<component name="ComposerJsonPluginSettings"> <component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings> <unboundedVersionInspectionSettings>

View File

@ -98,6 +98,10 @@
<sourceFolder url="file://$MODULE_DIR$/add/add-one/examples" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/add/add-one/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/add/add-one/tests" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/add/add-one/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/add/add-one/benches" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/add/add-one/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/smart_pointers/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/smart_pointers/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/smart_pointers/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/smart_pointers/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" /> <excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" /> <excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
<excludeFolder url="file://$MODULE_DIR$/adder/target" /> <excludeFolder url="file://$MODULE_DIR$/adder/target" />
@ -119,6 +123,7 @@
<excludeFolder url="file://$MODULE_DIR$/references/target" /> <excludeFolder url="file://$MODULE_DIR$/references/target" />
<excludeFolder url="file://$MODULE_DIR$/scope/target" /> <excludeFolder url="file://$MODULE_DIR$/scope/target" />
<excludeFolder url="file://$MODULE_DIR$/slices/target" /> <excludeFolder url="file://$MODULE_DIR$/slices/target" />
<excludeFolder url="file://$MODULE_DIR$/smart_pointers/target" />
<excludeFolder url="file://$MODULE_DIR$/structs/target" /> <excludeFolder url="file://$MODULE_DIR$/structs/target" />
<excludeFolder url="file://$MODULE_DIR$/variables/target" /> <excludeFolder url="file://$MODULE_DIR$/variables/target" />
</content> </content>

View File

@ -14,82 +14,28 @@
</component> </component>
<component name="FileEditorManager"> <component name="FileEditorManager">
<leaf> <leaf>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep_v2/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="7" selection-start-line="7" selection-end-line="7" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep_v2/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="272">
<caret line="17" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/minigrep_v2/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="624">
<caret line="39" selection-start-line="39" selection-end-line="39" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/add/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="96">
<caret line="6" selection-start-line="6" selection-end-line="6" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/add/add-one/src/lib.rs"> <entry file="file://$PROJECT_DIR$/smart_pointers/src/main.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="208"> <state relative-caret-position="32">
<caret line="13" column="5" lean-forward="true" selection-start-line="13" selection-start-column="5" selection-end-line="13" selection-end-column="5" /> <caret line="2" lean-forward="true" selection-start-line="2" selection-end-line="2" />
<folding> <folding>
<element signature="e#30#31#0" expanded="true" /> <element signature="e#68#69#0" expanded="true" />
<element signature="e#40#41#0" expanded="true" /> <element signature="e#91#92#0" expanded="true" />
<element signature="e#87#88#0" expanded="true" /> <element signature="e#200#201#0" expanded="true" />
<element signature="e#123#124#0" expanded="true" /> <element signature="e#216#217#0" expanded="true" />
<element signature="e#351#352#0" expanded="true" />
<element signature="e#433#434#0" expanded="true" />
<element signature="e#244#245#0" expanded="true" />
<element signature="e#261#262#0" expanded="true" />
</folding> </folding>
</state> </state>
</provider> </provider>
</entry> </entry>
</file> </file>
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/add/adder/Cargo.toml"> <entry file="file://$PROJECT_DIR$/smart_pointers/src/lib.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor" />
<state relative-caret-position="144">
<caret line="9" selection-start-line="9" selection-end-line="9" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/add/adder/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="64">
<caret line="4" column="77" selection-start-line="4" selection-start-column="77" selection-end-line="4" selection-end-column="77" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/add/add-one/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="7" selection-start-line="7" selection-end-line="7" />
</state>
</provider>
</entry> </entry>
</file> </file>
</leaf> </leaf>
@ -159,6 +105,8 @@
<option value="$PROJECT_DIR$/add/add-one/Cargo.toml" /> <option value="$PROJECT_DIR$/add/add-one/Cargo.toml" />
<option value="$PROJECT_DIR$/add/adder/Cargo.toml" /> <option value="$PROJECT_DIR$/add/adder/Cargo.toml" />
<option value="$PROJECT_DIR$/add/add-one/src/lib.rs" /> <option value="$PROJECT_DIR$/add/add-one/src/lib.rs" />
<option value="$PROJECT_DIR$/smart_pointers/src/lib.rs" />
<option value="$PROJECT_DIR$/smart_pointers/src/main.rs" />
</list> </list>
</option> </option>
</component> </component>
@ -169,10 +117,10 @@
<other-services-enabled>false</other-services-enabled> <other-services-enabled>false</other-services-enabled>
</component> </component>
<component name="ProjectFrameBounds"> <component name="ProjectFrameBounds">
<option name="x" value="1920" /> <option name="x" value="1680" />
<option name="y" value="-438" /> <option name="y" value="-410" />
<option name="width" value="1080" /> <option name="width" value="1050" />
<option name="height" value="1897" /> <option name="height" value="1657" />
</component> </component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true"> <component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="1" id="Add" /> <ConfirmationsSetting value="1" id="Add" />
@ -192,43 +140,12 @@
<path> <path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" /> <item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" /> <item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="add" type="9f88c78c:ScopeViewTreeModel$FileNode" /> <item name="smart_pointers" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path> </path>
<path> <path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" /> <item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" /> <item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="add" type="9f88c78c:ScopeViewTreeModel$FileNode" /> <item name="smart_pointers" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="add-one" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="add" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="add-one" 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="add" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="adder" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="add" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="adder" 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="minigrep_v2" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="minigrep_v2" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" /> <item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path> </path>
</expand> </expand>
@ -262,7 +179,7 @@
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" /> <property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549304605543" /> <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549379395077" />
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" /> <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.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
<property name="js.eslint.nodeInterpreter" value="project" /> <property name="js.eslint.nodeInterpreter" value="project" />
@ -290,7 +207,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="RunManager" selected="Cargo Command.Test lib::tests (1)"> <component name="RunManager" selected="Cargo Command.Run smart_pointers">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false"> <configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" /> <option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" /> <option name="VM_PARAMETERS" value="-ea" />
@ -309,13 +226,13 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="Run closures" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true"> <configuration name="Run smart_pointers" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" /> <option name="channel" value="DEFAULT" />
<option name="command" value="run --package closures --bin closures" /> <option name="command" value="run --package smart_pointers --bin smart_pointers" />
<option name="allFeatures" value="false" /> <option name="allFeatures" value="false" />
<option name="nocapture" value="false" /> <option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" /> <option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/closures" /> <option name="workingDirectory" value="file://$PROJECT_DIR$/smart_pointers" />
<envs /> <envs />
<method v="2" /> <method v="2" />
</configuration> </configuration>
@ -368,11 +285,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Cargo Command.Run smart_pointers" />
<item itemvalue="Cargo Command.Test lib::tests (1)" /> <item itemvalue="Cargo Command.Test lib::tests (1)" />
<item itemvalue="Cargo Command.Test using_other_iterator_trait_methods" /> <item itemvalue="Cargo Command.Test using_other_iterator_trait_methods" />
<item itemvalue="Cargo Command.Test calling_next_directly" /> <item itemvalue="Cargo Command.Test calling_next_directly" />
<item itemvalue="Cargo Command.Test filters_by_size" /> <item itemvalue="Cargo Command.Test filters_by_size" />
<item itemvalue="Cargo Command.Run closures" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -406,11 +323,11 @@
</history-entry> </history-entry>
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" /> <frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
<editor active="true" /> <editor active="true" />
<layout> <layout>
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.4968701" visible="true" weight="0.2540132" /> <window_info content_ui="combo" id="Project" order="0" sideWeight="0.49586776" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.5031299" side_tool="true" visible="true" weight="0.2540132" /> <window_info id="Structure" order="1" sideWeight="0.5041322" side_tool="true" visible="true" weight="0.26141885" />
<window_info id="Image Layers" order="2" /> <window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" /> <window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" /> <window_info id="Capture Tool" order="4" />
@ -418,7 +335,7 @@
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" /> <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="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" /> <window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29737285" /> <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29696578" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <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="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -426,7 +343,7 @@
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" /> <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="Version Control" order="8" />
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" /> <window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
<window_info anchor="bottom" id="Terminal" order="10" visible="true" weight="0.28563443" /> <window_info anchor="bottom" id="Terminal" order="10" weight="0.66429955" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" /> <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="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
@ -798,8 +715,8 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/add/add-one/src/lib.rs"> <entry file="file://$PROJECT_DIR$/add/add-one/src/lib.rs">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="208"> <state relative-caret-position="240">
<caret line="13" column="5" lean-forward="true" selection-start-line="13" selection-start-column="5" selection-end-line="13" selection-end-column="5" /> <caret line="15" lean-forward="true" selection-start-line="15" selection-end-line="15" />
<folding> <folding>
<element signature="e#30#31#0" expanded="true" /> <element signature="e#30#31#0" expanded="true" />
<element signature="e#40#41#0" expanded="true" /> <element signature="e#40#41#0" expanded="true" />
@ -809,5 +726,25 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/smart_pointers/src/lib.rs">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/smart_pointers/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="32">
<caret line="2" lean-forward="true" selection-start-line="2" selection-end-line="2" />
<folding>
<element signature="e#68#69#0" expanded="true" />
<element signature="e#91#92#0" expanded="true" />
<element signature="e#200#201#0" expanded="true" />
<element signature="e#216#217#0" expanded="true" />
<element signature="e#351#352#0" expanded="true" />
<element signature="e#433#434#0" expanded="true" />
<element signature="e#244#245#0" expanded="true" />
<element signature="e#261#262#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component> </component>
</project> </project>

View File

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

View File

@ -0,0 +1,56 @@
use std::ops::Deref;
use std::mem::drop;
struct MyBox<T>(T);
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}
impl<T> Deref for MyBox<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop (&mut self) {
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
}
}
fn hello(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
let x = 5;
let y = MyBox::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
// Deref trait
let m = MyBox::new(String::from("Rust"));
hello(&m); // hello(&(*m)[..]); without deref coercions
// Drop trait
let c = CustomSmartPointer { data: String::from("my stuff") };
println!("CustomSmartPointer created.");
// Manual early "destruction" with std::mem::drop, instead of
// automatic cleanup via the Drop trait
drop(c);
println!("CustomSmartPointer dropped before the end of main.");
let _d = CustomSmartPointer { data: String::from("other stuff") };
println!("CustomSmartPointer created.");
}