Add advanced_lifetimes example

This commit is contained in:
Timothy Warren 2019-02-07 12:28:51 -05:00
parent 6cf7aa9000
commit 13233d5eb1
6 changed files with 134 additions and 57 deletions

View File

@ -37,6 +37,7 @@
<cargoProject FILE="$PROJECT_DIR$/blog/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/pattern_matching/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/unsafe_blocks/Cargo.toml" />
<cargoProject FILE="$PROJECT_DIR$/advanced_lifetimes/Cargo.toml" />
</component>
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>

View File

@ -149,9 +149,14 @@
<sourceFolder url="file://$MODULE_DIR$/unsafe_blocks/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/unsafe_blocks/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/unsafe_blocks/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/advanced_lifetimes/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/advanced_lifetimes/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/advanced_lifetimes/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/advanced_lifetimes/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" />
<excludeFolder url="file://$MODULE_DIR$/advanced_lifetimes/target" />
<excludeFolder url="file://$MODULE_DIR$/blog/target" />
<excludeFolder url="file://$MODULE_DIR$/branches/target" />
<excludeFolder url="file://$MODULE_DIR$/closures/target" />

View File

@ -2,8 +2,6 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/unsafe_blocks/Cargo.toml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/unsafe_blocks/src/main.rs" afterDir="false" />
<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" />
@ -16,11 +14,28 @@
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/unsafe_blocks/src/main.rs">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/advanced_lifetimes/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="552">
<caret line="40" column="56" selection-start-line="40" selection-start-column="56" selection-end-line="40" selection-end-column="56" />
<state relative-caret-position="272">
<caret line="17" column="20" selection-start-line="17" selection-start-column="20" selection-end-line="17" selection-end-column="20" />
<folding>
<element signature="e#237#238#0" expanded="true" />
<element signature="e#277#278#0" expanded="true" />
<element signature="e#337#338#0" expanded="true" />
<element signature="e#380#381#0" expanded="true" />
<element signature="e#438#439#0" expanded="true" />
<element signature="e#474#475#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/advanced_lifetimes/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="160">
<caret line="10" column="59" selection-start-line="10" selection-start-column="59" selection-end-line="10" selection-end-column="59" />
</state>
</provider>
</entry>
@ -54,8 +69,6 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/enums/src/main.rs" />
<option value="$PROJECT_DIR$/modules/src/sound.rs" />
<option value="$PROJECT_DIR$/modules/src/performance_group.rs" />
<option value="$PROJECT_DIR$/modules/src/main.rs" />
<option value="$PROJECT_DIR$/modules/src/sound/instrument.rs" />
@ -105,6 +118,8 @@
<option value="$PROJECT_DIR$/blog/Cargo.toml" />
<option value="$PROJECT_DIR$/pattern_matching/src/main.rs" />
<option value="$PROJECT_DIR$/unsafe_blocks/src/main.rs" />
<option value="$PROJECT_DIR$/advanced_lifetimes/src/lib.rs" />
<option value="$PROJECT_DIR$/advanced_lifetimes/src/main.rs" />
</list>
</option>
</component>
@ -138,12 +153,12 @@
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="unsafe_blocks" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="advanced_lifetimes" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
<path>
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
<item name="unsafe_blocks" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="advanced_lifetimes" type="9f88c78c:ScopeViewTreeModel$FileNode" />
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
</path>
</expand>
@ -177,7 +192,7 @@
</component>
<component name="PropertiesComponent">
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549557641362" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549560474400" />
<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" />
@ -205,7 +220,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Cargo Command.Run unsafe_blocks">
<component name="RunManager" selected="Cargo Command.Run advanced_lifetimes">
<configuration name="&lt;template&gt;" type="TestNG" default="true" selected="false">
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" value="-ea" />
@ -224,23 +239,13 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Run blog" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<configuration name="Run advanced_lifetimes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package blog --bin blog" />
<option name="command" value="run --package advanced_lifetimes --bin advanced_lifetimes" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/blog" />
<envs />
<method v="2" />
</configuration>
<configuration name="Run gui" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="run --package gui --bin gui" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/gui" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_lifetimes" />
<envs />
<method v="2" />
</configuration>
@ -266,11 +271,21 @@
</configuration>
<configuration name="Test lib::tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package oop --lib tests" />
<option name="command" value="test --package advanced_lifetimes --lib tests" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/oop" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_lifetimes" />
<envs />
<method v="2" />
</configuration>
<configuration name="Test tests::it_works" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="channel" value="DEFAULT" />
<option name="command" value="test --package advanced_lifetimes --lib tests::it_works -- --exact" />
<option name="allFeatures" value="false" />
<option name="nocapture" value="false" />
<option name="backtrace" value="SHORT" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_lifetimes" />
<envs />
<method v="2" />
</configuration>
@ -283,21 +298,15 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo Command.Run advanced_lifetimes" />
<item itemvalue="Cargo Command.Test lib::tests" />
<item itemvalue="Cargo Command.Test tests::it_works" />
<item itemvalue="Cargo Command.Run unsafe_blocks" />
<item itemvalue="Cargo Command.Run pattern_matching" />
<item itemvalue="Cargo Command.Run blog" />
<item itemvalue="Cargo Command.Run gui" />
<item itemvalue="Cargo Command.Test lib::tests" />
</list>
</recent_temporary>
</component>
<component name="TestHistory">
<history-entry file="Test_tests__greeting_contains_name - 2019.01.31 at 14h 18m 57s.xml">
<configuration name="Test tests::greeting_contains_name" configurationId="CargoCommandRunConfiguration" />
</history-entry>
<history-entry file="Test_tests__greeting_contains_name - 2019.01.31 at 14h 19m 31s.xml">
<configuration name="Test tests::greeting_contains_name" configurationId="CargoCommandRunConfiguration" />
</history-entry>
<history-entry file="Test_lib__tests - 2019.01.31 at 14h 20m 55s.xml">
<configuration name="Test lib::tests" configurationId="CargoCommandRunConfiguration" />
</history-entry>
@ -322,6 +331,12 @@
<history-entry file="Test_lib__tests - 2019.02.06 at 13h 49m 17s.xml">
<configuration name="Test lib::tests" configurationId="CargoCommandRunConfiguration" />
</history-entry>
<history-entry file="Test_tests__it_works - 2019.02.07 at 12h 12m 47s.xml">
<configuration name="Test tests::it_works" configurationId="CargoCommandRunConfiguration" />
</history-entry>
<history-entry file="Test_lib__tests - 2019.02.07 at 12h 16m 54s.xml">
<configuration name="Test lib::tests" configurationId="CargoCommandRunConfiguration" />
</history-entry>
</component>
<component name="ToolWindowManager">
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
@ -344,7 +359,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.3428018" />
<window_info anchor="bottom" id="Terminal" order="10" 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" />
@ -358,24 +373,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/slices/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="704">
<caret line="44" selection-start-line="44" selection-end-line="44" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/rectangles/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="704">
<caret line="44" lean-forward="true" selection-start-line="44" selection-end-line="44" />
<folding>
<element signature="e#133#134#0" expanded="true" />
<element signature="e#176#177#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/.idea/workspace.xml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="368">
@ -791,8 +788,30 @@
</entry>
<entry file="file://$PROJECT_DIR$/unsafe_blocks/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="552">
<caret line="40" column="56" selection-start-line="40" selection-start-column="56" selection-end-line="40" selection-end-column="56" />
<state relative-caret-position="448">
<caret line="28" column="50" selection-start-line="28" selection-start-column="50" selection-end-line="28" selection-end-column="50" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/advanced_lifetimes/src/lib.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="272">
<caret line="17" column="20" selection-start-line="17" selection-start-column="20" selection-end-line="17" selection-end-column="20" />
<folding>
<element signature="e#237#238#0" expanded="true" />
<element signature="e#277#278#0" expanded="true" />
<element signature="e#337#338#0" expanded="true" />
<element signature="e#380#381#0" expanded="true" />
<element signature="e#438#439#0" expanded="true" />
<element signature="e#474#475#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/advanced_lifetimes/src/main.rs">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="160">
<caret line="10" column="59" selection-start-line="10" selection-start-column="59" selection-end-line="10" selection-end-column="59" />
</state>
</provider>
</entry>

View File

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

View File

@ -0,0 +1,26 @@
struct Context<'s>(&'s str);
// 's : 'c tells Rust that the 's lifetime is tied to the 'c lifetime
struct Parser<'c, 's : 'c> {
context: &'c Context<'s>,
}
impl<'c, 's> Parser<'c, 's> {
fn parse(&self) -> Result<(), &'s str> {
Err(&self.context.0[1..])
}
}
fn parse_context(context: Context) -> Result<(), &str> {
Parser { context: &context }.parse()
}
struct Ref<'a, T: 'a>(&'a T);
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -0,0 +1,19 @@
trait Red {}
struct Ball<'a> {
diameter: &'a i32,
}
impl<'a> Red for Ball<'a> {}
struct StrWrap<'a>(&'a str);
fn foo(string: &str) -> StrWrap<'_> { // Anonymous lifetime
StrWrap(string)
}
fn main() {
let num = 5;
let obj = Box::new(Ball { diameter: &num}) as Box<dyn Red>;
}