From 9b0fff8c9fc38bd75de5e38c920800c749a79177 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 5 Feb 2019 10:10:47 -0500 Subject: [PATCH] Add smart_pointers example --- .idea/misc.xml | 1 + .idea/rust.iml | 5 ++ .idea/workspace.xml | 171 ++++++++++++------------------------- smart_pointers/Cargo.toml | 7 ++ smart_pointers/src/main.rs | 56 ++++++++++++ 5 files changed, 123 insertions(+), 117 deletions(-) create mode 100644 smart_pointers/Cargo.toml create mode 100644 smart_pointers/src/main.rs diff --git a/.idea/misc.xml b/.idea/misc.xml index ee60ddf..8ca6f05 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,6 +24,7 @@ + diff --git a/.idea/rust.iml b/.idea/rust.iml index 3d5de12..bfb4a8b 100644 --- a/.idea/rust.iml +++ b/.idea/rust.iml @@ -98,6 +98,10 @@ + + + + @@ -119,6 +123,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 935ec18..7405fa5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -14,82 +14,28 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - - - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -159,6 +105,8 @@ @@ -169,10 +117,10 @@ false - @@ -192,43 +140,12 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -262,7 +179,7 @@ - + @@ -290,7 +207,7 @@ - + - + @@ -368,11 +285,11 @@ + - @@ -406,11 +323,11 @@ - + - - + + @@ -418,7 +335,7 @@ - + @@ -426,7 +343,7 @@ - + @@ -798,8 +715,8 @@ - - + + @@ -809,5 +726,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/smart_pointers/Cargo.toml b/smart_pointers/Cargo.toml new file mode 100644 index 0000000..d19c946 --- /dev/null +++ b/smart_pointers/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "smart_pointers" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] diff --git a/smart_pointers/src/main.rs b/smart_pointers/src/main.rs new file mode 100644 index 0000000..4654021 --- /dev/null +++ b/smart_pointers/src/main.rs @@ -0,0 +1,56 @@ +use std::ops::Deref; +use std::mem::drop; + +struct MyBox(T); + +impl MyBox { + fn new(x: T) -> MyBox { + MyBox(x) + } +} + +impl Deref for MyBox { + 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."); +}