Add final book project
This commit is contained in:
parent
12b2de3d68
commit
c5a08e2226
@ -43,6 +43,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/advanced_functions/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/macros/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/pancakes/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/hello/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
@ -173,6 +173,10 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/pancakes/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pancakes/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/pancakes/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/hello/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/hello/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/hello/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/hello/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
|
||||
@ -191,6 +195,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/generics/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/guessing_game/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/gui/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/hello/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/hello_macro/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/interior_mutability/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/lifetimes/target" />
|
||||
|
@ -14,51 +14,24 @@
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/pancakes/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
|
||||
<folding>
|
||||
<element signature="e#116#117#0" expanded="true" />
|
||||
<element signature="e#147#148#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="288">
|
||||
<caret line="18" column="5" selection-start-line="18" selection-start-column="5" selection-end-line="18" selection-end-column="5" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs">
|
||||
<entry file="file://$PROJECT_DIR$/hello/src/bin/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="336">
|
||||
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
|
||||
<caret line="21" column="31" selection-start-line="21" selection-start-column="31" selection-end-line="21" selection-end-column="31" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/Cargo.toml">
|
||||
<entry file="file://$PROJECT_DIR$/hello/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/pancakes/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
|
||||
<state relative-caret-position="1183">
|
||||
<caret line="83" lean-forward="true" selection-start-line="83" selection-end-line="83" />
|
||||
<folding>
|
||||
<element signature="e#194#195#0" expanded="true" />
|
||||
<element signature="e#211#212#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -92,12 +65,6 @@
|
||||
<component name="IdeDocumentHistory">
|
||||
<option name="CHANGED_PATHS">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/generics/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/lifetimes/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/adder/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/adder/tests/common/mod.rs" />
|
||||
<option value="$PROJECT_DIR$/adder/tests/integration_test.rs" />
|
||||
<option value="$PROJECT_DIR$/minigrep/poem.txt" />
|
||||
<option value="$PROJECT_DIR$/minigrep/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/minigrep/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/closures/src/main.rs" />
|
||||
@ -143,6 +110,12 @@
|
||||
<option value="$PROJECT_DIR$/pancakes/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/pancakes/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/hello/hello.html" />
|
||||
<option value="$PROJECT_DIR$/hello/404.html" />
|
||||
<option value="$PROJECT_DIR$/hello/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/hello/bin/main.rs" />
|
||||
<option value="$PROJECT_DIR$/hello/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/hello/src/bin/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -199,19 +172,12 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="hello" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="hello_macro_derive" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="hello_macro" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="hello_macro_derive" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="hello" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
@ -227,7 +193,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549898035962" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549907921290" />
|
||||
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
|
||||
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
|
||||
<property name="js.eslint.nodeInterpreter" value="project" />
|
||||
@ -243,6 +209,12 @@
|
||||
<property name="run.code.analysis.last.selected.profile" value="pProject Default" />
|
||||
<property name="settings.editor.selected.configurable" value="reference.settingsdialog.IDE.editor.colors.General" />
|
||||
</component>
|
||||
<component name="RecentsManager">
|
||||
<key name="MoveFile.RECENT_KEYS">
|
||||
<recent name="$PROJECT_DIR$/hello/src" />
|
||||
<recent name="$PROJECT_DIR$/hello/bin" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunDashboard">
|
||||
<option name="ruleStates">
|
||||
<list>
|
||||
@ -255,7 +227,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run pancakes">
|
||||
<component name="RunManager" selected="Cargo Command.Run hello">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -284,16 +256,6 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run advanced_lifetimes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package advanced_lifetimes --bin advanced_lifetimes" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_lifetimes" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run advanced_traits" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package advanced_traits --bin advanced_traits" />
|
||||
@ -314,6 +276,16 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run hello" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package hello --bin hello" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/hello" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run pancakes" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package pancakes --bin pancakes" />
|
||||
@ -333,11 +305,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run hello" />
|
||||
<item itemvalue="Cargo Command.Run pancakes" />
|
||||
<item itemvalue="Cargo Command.Run advanced_functions" />
|
||||
<item itemvalue="Cargo Command.Run advanced_types" />
|
||||
<item itemvalue="Cargo Command.Run advanced_traits" />
|
||||
<item itemvalue="Cargo Command.Run advanced_lifetimes" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -377,7 +349,7 @@
|
||||
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
|
||||
<editor active="true" />
|
||||
<layout>
|
||||
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.4957483" visible="true" weight="0.2540132" />
|
||||
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.4957483" visible="true" weight="0.2540132" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.5042517" side_tool="true" visible="true" weight="0.2540132" />
|
||||
<window_info id="Image Layers" order="2" />
|
||||
<window_info id="Designer" order="3" />
|
||||
@ -394,7 +366,7 @@
|
||||
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
|
||||
<window_info anchor="bottom" id="Version Control" order="8" />
|
||||
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
|
||||
<window_info anchor="bottom" id="Terminal" order="10" visible="true" weight="0.34264952" />
|
||||
<window_info active="true" anchor="bottom" id="Terminal" order="10" visible="true" weight="0.34264952" />
|
||||
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
|
||||
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
|
||||
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
|
||||
@ -408,59 +380,6 @@
|
||||
<option name="version" value="1" />
|
||||
</component>
|
||||
<component name="editorHistoryManager">
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="128">
|
||||
<caret line="8" column="32" lean-forward="true" selection-start-line="8" selection-start-column="32" selection-end-line="8" selection-end-column="32" />
|
||||
<folding>
|
||||
<element signature="e#127#128#0" expanded="true" />
|
||||
<element signature="e#171#172#0" expanded="true" />
|
||||
<element signature="e#382#383#0" expanded="true" />
|
||||
<element signature="e#420#421#0" expanded="true" />
|
||||
<element signature="e#782#783#0" expanded="true" />
|
||||
<element signature="e#826#827#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/point.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="544">
|
||||
<caret line="34" lean-forward="true" selection-start-line="34" selection-end-line="34" />
|
||||
<folding>
|
||||
<element signature="e#548#549#0" expanded="true" />
|
||||
<element signature="e#570#571#0" expanded="true" />
|
||||
<element signature="e#665#666#0" expanded="true" />
|
||||
<element signature="e#720#721#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/generics/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" column="9" lean-forward="true" selection-start-line="12" selection-start-column="9" selection-end-line="12" selection-end-column="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/lifetimes/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="912">
|
||||
<caret line="57" lean-forward="true" selection-start-line="57" selection-end-line="57" />
|
||||
<folding>
|
||||
<element signature="e#111#112#0" expanded="true" />
|
||||
<element signature="e#127#128#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/adder/tests/common/mod.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="48">
|
||||
<caret line="3" lean-forward="true" selection-start-line="3" selection-end-line="3" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/adder/tests/integration_test.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
@ -824,21 +743,21 @@
|
||||
<entry file="file://$PROJECT_DIR$/pancakes/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
|
||||
<caret line="9" selection-start-line="9" selection-end-line="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" />
|
||||
<caret line="12" selection-start-line="12" selection-end-line="12" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/pancakes/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" lean-forward="true" selection-start-line="9" selection-end-line="9" />
|
||||
<caret line="9" selection-start-line="9" selection-end-line="9" />
|
||||
<folding>
|
||||
<element signature="e#116#117#0" expanded="true" />
|
||||
<element signature="e#147#148#0" expanded="true" />
|
||||
@ -854,9 +773,48 @@
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/hello_macro_derive/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="432">
|
||||
<caret line="27" selection-start-line="27" selection-end-line="27" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello/404.html">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="176">
|
||||
<caret line="11" lean-forward="true" selection-start-line="11" selection-end-line="11" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="112">
|
||||
<caret line="7" lean-forward="true" selection-start-line="7" selection-end-line="7" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello/hello.html">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="160">
|
||||
<caret line="10" column="7" selection-start-line="10" selection-start-column="7" selection-end-line="10" selection-end-column="7" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="1183">
|
||||
<caret line="83" lean-forward="true" selection-start-line="83" selection-end-line="83" />
|
||||
<folding>
|
||||
<element signature="e#194#195#0" expanded="true" />
|
||||
<element signature="e#211#212#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/hello/src/bin/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="336">
|
||||
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
|
||||
<caret line="21" column="31" selection-start-line="21" selection-start-column="31" selection-end-line="21" selection-end-column="31" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
11
hello/404.html
Normal file
11
hello/404.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Hello!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Oops!</h1>
|
||||
<p>Sorry, I don't know what you're asking for.</p>
|
||||
</body>
|
||||
</html>
|
7
hello/Cargo.toml
Normal file
7
hello/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "hello"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
11
hello/hello.html
Normal file
11
hello/hello.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!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>
|
48
hello/src/bin/main.rs
Normal file
48
hello/src/bin/main.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
use std::net::TcpListener;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use hello::ThreadPool;
|
||||
|
||||
fn main() {
|
||||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
||||
let pool = ThreadPool::new(4);
|
||||
|
||||
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();
|
||||
}
|
115
hello/src/lib.rs
Normal file
115
hello/src/lib.rs
Normal file
@ -0,0 +1,115 @@
|
||||
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)()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThreadPool {
|
||||
workers: Vec<Worker>,
|
||||
sender: mpsc::Sender<Message>,
|
||||
}
|
||||
|
||||
type Job = Box<FnBox + Send + 'static>;
|
||||
|
||||
impl ThreadPool {
|
||||
/// Create a new ThreadPool.
|
||||
///
|
||||
/// The size is the number of threads in the pool.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// The `new` function will panic if the size is zero.
|
||||
pub fn new(size: usize) -> ThreadPool {
|
||||
assert!(size > 0);
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
ThreadPool {
|
||||
workers,
|
||||
sender,
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
println!("Sending terminate message to all workers.");
|
||||
|
||||
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 {
|
||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
|
||||
let thread = thread::spawn(move|| {
|
||||
loop {
|
||||
let message = receiver.lock().unwrap().recv().unwrap();
|
||||
|
||||
match message {
|
||||
Message::NewJob(job) => {
|
||||
println!("Worker {} got a job; executing.", id);
|
||||
job.call_box();
|
||||
},
|
||||
Message::Terminate => {
|
||||
println!("Worker {} was told to terminate", id);
|
||||
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Worker {
|
||||
id,
|
||||
thread: Some(thread),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user