threads and oop

This commit is contained in:
gbrochar 2020-12-08 13:19:47 +01:00
parent 823321ee27
commit 6334e5627a
10 changed files with 319 additions and 0 deletions

14
blog/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "blog"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
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]

109
blog/src/lib.rs Normal file
View File

@ -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<Self>) -> Box<dyn State>;
// fn approve(self: Box<Self>) -> Box<dyn State>;
// fn content<'a>(&self, _post: &'a Post) -> &'a str {
// ""
// }
// }
// struct Draft {}
// impl State for Draft {
// fn request_review(self: Box<Self>) -> Box<dyn State> {
// Box::new(PendingReview {})
// }
// fn approve(self: Box<Self>) -> Box<dyn State> {
// self
// }
// }
// struct PendingReview {}
// impl State for PendingReview {
// fn request_review(self: Box<Self>) -> Box<dyn State> {
// self
// }
// fn approve(self: Box<Self>) -> Box<dyn State> {
// Box::new(Published {})
// }
// }
// struct Published {}
// impl State for Published {
// fn request_review(self: Box<Self>) -> Box<dyn State> {
// self
// }
// fn approve(self: Box<Self>) -> Box<dyn State> {
// self
// }
// fn content<'a>(&self, post: &'a Post) -> &'a str {
// &post.content
// }
// }

46
blog/src/main.rs Normal file
View File

@ -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());
// }

9
deadlocks/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "deadlocks"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

33
deadlocks/src/main.rs Normal file
View File

@ -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);
}

13
oop/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "oop"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
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]

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

@ -0,0 +1,27 @@
pub trait Draw {
fn draw(&self);
}
pub struct Screen {
pub components: Vec<Box<dyn Draw>>,
}
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!");
}
}

36
oop/src/main.rs Normal file
View File

@ -0,0 +1,36 @@
use gui::{Draw, Button, Screen};
struct SelectBox {
width: u32,
height: u32,
options: Vec<String>,
}
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();
}

9
threads/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "threads"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

23
threads/src/main.rs Normal file
View File

@ -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());
}