summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blog/Cargo.toml6
-rw-r--r--src/blog/src/lib.rs108
-rw-r--r--src/blog/src/main.rs21
-rw-r--r--src/oop/Cargo.toml6
-rw-r--r--src/oop/src/lib.rs27
-rw-r--r--src/oop/src/main.rs40
-rw-r--r--src/streams/Cargo.toml7
-rw-r--r--src/streams/src/main.rs63
-rw-r--r--src/thread2async/Cargo.toml7
-rw-r--r--src/thread2async/src/main.rs23
10 files changed, 308 insertions, 0 deletions
diff --git a/src/blog/Cargo.toml b/src/blog/Cargo.toml
new file mode 100644
index 0000000..790832e
--- /dev/null
+++ b/src/blog/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "blog"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/blog/src/lib.rs b/src/blog/src/lib.rs
new file mode 100644
index 0000000..c66e850
--- /dev/null
+++ b/src/blog/src/lib.rs
@@ -0,0 +1,108 @@
+pub struct Post {
+ state: Option<Box<dyn State>>,
+
+ content: String,
+}
+
+impl Post {
+ pub fn new() -> Post {
+ Post {
+ state: Some(Box::new(Draft {})),
+ content: String::new(),
+ }
+ }
+
+ pub fn add_text(&mut self, text: &str) {
+ self.content.push_str(text);
+ }
+
+ pub fn content(&self) -> &str {
+ self.state.as_ref().unwrap().content(self)
+ }
+
+ pub fn request_review(&mut self) {
+ if let Some(s) = self.state.take() {
+ self.state = Some(s.request_review())
+ }
+ }
+
+ pub fn approve(&mut self) {
+ if let Some(s) = self.state.take() {
+ self.state = Some(s.approve())
+ }
+ }
+
+ pub fn reject(&mut self) {
+ if let Some(s) = self.state.take() {
+ self.state = Some(s.reject())
+ }
+ }
+}
+
+trait State {
+ fn request_review(self: Box<Self>) -> Box<dyn State>;
+ fn approve(self: Box<Self>) -> Box<dyn State>;
+ fn reject(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 { approves: 0 })
+ }
+
+ fn approve(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+}
+
+struct PendingReview {
+ approves: u8,
+}
+
+impl State for PendingReview {
+ fn request_review(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn approve(self: Box<Self>) -> Box<dyn State> {
+ match self.approves {
+ 0 => {
+ Box::new(PendingReview { approves: 1 })
+ },
+ _ => Box::new(Published {}),
+ }
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ Box::new(Draft {})
+ }
+}
+
+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
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+}
diff --git a/src/blog/src/main.rs b/src/blog/src/main.rs
new file mode 100644
index 0000000..9b5563e
--- /dev/null
+++ b/src/blog/src/main.rs
@@ -0,0 +1,21 @@
+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.reject();
+ assert_eq!("", post.content());
+
+ post.request_review();
+ assert_eq!("", post.content());
+
+ post.approve();
+ post.approve();
+ assert_eq!("I ate a salad for lunch today", post.content());
+}
diff --git a/src/oop/Cargo.toml b/src/oop/Cargo.toml
new file mode 100644
index 0000000..40f6a81
--- /dev/null
+++ b/src/oop/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "oop"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/src/oop/src/lib.rs b/src/oop/src/lib.rs
new file mode 100644
index 0000000..1f260c0
--- /dev/null
+++ b/src/oop/src/lib.rs
@@ -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) {
+ // some code
+ }
+}
diff --git a/src/oop/src/main.rs b/src/oop/src/main.rs
new file mode 100644
index 0000000..5bd600e
--- /dev/null
+++ b/src/oop/src/main.rs
@@ -0,0 +1,40 @@
+use oop::{
+ Button,
+ Screen,
+ Draw,
+};
+
+struct SelectBox {
+ width: u32,
+ height: u32,
+ options: Vec<String>,
+}
+
+impl Draw for SelectBox {
+ fn draw(&self) {
+ // some code
+ }
+}
+
+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();
+}
diff --git a/src/streams/Cargo.toml b/src/streams/Cargo.toml
new file mode 100644
index 0000000..1b3c647
--- /dev/null
+++ b/src/streams/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "streams"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+trpl = "0.3.0"
diff --git a/src/streams/src/main.rs b/src/streams/src/main.rs
new file mode 100644
index 0000000..c7f9022
--- /dev/null
+++ b/src/streams/src/main.rs
@@ -0,0 +1,63 @@
+use std::{
+ time::Duration,
+ pin::{Pin, pin},
+};
+
+
+use trpl;
+use trpl::StreamExt;
+
+fn main() {
+ trpl::block_on(async {
+ let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ let iter = values.iter().map(|n| n * 2);
+ let mut stream = trpl::stream_from_iter(iter);
+
+ while let Some(value) = stream.next().await {
+ println!("The value was: {value}");
+ }
+
+ let (tx, mut rx) = trpl::channel();
+
+ let tx1 = tx.clone();
+ let tx1_fut = pin!(async move {
+ let vals = vec![
+ String::from("hi"),
+ String::from("from"),
+ String::from("the"),
+ String::from("future"),
+ ];
+
+ for val in vals {
+ tx1.send(val).unwrap();
+ trpl::sleep(Duration::from_secs(1)).await;
+ }
+ });
+
+ let rx_fut = pin!(async {
+ while let Some(value) = rx.recv().await {
+ println!("received '{value}'");
+ }
+ });
+
+ let tx_fut = pin!(async move {
+ // snip
+ let vals = vec![
+ String::from("more"),
+ String::from("messages"),
+ String::from("for"),
+ String::from("you"),
+ ];
+
+ for val in vals {
+ tx.send(val).unwrap();
+ trpl::sleep(Duration::from_secs(1)).await;
+ }
+ });
+
+ let futures: Vec<Pin<&mut dyn Future<Output = ()>>> =
+ vec![tx1_fut, rx_fut, tx_fut];
+
+ trpl::join_all(futures).await;
+ });
+}
diff --git a/src/thread2async/Cargo.toml b/src/thread2async/Cargo.toml
new file mode 100644
index 0000000..c9083e3
--- /dev/null
+++ b/src/thread2async/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "thread2async"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+trpl = "0.3.0"
diff --git a/src/thread2async/src/main.rs b/src/thread2async/src/main.rs
new file mode 100644
index 0000000..ccc3edb
--- /dev/null
+++ b/src/thread2async/src/main.rs
@@ -0,0 +1,23 @@
+use std::{
+ thread,
+ time::Duration,
+};
+
+use trpl;
+
+fn main() {
+ let (tx, mut rx) = trpl::channel();
+
+ thread::spawn(move || {
+ for i in 1..11 {
+ tx.send(i).unwrap();
+ thread::sleep(Duration::from_secs(1));
+ }
+ });
+
+ trpl::block_on(async {
+ while let Some(message) = rx.recv().await {
+ println!("{message}");
+ }
+ });
+}