Add basic oop example

This commit is contained in:
Timothy Warren 2019-02-06 13:49:34 -05:00
parent 7a575c7f58
commit ec147dce21
5 changed files with 112 additions and 40 deletions

View File

@ -30,6 +30,8 @@
<cargoProject FILE="$PROJECT_DIR$/reference_cycles/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/threads/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/mutex/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/oop/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/twelve_days_of_christmas/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

View File

@ -122,6 +122,14 @@
<sourceFolder url="file://$MODULE_DIR$/mutex/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/mutex/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/mutex/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/oop/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/oop/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/oop/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/oop/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/twelve_days_of_christmas/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/twelve_days_of_christmas/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/twelve_days_of_christmas/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/twelve_days_of_christmas/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" />
@ -141,6 +149,7 @@
<excludeFolder url="file://$MODULE_DIR$/minigrep_v2/target" />
<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$/rectangles/target" />
<excludeFolder url="file://$MODULE_DIR$/reference_counting/target" />
<excludeFolder url="file://$MODULE_DIR$/reference_cycles/target" />
@ -150,6 +159,7 @@
<excludeFolder url="file://$MODULE_DIR$/smart_pointers/target" />
<excludeFolder url="file://$MODULE_DIR$/structs/target" />
<excludeFolder url="file://$MODULE_DIR$/threads/target" />
<excludeFolder url="file://$MODULE_DIR$/twelve_days_of_christmas/target" />
<excludeFolder url="file://$MODULE_DIR$/variables/target" />
</content>
<orderEntry type="inheritedJdk" />

View File

@ -15,10 +15,14 @@
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/mutex/src/main.rs">
<entry file="file://$PROJECT_DIR$/oop/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state>
<caret column="91" selection-start-column="91" selection-end-column="91" />
<state relative-caret-position="784">
<caret line="49" column="29" selection-start-line="49" selection-start-column="29" selection-end-line="49" selection-end-column="29" />
<folding>
<element signature="e#510#511#0" expanded="true" />
<element signature="e#537#538#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
@ -36,10 +40,12 @@
<findStrings>
<find>ftoc</find>
<find>expensive_closure(intensity)</find>
<find>ordinals</find>
</findStrings>
<replaceStrings>
<replace>f_to_c</replace>
<replace>expensive_result.value(intensity)</replace>
<replace>ORDINALS</replace>
</replaceStrings>
</component>
<component name="Git.Settings">
@ -48,7 +54,6 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/variables/src/main.rs" />
<option value="$PROJECT_DIR$/functions/src/main.rs" />
<option value="$PROJECT_DIR$/branches/src/main.rs" />
<option value="$PROJECT_DIR$/ctof/src/main.rs" />
@ -97,6 +102,9 @@
<option value="$PROJECT_DIR$/reference_cycles/src/main.rs" />
<option value="$PROJECT_DIR$/threads/src/main.rs" />
<option value="$PROJECT_DIR$/mutex/src/main.rs" />
<option value="$PROJECT_DIR$/twelve_days_of_christmas/Cargo.toml" />
<option value="$PROJECT_DIR$/twelve_days_of_christmas/src/main.rs" />
<option value="$PROJECT_DIR$/oop/src/lib.rs" />
</list>
</option>
</component>
@ -127,17 +135,6 @@
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="mutex" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="mutex" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand>
<select />
</subPane>
@ -169,7 +166,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549394084677" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549478828057" />
<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" />
@ -197,7 +194,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Cargo Command.Run mutex">
<component name="RunManager" selected="Cargo Command.Run twelve_days_of_christmas">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" />
@ -256,13 +253,13 @@
<envs />
<method v="2" />
</configuration>
<configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<configuration name="Run twelve_days_of_christmas" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package interior_mutability --lib tests" />
<option name="command" value="run --package twelve_days_of_christmas --bin twelve_days_of_christmas" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/interior_mutability" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/twelve_days_of_christmas" />
<envs />
<method v="2" />
</configuration>
@ -275,11 +272,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo Command.Run twelve_days_of_christmas" />
<item itemvalue="Cargo Command.Run mutex" />
<item itemvalue="Cargo Command.Run threads" />
<item itemvalue="Cargo Command.Run reference_cycles" />
<item itemvalue="Cargo Command.Run reference_counting" />
<item itemvalue="Cargo Command.Test lib::tests" />
</list>
</recent_temporary>
</component>
@ -319,8 +316,8 @@
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
<editor active="true" />
<layout>
<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.5041322" side_tool="true" visible="true" weight="0.26141885" />
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49644932" visible="true" weight="0.26141885" />
<window_info id="Structure" order="1" sideWeight="0.50355065" side_tool="true" visible="true" weight="0.26141885" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="Capture Tool" order="4" />
@ -328,7 +325,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.29696578" />
<window_info anchor="bottom" id="Run" order="2" weight="0.29696578" />
<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,20 +347,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/variables/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" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/functions/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="128">
<caret line="8" column="41" selection-start-line="8" selection-start-column="41" selection-end-line="8" selection-end-column="41" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/branches/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="160">
@ -772,8 +755,33 @@
</entry>
<entry file="file://$PROJECT_DIR$/mutex/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state>
<caret column="91" selection-start-column="91" selection-end-column="91" />
<state relative-caret-position="640">
<caret line="40" selection-start-line="40" selection-end-line="40" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/twelve_days_of_christmas/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="112">
<caret line="7" column="27" lean-forward="true" selection-start-line="7" selection-start-column="27" selection-end-line="7" selection-end-column="27" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/twelve_days_of_christmas/Cargo.toml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="32">
<caret line="2" column="13" selection-start-line="2" selection-start-column="13" selection-end-line="2" selection-end-column="13" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/oop/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="784">
<caret line="49" column="29" selection-start-line="49" selection-start-column="29" selection-end-line="49" selection-end-column="29" />
<folding>
<element signature="e#510#511#0" expanded="true" />
<element signature="e#537#538#0" expanded="true" />
</folding>
</state>
</provider>
</entry>

7
oop/Cargo.toml Normal file
View File

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

45
oop/src/lib.rs Normal file
View File

@ -0,0 +1,45 @@
/// Example of encapsulation, as a
/// requirement of Object Oriented Programming.
///
/// The list property is private, eg, encapsulated,
/// while the object itself handles manipulation of
/// the list property.
pub struct AveragedCollection {
list: Vec<i32>,
average: f64,
}
impl AveragedCollection {
pub fn add(&mut self, value: i32) {
self.list.push(value);
self.update_average();
}
pub fn remove(&mut self) -> Option<i32> {
let result = self.list.pop();
match result {
Some(value) => {
self.update_average();
Some(value)
},
None => None,
}
}
pub fn average(&self) -> f64 {
self.average
}
fn update_average(&mut self) {
let total: i32 = self.list.iter().sum();
self.average = total as f64 / self.list.len() as f64;
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}