From 6334e5627ac22edae98861a894480cc4e31038b7 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Tue, 8 Dec 2020 13:19:47 +0100 Subject: [PATCH] threads and oop --- blog/Cargo.toml | 14 ++++++ blog/src/lib.rs | 109 ++++++++++++++++++++++++++++++++++++++++++ blog/src/main.rs | 46 ++++++++++++++++++ deadlocks/Cargo.toml | 9 ++++ deadlocks/src/main.rs | 33 +++++++++++++ oop/Cargo.toml | 13 +++++ oop/src/lib.rs | 27 +++++++++++ oop/src/main.rs | 36 ++++++++++++++ threads/Cargo.toml | 9 ++++ threads/src/main.rs | 23 +++++++++ 10 files changed, 319 insertions(+) create mode 100644 blog/Cargo.toml create mode 100644 blog/src/lib.rs create mode 100644 blog/src/main.rs create mode 100644 deadlocks/Cargo.toml create mode 100644 deadlocks/src/main.rs create mode 100644 oop/Cargo.toml create mode 100644 oop/src/lib.rs create mode 100644 oop/src/main.rs create mode 100644 threads/Cargo.toml create mode 100644 threads/src/main.rs diff --git a/blog/Cargo.toml b/blog/Cargo.toml new file mode 100644 index 0000000..97da10c --- /dev/null +++ b/blog/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "blog" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +[lib] +name = "blog" +path = "src/lib.rs" + + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/blog/src/lib.rs b/blog/src/lib.rs new file mode 100644 index 0000000..4cb14f9 --- /dev/null +++ b/blog/src/lib.rs @@ -0,0 +1,109 @@ +pub struct Post { + content: String, +} + +pub struct DraftPost { + content: String, +} + +impl Post { + pub fn new() -> DraftPost { + DraftPost { + content: String::new(), + } + } + + pub fn content(&self) -> &str { + &self.content + } +} + +impl DraftPost { + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn request_review(self) -> PendingReviewPost { + PendingReviewPost { + content: self.content, + pre_approved: false, + } + } +} + +pub struct PendingReviewPost { + content: String, + pre_approved: bool, +} + +pub enum PendingOrPost { + PendingReviewPost(PendingReviewPost), + Post(Post), +} + +impl PendingReviewPost { + pub fn approve(mut self) -> PendingOrPost { + if self.pre_approved { + PendingOrPost::Post(Post { + content: self.content, + }) + } else { + self.pre_approved = true; + PendingOrPost::PendingReviewPost(self) + } + } + + pub fn reject(self) -> DraftPost { + DraftPost { + content: self.content, + } + } +} + +// trait State { +// fn request_review(self: Box) -> Box; +// fn approve(self: Box) -> Box; +// fn content<'a>(&self, _post: &'a Post) -> &'a str { +// "" +// } +// } + +// struct Draft {} + +// impl State for Draft { +// fn request_review(self: Box) -> Box { +// Box::new(PendingReview {}) +// } + +// fn approve(self: Box) -> Box { +// self +// } +// } + +// struct PendingReview {} + +// impl State for PendingReview { +// fn request_review(self: Box) -> Box { +// self +// } + +// fn approve(self: Box) -> Box { +// Box::new(Published {}) +// } +// } + +// struct Published {} + +// impl State for Published { +// fn request_review(self: Box) -> Box { +// self +// } + +// fn approve(self: Box) -> Box { +// self +// } + +// fn content<'a>(&self, post: &'a Post) -> &'a str { +// &post.content +// } +// } \ No newline at end of file diff --git a/blog/src/main.rs b/blog/src/main.rs new file mode 100644 index 0000000..b8d7e06 --- /dev/null +++ b/blog/src/main.rs @@ -0,0 +1,46 @@ +use blog::Post; + +fn main() { + let mut post = Post::new(); + + post.add_text("I ate a salad for lunch today"); + + let post = post.request_review(); + + let post = if let blog::PendingOrPost::PendingReviewPost(c) = post.approve() { + c + } else { + panic!("Didn't get PendingReviewPost after first call to approve"); + }; + let mut post = post.reject(); + post.add_text(" and it was good!"); + + let post = post.request_review(); + let post = if let blog::PendingOrPost::PendingReviewPost(c) = post.approve() { + c + } else { + panic!("Didn't get PendingReviewPost after first call to approve"); + }; + let post = if let blog::PendingOrPost::Post(c) = post.approve() { + c + } else { + panic!("Didn't get Post after first call to approve"); + }; + + assert_eq!("I ate a salad for lunch today and it was good!", post.content()); +} + +// use blog::Post; + +// fn main() { +// let mut post = Post::new(); + +// post.add_text("I ate a salad for lunch today"); +// assert_eq!("", post.content()); + +// post.request_review(); +// assert_eq!("", post.content()); + +// post.approve(); +// assert_eq!("I ate a salad for lunch today", post.content()); +// } \ No newline at end of file diff --git a/deadlocks/Cargo.toml b/deadlocks/Cargo.toml new file mode 100644 index 0000000..0eff721 --- /dev/null +++ b/deadlocks/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "deadlocks" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/deadlocks/src/main.rs b/deadlocks/src/main.rs new file mode 100644 index 0000000..e0700fb --- /dev/null +++ b/deadlocks/src/main.rs @@ -0,0 +1,33 @@ +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; + +fn main() { + let mutex1 = Arc::new(Mutex::new(0)); + let mutex2 = Arc::new(Mutex::new(0)); + let mutex3 = Arc::clone(&mutex1); + let mutex4 = Arc::clone(&mutex2); + let mutex5 = Arc::clone(&mutex1); + let mutex6 = Arc::clone(&mutex2); + + let handle1 = thread::spawn(move || { + let mut num1 = mutex1.lock().unwrap(); + *num1 = 1; + thread::sleep(Duration::from_secs(1)); + let mut num2 = mutex2.lock().unwrap(); + *num2 = 22; + }); + + let handle2 = thread::spawn(move || { + let mut num2 = mutex4.lock().unwrap(); + *num2 = 2; + thread::sleep(Duration::from_secs(1)); + let mut num1 = mutex3.lock().unwrap(); + *num1 = 11; + }); + + handle1.join().unwrap(); + handle2.join().unwrap(); + + println!("mutex1: {:?}, mutex2: {:?}", mutex5, mutex6); +} \ No newline at end of file diff --git a/oop/Cargo.toml b/oop/Cargo.toml new file mode 100644 index 0000000..88f1ffa --- /dev/null +++ b/oop/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "oop" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +name = "gui" +path = "src/lib.rs" + +[dependencies] diff --git a/oop/src/lib.rs b/oop/src/lib.rs new file mode 100644 index 0000000..ae8010b --- /dev/null +++ b/oop/src/lib.rs @@ -0,0 +1,27 @@ +pub trait Draw { + fn draw(&self); +} + +pub struct Screen { + pub components: Vec>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +pub struct Button { + pub width: u32, + pub height: u32, + pub label: String, +} + +impl Draw for Button { + fn draw(&self) { + println!("Button drawn!"); + } +} diff --git a/oop/src/main.rs b/oop/src/main.rs new file mode 100644 index 0000000..9ec570f --- /dev/null +++ b/oop/src/main.rs @@ -0,0 +1,36 @@ +use gui::{Draw, Button, Screen}; + +struct SelectBox { + width: u32, + height: u32, + options: Vec, +} + +impl Draw for SelectBox { + fn draw(&self) { + println!("SelectBox drawn!"); + } +} + +fn main() { + let screen = Screen { + components: vec![ + Box::new(SelectBox { + width: 75, + height: 10, + options: vec![ + String::from("Yes"), + String::from("Maybe"), + String::from("No"), + ], + }), + Box::new(Button { + width: 50, + height: 10, + label: String::from("OK"), + }), + ], + }; + + screen.run(); +} \ No newline at end of file diff --git a/threads/Cargo.toml b/threads/Cargo.toml new file mode 100644 index 0000000..d0b174c --- /dev/null +++ b/threads/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "threads" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/threads/src/main.rs b/threads/src/main.rs new file mode 100644 index 0000000..c85d332 --- /dev/null +++ b/threads/src/main.rs @@ -0,0 +1,23 @@ +use std::sync::{Arc, Mutex}; +use std::thread; + +fn main() { + let counter = Arc::new(Mutex::new(0)); + let mut handles = vec![]; + + for _ in 0..10 { + let counter = Arc::clone(&counter); + let handle = thread::spawn(move || { + let mut num = counter.lock().unwrap(); + + *num += 1; + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + println!("Result: {}", *counter.lock().unwrap()); +} \ No newline at end of file