summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--add/Cargo.lock11
-rw-r--r--advanced/adv-fn-closure/Cargo.lock11
-rw-r--r--advanced/adv-fn-closure/Cargo.toml8
-rw-r--r--advanced/adv-fn-closure/src/main.rs79
-rw-r--r--advanced/adv-lifetimes/Cargo.lock4
-rw-r--r--advanced/adv-traits/Cargo.lock4
-rw-r--r--advanced/adv-traits/src/main.rs161
-rw-r--r--advanced/adv-types/Cargo.lock4
-rw-r--r--advanced/adv-types/Cargo.toml7
-rw-r--r--advanced/adv-types/src/main.rs41
-rw-r--r--advanced/hello_macro/Cargo.toml7
-rw-r--r--advanced/hello_macro/src/lib.rs3
-rw-r--r--advanced/unsafe-rust/Cargo.lock4
-rw-r--r--blog/Cargo.lock4
-rw-r--r--concurrency/Cargo.lock4
-rw-r--r--functional/Cargo.lock4
-rw-r--r--hello_server/404.html19
-rw-r--r--hello_server/Cargo.lock4
-rw-r--r--hello_server/Cargo.toml7
-rw-r--r--hello_server/hello.html18
-rw-r--r--hello_server/src/bin/main.rs46
-rw-r--r--hello_server/src/lib.rs117
-rw-r--r--oop/Cargo.lock4
-rw-r--r--patterns/Cargo.lock4
-rw-r--r--smart-pointers/Cargo.lock4
26 files changed, 564 insertions, 17 deletions
diff --git a/.gitignore b/.gitignore
index 45b6de8..871d5ba 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
-*/target/*
+target/
*.txt
diff --git a/add/Cargo.lock b/add/Cargo.lock
new file mode 100644
index 0000000..64cb0d1
--- /dev/null
+++ b/add/Cargo.lock
@@ -0,0 +1,11 @@
+[[package]]
+name = "add-one"
+version = "0.1.0"
+
+[[package]]
+name = "adder"
+version = "0.1.0"
+dependencies = [
+ "add-one 0.1.0",
+]
+
diff --git a/advanced/adv-fn-closure/Cargo.lock b/advanced/adv-fn-closure/Cargo.lock
new file mode 100644
index 0000000..360607c
--- /dev/null
+++ b/advanced/adv-fn-closure/Cargo.lock
@@ -0,0 +1,11 @@
+[[package]]
+name = "adv-fn-closure"
+version = "0.1.0"
+dependencies = [
+ "hello_macro 0.1.0",
+]
+
+[[package]]
+name = "hello_macro"
+version = "0.1.0"
+
diff --git a/advanced/adv-fn-closure/Cargo.toml b/advanced/adv-fn-closure/Cargo.toml
new file mode 100644
index 0000000..6cd8dbd
--- /dev/null
+++ b/advanced/adv-fn-closure/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "adv-fn-closure"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+hello_macro = { path = "../hello_macro" }
diff --git a/advanced/adv-fn-closure/src/main.rs b/advanced/adv-fn-closure/src/main.rs
new file mode 100644
index 0000000..064f300
--- /dev/null
+++ b/advanced/adv-fn-closure/src/main.rs
@@ -0,0 +1,79 @@
+use hello_macro::HelloMacro;
+//use hello_macro_derive::HelloMacro;
+
+struct Pancakes;
+
+impl HelloMacro for Pancakes {
+ fn hello_macro() {
+ println!("pancakes");
+ }
+}
+
+fn main() {
+ Pancakes::hello_macro();
+}
+
+//fn add_one(x: i32) -> i32 {
+// x + 1
+//}
+//
+//fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
+// f(arg) + f(arg)
+//}
+
+
+//fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
+// Box::new(|x| x + 1)
+//}
+//
+//
+//enum Status {
+// Value(u32),
+// Stop,
+//}
+
+
+//#[macro_export]
+//macro_rules! vec {
+// ( $( $x:expr ), * ) => {
+// {
+// let mut temp_vec = Vec::new();
+// $(
+// temp_vec.push($x);
+// )*
+// temp_vec
+// }
+// };
+//}
+
+
+//#[some_attribute]
+//pub fn some_name(input: TokenStream) -> TokenStream {
+//}
+//
+//
+//fn main() {
+// let answer = do_twice(add_one, 5);
+//
+// println!("{}", answer);
+
+
+// let list_of_numbers = vec![1, 2, 3];
+// let list_of_strings: Vec<String> = list_of_numbers
+// .iter()
+// .map(ToString::to_string)
+// .collect();
+// dbg!(list_of_strings);
+
+
+// let list_of_statuses: Vec<Status> =
+// (0u32..20)
+// .map(Status::Value)
+// .collect();
+
+
+ //returns_closure();
+
+
+
+//}
diff --git a/advanced/adv-lifetimes/Cargo.lock b/advanced/adv-lifetimes/Cargo.lock
new file mode 100644
index 0000000..591810c
--- /dev/null
+++ b/advanced/adv-lifetimes/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-lifetimes"
+version = "0.1.0"
+
diff --git a/advanced/adv-traits/Cargo.lock b/advanced/adv-traits/Cargo.lock
new file mode 100644
index 0000000..4fb1ae5
--- /dev/null
+++ b/advanced/adv-traits/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-traits"
+version = "0.1.0"
+
diff --git a/advanced/adv-traits/src/main.rs b/advanced/adv-traits/src/main.rs
index 6ec0aac..659883c 100644
--- a/advanced/adv-traits/src/main.rs
+++ b/advanced/adv-traits/src/main.rs
@@ -8,27 +8,156 @@
// fn next(&mut self) -> Option<Self::Item>;
//}
//
-use std::ops::Add;
+//
+//#[derive(Debug, PartialEq)]
+//struct Point {
+// x: i32,
+// y: i32,
+//}
+//
+//impl Add for Point {
+// type Output = Point;
+//
+// fn add(self, other: Point) -> Point {
+// Point {
+// x: self.x + other.x,
+// y: self.y + other.y,
+// }
+// }
+//}
-#[derive(Debug, PartialEq)]
-struct Point {
- x: i32,
- y: i32,
-}
+//fn main() {
+// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
+// Point { x: 3, y: 3 });
+// println!("Done.");
+//}
+
+//trait Add<RHS=Self> {
+// type Output;
+//
+// fn add(self, rhs: RHS) -> Self::Output;
+//}
+
+//use std::ops::Add;
+//
+//struct Millimeters(u32);
+//struct Meters(u32);
+//
+//impl Add<Meters> for Millimeters {
+// type Output = Millimeters;
+//
+// fn add(self, other: Meters) -> Millimeters {
+// Millimeters(self.0 + (other.0 * 1000))
+// }
+//}
+
+
+//trait Pilot {
+// fn fly(&self);
+//}
+//
+//trait Wizard {
+// fn fly(&self);
+//}
+//
+//struct Human;
+//
+//impl Pilot for Human {
+// fn fly(&self) {
+// println!("Because I was inverted.");
+// }
+//}
+//
+//impl Wizard for Human {
+// fn fly(&self) {
+// println!("Fly, you fools.");
+// }
+//}
+//
+//impl Human {
+// fn fly(&self) {
+// println!("*feels real loose like a long-neck goose*");
+// }
+//}
+//
+//
+//trait Animal {
+// fn baby_name() -> String;
+//}
+//
+//struct Dog;
+//
+//impl Dog {
+// fn baby_name() -> String {
+// String::from("Spot")
+// }
+//}
+//
+//impl Animal for Dog {
+// fn baby_name() -> String {
+// String::from("puppy")
+// }
+//}
+
+
+//fn main() {
+// let person = Human;
+// person.fly();
+// Pilot::fly(&person);
+// Wizard::fly(&person);
+//
+//
+// println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
+//
+//
+// println!("Done.");
+//}
+//
+
+
+//use std::fmt;
+//
+//trait OutlinePrint: fmt::Display {
+// fn outline_print(&self) {
+// let output = self.to_string();
+// let len = output.len();
+// println!("{}", "*".repeat(len + 4));
+// println!("*{}*", " ".repeat(len + 2));
+// println!("* {} *", output);
+// println!("*{}*", " ".repeat(len + 2));
+// println!("{}", "*".repeat(len + 4));
+// }
+//}
+//
+//struct Point {
+// x: i32,
+// y: i32,
+//}
+//
+//impl OutlinePrint for Point {}
+//
+//impl fmt::Display for Point {
+// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+// write!(f, "({}, {})", self.x, self.y)
+// }
+//}
+//
+//fn main() {
+// let point = Point { x: 5, y: 6 };
+// point.outline_print();
+//}
+
+use std::fmt;
-impl Add for Point {
- type Output = Point;
+struct Wrapper(Vec<String>);
- fn add(self, other: Point) -> Point {
- Point {
- x: self.x + other.x,
- y: self.y + other.y,
- }
+impl fmt::Display for Wrapper {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "[{}]", self.0.join(", "))
}
}
fn main() {
- assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
- Point { x: 3, y: 3 });
- println!("Done.");
+ let w = Wrapper(vec![String::from("hello"), String::from("world")]);
+ println!("w = {}", w);
}
diff --git a/advanced/adv-types/Cargo.lock b/advanced/adv-types/Cargo.lock
new file mode 100644
index 0000000..a7ad78b
--- /dev/null
+++ b/advanced/adv-types/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "adv-types"
+version = "0.1.0"
+
diff --git a/advanced/adv-types/Cargo.toml b/advanced/adv-types/Cargo.toml
new file mode 100644
index 0000000..a3ca211
--- /dev/null
+++ b/advanced/adv-types/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "adv-types"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/advanced/adv-types/src/main.rs b/advanced/adv-types/src/main.rs
new file mode 100644
index 0000000..60d42b2
--- /dev/null
+++ b/advanced/adv-types/src/main.rs
@@ -0,0 +1,41 @@
+//use std::io::Error;
+//use std::fmt;
+
+//pub trait Write {
+//
+// fn write(&mut self, buf: &[u8]) -> Result<usize>;
+// fn flush(&mut self) -> Result<(), Error>;
+//
+// fn write_all(&mut self, buf: &[u8]) -> Result<()>;
+// fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
+//}
+
+//fn main() {
+// type Miles = i32;
+//
+// let x: i32 = 5;
+// let y: Miles = 5;
+//
+// println!("x + y = {}", x + y);
+
+
+//}
+
+//type Thunk = Box<dyn Fn() + Send + 'static>;
+//
+//let f: Thunk = Box::new(|| println!("hi"));
+//
+//fn takes_long_type(f: Thunk) {
+//}
+//
+//fn returns_long_type() -> Thunk {
+//}
+
+fn main() {
+ print!("forever");
+
+ loop {
+ print!("and ever ");
+ }
+}
+
diff --git a/advanced/hello_macro/Cargo.toml b/advanced/hello_macro/Cargo.toml
new file mode 100644
index 0000000..e812230
--- /dev/null
+++ b/advanced/hello_macro/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "hello_macro"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/advanced/hello_macro/src/lib.rs b/advanced/hello_macro/src/lib.rs
new file mode 100644
index 0000000..e747931
--- /dev/null
+++ b/advanced/hello_macro/src/lib.rs
@@ -0,0 +1,3 @@
+pub trait HelloMacro {
+ fn hello_macro();
+}
diff --git a/advanced/unsafe-rust/Cargo.lock b/advanced/unsafe-rust/Cargo.lock
new file mode 100644
index 0000000..fbd902c
--- /dev/null
+++ b/advanced/unsafe-rust/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "unsafe-rust"
+version = "0.1.0"
+
diff --git a/blog/Cargo.lock b/blog/Cargo.lock
new file mode 100644
index 0000000..8e79386
--- /dev/null
+++ b/blog/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "blog"
+version = "0.1.0"
+
diff --git a/concurrency/Cargo.lock b/concurrency/Cargo.lock
new file mode 100644
index 0000000..74a4d7a
--- /dev/null
+++ b/concurrency/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "concurrency"
+version = "0.1.0"
+
diff --git a/functional/Cargo.lock b/functional/Cargo.lock
new file mode 100644
index 0000000..ba41a73
--- /dev/null
+++ b/functional/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "functional"
+version = "0.1.0"
+
diff --git a/hello_server/404.html b/hello_server/404.html
new file mode 100644
index 0000000..b106938
--- /dev/null
+++ b/hello_server/404.html
@@ -0,0 +1,19 @@
+
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>
+ Hello!
+ </title>
+ </head>
+ <body>
+ <h1>
+ Oops!
+ </h1>
+ <p>
+ Sorry, Idk what you're asking for.
+ </p>
+ </body>
+</html>
+
diff --git a/hello_server/Cargo.lock b/hello_server/Cargo.lock
new file mode 100644
index 0000000..738010c
--- /dev/null
+++ b/hello_server/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "hello_server"
+version = "0.1.0"
+
diff --git a/hello_server/Cargo.toml b/hello_server/Cargo.toml
new file mode 100644
index 0000000..39057cc
--- /dev/null
+++ b/hello_server/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "hello_server"
+version = "0.1.0"
+authors = ["Adam Carpenter <53hornet@gmail.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/hello_server/hello.html b/hello_server/hello.html
new file mode 100644
index 0000000..869c8f9
--- /dev/null
+++ b/hello_server/hello.html
@@ -0,0 +1,18 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>
+ Hello!
+ </title>
+ </head>
+ <body>
+ <h1>
+ Hello!
+ </h1>
+ <p>
+ Hi from Rust
+ </p>
+ </body>
+</html>
+
diff --git a/hello_server/src/bin/main.rs b/hello_server/src/bin/main.rs
new file mode 100644
index 0000000..bcf1e46
--- /dev/null
+++ b/hello_server/src/bin/main.rs
@@ -0,0 +1,46 @@
+use hello_server::ThreadPool;
+use std::thread;
+use std::time::Duration;
+use std::io::prelude::*;
+use std::fs;
+use std::net::TcpStream;
+use std::net::TcpListener;
+
+fn main() {
+ let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
+ let pool = ThreadPool::new(4).unwrap();
+
+ for stream in listener.incoming().take(2) {
+ let stream = stream.unwrap();
+
+ pool.execute(|| {
+ handle_connection(stream);
+ });
+ }
+
+ println!("shutting down...");
+
+}
+
+fn handle_connection(mut stream: TcpStream) {
+ let mut buffer = [0; 512];
+ stream.read(&mut buffer).unwrap();
+ let get = b"GET / HTTP/1.1\r\n";
+ let sleep = b"GET /sleep HTTP/1.1\r\n";
+
+ let (status_line, filename) = if buffer.starts_with(get) {
+ ("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
+ }
+ else if buffer.starts_with(sleep) {
+ thread::sleep(Duration::from_secs(5));
+ ("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
+ }
+ else {
+ ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
+ };
+
+ let contents = fs::read_to_string(filename).unwrap();
+ let response = format!("{}{}", status_line, contents);
+ stream.write(response.as_bytes()).unwrap();
+ stream.flush().unwrap();
+}
diff --git a/hello_server/src/lib.rs b/hello_server/src/lib.rs
new file mode 100644
index 0000000..cd1f616
--- /dev/null
+++ b/hello_server/src/lib.rs
@@ -0,0 +1,117 @@
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::mpsc;
+use std::thread;
+
+enum Message {
+ NewJob(Job),
+ Terminate,
+}
+
+trait FnBox {
+ fn call_box(self: Box<Self>);
+}
+
+impl <F: FnOnce()> FnBox for F {
+ fn call_box(self: Box<F>) {
+ (*self)()
+ }
+}
+
+type Job = Box<FnBox + Send + 'static>;
+
+pub struct ThreadPool {
+ workers: Vec<Worker>,
+ sender: mpsc::Sender<Message>,
+}
+
+impl ThreadPool {
+ pub fn new(size: usize) -> Result<ThreadPool, &'static str> {
+ if size <= 0 {
+ return Err("failed to create pool");
+ }
+
+ let (sender, receiver) = mpsc::channel();
+
+ let receiver = Arc::new(Mutex::new(receiver));
+
+ let mut workers = Vec::with_capacity(size);
+
+ for id in 0..size {
+ workers.push(Worker::new(id, Arc::clone(&receiver))?);
+ }
+
+ Ok(ThreadPool {
+ workers,
+ sender,
+ })
+ }
+
+ pub fn spawn<F, T>(f: F) -> thread::JoinHandle<T>
+ where
+ F: FnOnce() -> T + Send + 'static,
+ T: Send + 'static
+ {
+ thread::spawn(f)
+ }
+
+ pub fn execute<F>(&self, f: F)
+ where
+ F: FnOnce() + Send + 'static
+ {
+ let job = Box::new(f);
+ self.sender.send(Message::NewJob(job)).unwrap();
+ }
+
+}
+
+impl Drop for ThreadPool {
+ fn drop(&mut self) {
+ for _ in &mut self.workers {
+ self.sender.send(Message::Terminate).unwrap();
+ }
+
+ println!("shutting down all workers...");
+
+ for worker in &mut self.workers {
+ println!("shutting down worker {}", worker.id);
+
+ if let Some(thread) = worker.thread.take() {
+ thread.join().unwrap();
+ }
+ }
+ }
+}
+
+struct Worker {
+ id: usize,
+ thread: Option<thread::JoinHandle<()>>,
+}
+
+impl Worker {
+ pub fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>)
+ -> Result<Worker, &'static str>
+ {
+ let thread = thread::spawn(move || {
+ loop {
+ let message = receiver.lock().unwrap().recv().unwrap();
+
+ match message {
+ Message::NewJob(job) => {
+ println!("worker {} got job, executing...", id);
+ job.call_box();
+ },
+ Message::Terminate => {
+ println!("worker {} was told to terminate...", id);
+ break;
+ },
+ }
+ }
+ });
+
+ Ok(Worker {
+ id,
+ thread: Some(thread),
+ })
+ }
+}
diff --git a/oop/Cargo.lock b/oop/Cargo.lock
new file mode 100644
index 0000000..fd3ef4a
--- /dev/null
+++ b/oop/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "oop"
+version = "0.1.0"
+
diff --git a/patterns/Cargo.lock b/patterns/Cargo.lock
new file mode 100644
index 0000000..a233623
--- /dev/null
+++ b/patterns/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "patterns"
+version = "0.1.0"
+
diff --git a/smart-pointers/Cargo.lock b/smart-pointers/Cargo.lock
new file mode 100644
index 0000000..7dec816
--- /dev/null
+++ b/smart-pointers/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "smart-pointers"
+version = "0.1.0"
+