Add reference_cycles example
This commit is contained in:
parent
fcd4ecceb1
commit
971140b6ff
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -27,6 +27,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/smart_pointers/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/reference_counting/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/interior_mutability/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/reference_cycles/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
5
.idea/rust.iml
generated
5
.idea/rust.iml
generated
@ -110,6 +110,10 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/interior_mutability/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/interior_mutability/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/interior_mutability/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/reference_cycles/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" />
|
||||
@ -130,6 +134,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/modules/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/rectangles/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_counting/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/reference_cycles/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/references/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/scope/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/slices/target" />
|
||||
|
72
.idea/workspace.xml
generated
72
.idea/workspace.xml
generated
@ -5,7 +5,6 @@
|
||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/rust.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/rust.iml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/reference_counting/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/reference_counting/src/main.rs" afterDir="false" />
|
||||
</list>
|
||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@ -15,24 +14,11 @@
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/interior_mutability/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="672">
|
||||
<caret line="55" column="69" selection-start-line="55" selection-start-column="69" selection-end-line="55" selection-end-column="69" />
|
||||
<folding>
|
||||
<element signature="e#1104#1105#0" expanded="true" />
|
||||
<element signature="e#1144#1145#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/reference_counting/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/reference_cycles/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="320">
|
||||
<caret line="20" column="15" selection-start-line="20" selection-start-column="15" selection-end-line="20" selection-end-column="15" />
|
||||
<state relative-caret-position="160">
|
||||
<caret line="10" column="11" lean-forward="true" selection-start-line="10" selection-start-column="11" selection-end-line="10" selection-end-column="11" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -108,6 +94,7 @@
|
||||
<option value="$PROJECT_DIR$/smart_pointers/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/interior_mutability/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/reference_counting/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/reference_cycles/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -141,12 +128,12 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="reference_counting" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="reference_cycles" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="reference_counting" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="reference_cycles" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
</expand>
|
||||
@ -180,7 +167,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549381304560" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549383008644" />
|
||||
<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" />
|
||||
@ -208,7 +195,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run reference_counting">
|
||||
<component name="RunManager" selected="Cargo Command.Run reference_cycles">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -237,6 +224,16 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run reference_cycles" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package reference_cycles --bin reference_cycles" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/reference_cycles" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run smart_pointers" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package smart_pointers --bin smart_pointers" />
|
||||
@ -247,16 +244,6 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Test lib::tests (1)" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="test --package minigrep_v2 --lib tests" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/minigrep_v2" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="test --package interior_mutability --lib tests" />
|
||||
@ -286,11 +273,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run reference_cycles" />
|
||||
<item itemvalue="Cargo Command.Run reference_counting" />
|
||||
<item itemvalue="Cargo Command.Test lib::tests" />
|
||||
<item itemvalue="Cargo Command.Test tests::it_sends_an_over_75_percent_warning_message" />
|
||||
<item itemvalue="Cargo Command.Run smart_pointers" />
|
||||
<item itemvalue="Cargo Command.Test lib::tests (1)" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -330,8 +317,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 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" />
|
||||
@ -339,7 +326,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" />
|
||||
@ -749,9 +736,16 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/reference_counting/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="448">
|
||||
<caret line="28" lean-forward="true" selection-start-line="28" selection-end-line="28" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/interior_mutability/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="672">
|
||||
<state relative-caret-position="679">
|
||||
<caret line="55" column="69" selection-start-line="55" selection-start-column="69" selection-end-line="55" selection-end-column="69" />
|
||||
<folding>
|
||||
<element signature="e#1104#1105#0" expanded="true" />
|
||||
@ -760,10 +754,10 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/reference_counting/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/reference_cycles/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="320">
|
||||
<caret line="20" column="15" selection-start-line="20" selection-start-column="15" selection-end-line="20" selection-end-column="15" />
|
||||
<state relative-caret-position="160">
|
||||
<caret line="10" column="11" lean-forward="true" selection-start-line="10" selection-start-column="11" selection-end-line="10" selection-end-column="11" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
7
reference_cycles/Cargo.toml
Normal file
7
reference_cycles/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "reference_cycles"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
98
reference_cycles/src/main.rs
Normal file
98
reference_cycles/src/main.rs
Normal file
@ -0,0 +1,98 @@
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::cell::RefCell;
|
||||
use List::{Cons, Nil};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum List {
|
||||
Cons(i32, RefCell<Rc<List>>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
impl List {
|
||||
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
|
||||
match self {
|
||||
Cons(_, item) => Some(item),
|
||||
Nil => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cycle_example() {
|
||||
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
|
||||
|
||||
println!("a initial rc count = {}", Rc::strong_count(&a));
|
||||
println!("a next item = {:?}", a.tail());
|
||||
|
||||
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
|
||||
|
||||
println!("a rc count after b creation = {}", Rc::strong_count(&a));
|
||||
println!("b initial rc count = {}", Rc::strong_count(&b));
|
||||
println!("b next item = {:?}", b.tail());
|
||||
|
||||
if let Some(link) = a.tail() {
|
||||
*link.borrow_mut() = Rc::clone(&b);
|
||||
}
|
||||
|
||||
println!("b rc count after changing a = {}", Rc::strong_count(&b));
|
||||
println!("a rc count after changing a = {}", Rc::strong_count(&a));
|
||||
|
||||
// Uncomment the next line to see that we have a cycle;
|
||||
// it will overflow the stack
|
||||
// println!("a next item = {:?}", a.tail());
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node {
|
||||
value: i32,
|
||||
parent: RefCell<Weak<Node>>,
|
||||
children: RefCell<Vec<Rc<Node>>>,
|
||||
}
|
||||
|
||||
fn weakref_example() {
|
||||
let leaf = Rc::new(Node {
|
||||
value: 3,
|
||||
parent: RefCell::new(Weak::new()),
|
||||
children: RefCell::new(vec![]),
|
||||
});
|
||||
|
||||
println!(
|
||||
"leaf strong = {}, weak = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
);
|
||||
|
||||
{
|
||||
let branch = Rc::new(Node {
|
||||
value: 5,
|
||||
parent: RefCell::new(Weak::new()),
|
||||
children: RefCell::new(vec![Rc::clone(&leaf)]),
|
||||
});
|
||||
|
||||
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
||||
|
||||
println!(
|
||||
"branch strong = {}, weak = {}",
|
||||
Rc::strong_count(&branch),
|
||||
Rc::weak_count(&branch),
|
||||
);
|
||||
|
||||
println!(
|
||||
"leaf strong = {}, weak = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
);
|
||||
}
|
||||
|
||||
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
||||
println!(
|
||||
"leaf strong = {}, weak = {}",
|
||||
Rc::strong_count(&leaf),
|
||||
Rc::weak_count(&leaf),
|
||||
);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
cycle_example();
|
||||
|
||||
weakref_example();
|
||||
}
|
Loading…
Reference in New Issue
Block a user