Skip to content

Commit df68efd

Browse files
committed
all of the things
1 parent 53f3763 commit df68efd

File tree

176 files changed

+1588
-1398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+1588
-1398
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ $RECYCLE.BIN/
4545
Network Trash Folder
4646
Temporary Items
4747
.apdisk
48+
49+
50+
#Added by cargo
51+
#
52+
#already existing elements are commented out
53+
54+
/target
55+
**/*.rs.bk
56+
Cargo.lock
57+
58+
59+
**/target

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "Chapter10/rusty-ml/fashion-mnist"]
2+
path = Chapter10/rusty-ml/fashion-mnist
3+
url = https://github.com/zalandoresearch/fashion-mnist.git

Chapter03/custom-build/out/.rustc_info.json

-1
This file was deleted.

Chapter03/custom-build/out/debug/.cargo-lock

Whitespace-only changes.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/.cargo-lock

Whitespace-only changes.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/.fingerprint/custom-build-0354a0e8029ace12/bin-custom-build

-1
This file was deleted.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/.fingerprint/custom-build-0354a0e8029ace12/bin-custom-build.json

-1
This file was deleted.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/.fingerprint/custom-build-0354a0e8029ace12/invoked.timestamp

-1
This file was deleted.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/custom-build.d

-1
This file was deleted.
Binary file not shown.
Binary file not shown.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/deps/custom_build.d

-5
This file was deleted.

Chapter03/custom-build/out/wasm32-unknown-unknown/debug/incremental/custom_build-2xa9c0dk2axzj/s-fbg7ypgl9l-1rxjcc6.lock

Whitespace-only changes.

Chapter03/hello_world/target/debug/.fingerprint/aho-corasick-1acf556bb0118ec7/lib-aho_corasick

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/aho-corasick-1acf556bb0118ec7/lib-aho_corasick.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/hello_world-63febb6f7e0e4f44/bin-hello_world

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/hello_world-63febb6f7e0e4f44/bin-hello_world.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/hello_world-63febb6f7e0e4f44/test-bin-hello_world

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/hello_world-63febb6f7e0e4f44/test-bin-hello_world.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/build

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/build-script-build-script-build

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/build-script-build-script-build.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/build.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/lib-kernel32

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/kernel32-sys-d6afa5bd3d7cfaef/lib-kernel32.json

-1
This file was deleted.
Binary file not shown.

Chapter03/hello_world/target/debug/.fingerprint/libc-690db683275b166f/lib-libc

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/libc-690db683275b166f/lib-libc.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/memchr-00c607621cc2d5ce/lib-memchr

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/memchr-00c607621cc2d5ce/lib-memchr.json

-1
This file was deleted.
Binary file not shown.

Chapter03/hello_world/target/debug/.fingerprint/rand-3056985a8c4e833e/lib-rand

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/rand-3056985a8c4e833e/lib-rand.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/regex-6b949f9945bac20b/lib-regex

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/regex-6b949f9945bac20b/lib-regex.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/regex-syntax-48ca0b541dad3633/lib-regex-syntax

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/regex-syntax-48ca0b541dad3633/lib-regex-syntax.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/thread-id-ef8852e88caafd34/lib-thread-id

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/thread-id-ef8852e88caafd34/lib-thread-id.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/thread_local-538d80d3b0c720b8/lib-thread_local

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/thread_local-538d80d3b0c720b8/lib-thread_local.json

-1
This file was deleted.
Binary file not shown.

Chapter03/hello_world/target/debug/.fingerprint/time-290cc8f9daa5ed64/lib-time

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/time-290cc8f9daa5ed64/lib-time.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/utf8-ranges-c0c68d6c77803563/lib-utf8-ranges

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/utf8-ranges-c0c68d6c77803563/lib-utf8-ranges.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/winapi-05520e89b7656dc3/lib-winapi

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/winapi-05520e89b7656dc3/lib-winapi.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/winapi-build-cdb03c7a2d525dd2/lib-build

-1
This file was deleted.

Chapter03/hello_world/target/debug/.fingerprint/winapi-build-cdb03c7a2d525dd2/lib-build.json

-1
This file was deleted.

Chapter03/hello_world/target/debug/_cargo-lock

Whitespace-only changes.

Chapter03/hello_world/target/debug/build/kernel32-sys-d6afa5bd3d7cfaef/output

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-6.1 MB
Binary file not shown.
Binary file not shown.

Chapter04/async-await/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ authors = ["Claus Matzinger <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
tokio = { version = "0.1", features = ["async-await-preview"] }
9-
hyper = "0.12"
8+
runtime = "0.3.0-alpha.6"
9+
surf = "1.0"

Chapter04/async-await/src/main.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
#![feature(await_macro, async_await)]
1+
use surf::Exception;
2+
use surf::http::StatusCode;
23

3-
use tokio::await;
4-
use tokio::prelude::*;
5-
use hyper::{Client, StatusCode};
6-
use std::time::Duration;
7-
use std::str;
8-
9-
async fn response_code(url: &str) -> StatusCode {
10-
let uri = url.parse().unwrap();
11-
let client = Client::new();
12-
let response = await!(client.get(uri)
13-
.timeout(Duration::from_secs(30))).unwrap();
14-
response.status()
4+
async fn response_code(url: &str) -> Result<StatusCode, Exception> {
5+
let res = surf::get(url).await?;
6+
Ok(res.status())
157
}
168

17-
fn main() {
18-
tokio::run_async(async {
19-
let status = await!(response_code("http://blog.x5ff.xyz"));
20-
println!("Got the status: {}", status);
21-
});
9+
#[runtime::main]
10+
async fn main() -> Result<(), Exception> {
11+
let url = "https://www.rust-lang.org";
12+
let status = response_code(url).await?;
13+
println!("{} responded with HTTP {}", url, status);
14+
Ok(())
2215
}

Chapter07/browser-rust/rust-digest/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
sha2 = "0.8"
13-
wasm-bindgen = "0.2.47"
13+
wasm-bindgen = "0.2.48"
1414

1515
[dependencies.web-sys]
16-
version = "0.3.4"
16+
version = "0.3.25"
1717
features = [
1818
'Document',
1919
'Element',

Chapter07/browser-rust/rust-digest/src/lib.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ pub fn start() -> Result<(), JsValue> {
2626

2727
#[wasm_bindgen]
2828
pub extern "C" fn digest_attach(data: String, elem_id: String) -> Result<(), JsValue> {
29-
let window = web_sys::window().expect("no global `window` exists");
30-
let document = window.document().expect("should have a document on window");
31-
//let body = document.body().expect("document should have a body");
32-
33-
// Manufacture the element we're gonna append
34-
let val = document.get_element_by_id(&elem_id).expect(&format!("Could not get element with id '{}'", elem_id));
35-
let signature = hex_digest(&data);
36-
val.set_inner_html(&signature);
37-
Ok(())
29+
web_sys::window().map_or(Err("No window found".into()), |win| {
30+
if let Some(doc) = win.document() {
31+
doc.get_element_by_id(&elem_id).map_or(Err(format!("No element with id {} found", elem_id).into()), |val|{
32+
let signature = hex_digest(&data);
33+
val.set_inner_html(&signature);
34+
Ok(())
35+
})
36+
}
37+
else {
38+
Err("No document found".into())
39+
}
40+
})
3841
}
3942
// No tests :(

Chapter09/cross-compile/src/main.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
#[cfg(target_arch = "x86")]
2+
const ARCH: &str = "x86";
3+
4+
#[cfg(target_arch = "x86_64")]
5+
const ARCH: &str = "x64";
6+
7+
#[cfg(target_arch = "mips")]
8+
const ARCH: &str = "mips";
9+
10+
#[cfg(target_arch = "powerpc")]
11+
const ARCH: &str = "powerpc";
12+
13+
#[cfg(target_arch = "powerpc64")]
14+
const ARCH: &str = "powerpc64";
15+
16+
#[cfg(target_arch = "arm")]
17+
const ARCH: &str = "ARM";
18+
19+
#[cfg(target_arch = "aarch64")]
20+
const ARCH: &str = "ARM64";
21+
122
fn main() {
223
println!("Hello, world!");
24+
println!("Compiled for {}", ARCH);
325
}

Chapter09/i2cdevice-drivers/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
rand = "0.5"
+15-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2+
mod sensor;
3+
4+
use sensor::{Bmx42Device, RawI2CDeviceMock, Thermometer};
5+
use std::thread::sleep;
6+
use std::time::Duration;
7+
8+
19
fn main() {
2-
println!("Hello, world!");
3-
}
10+
let mut device = Bmx42Device::new(RawI2CDeviceMock::new("/dev/i2c-1".into(), 0x5f)).unwrap();
11+
let pause = Duration::from_secs(1);
12+
loop {
13+
println!("Current temperature {} °C", device.temp_celsius().unwrap());
14+
sleep(pause);
15+
}
16+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::io;
2+
use rand::prelude::*;
3+
4+
pub trait Thermometer {
5+
fn temp_celsius(&mut self) -> Result<f32>;
6+
}
7+
8+
type Result<T> = std::result::Result<T, io::Error>;
9+
10+
enum Register {
11+
Calib0 = 0x00,
12+
Data = 0x01,
13+
}
14+
15+
#[allow(dead_code)]
16+
pub struct RawI2CDeviceMock {
17+
path: String,
18+
device_id: u8,
19+
}
20+
21+
22+
impl RawI2CDeviceMock {
23+
pub fn new(path: String, device_id: u8) -> RawI2CDeviceMock {
24+
RawI2CDeviceMock {
25+
path: path,
26+
device_id: device_id,
27+
}
28+
}
29+
30+
pub fn read(&self, register: u8) -> Result<u8> {
31+
let register = register as usize;
32+
if register == Register::Calib0 as usize {
33+
Ok(1_u8)
34+
} else { // register is the data register
35+
Ok(random::<u8>())
36+
}
37+
}
38+
}
39+
40+
pub struct Bmx42Device {
41+
raw: RawI2CDeviceMock,
42+
calibration: u8,
43+
}
44+
45+
impl Bmx42Device {
46+
pub fn new(device: RawI2CDeviceMock) -> Result<Bmx42Device> {
47+
let calib = device.read(Register::Calib0 as u8)?;
48+
Ok(Bmx42Device {
49+
raw: device,
50+
calibration: calib
51+
})
52+
}
53+
}
54+
55+
56+
impl Thermometer for Bmx42Device {
57+
fn temp_celsius(&mut self) -> Result<f32> {
58+
let raw_temp = self.raw.read(Register::Data as u8)?;
59+
// converts the result into something usable; from the specification
60+
Ok(((raw_temp as i8) << (self.calibration as i8)) as f32 / 10.0)
61+
}
62+
}

Chapter09/reading-hardware/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
tokio = "0.1"
11+
tokio-timer = "0.2"
12+
rand = "0.5"
+46-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
mod sensor;
2+
use tokio::prelude::*;
3+
use tokio::timer::Interval;
4+
5+
use sensor::{Bmx42Device, RawI2CDeviceMock, Thermometer};
6+
use std::time::{Duration, UNIX_EPOCH, SystemTime, Instant};
7+
use std::thread;
8+
9+
use std::sync::mpsc::channel;
10+
11+
fn current_timestamp_ms() -> u64 {
12+
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
13+
}
14+
15+
#[derive(Debug)]
16+
struct Reading {
17+
timestamp: u64,
18+
value: f32
19+
}
20+
21+
122
fn main() {
2-
println!("Hello, world!");
23+
let mut device = Bmx42Device::new(RawI2CDeviceMock::new("/dev/i2c-1".into(), 0x5f)).unwrap();
24+
25+
let (sx, rx) = channel();
26+
27+
thread::spawn(move || {
28+
while let Ok(reading) = rx.recv() {
29+
30+
// or batch and save/send to somewhere
31+
println!("{:?}", reading);
32+
}
33+
});
34+
let task = Interval::new(Instant::now(), Duration::from_secs(1))
35+
.take(5)
36+
.for_each(move |_instant| {
37+
let sx = sx.clone();
38+
let temp = device.temp_celsius().unwrap();
39+
let _ = sx.send(Reading {
40+
timestamp: current_timestamp_ms(),
41+
value: temp
42+
});
43+
Ok(())
44+
})
45+
.map_err(|e| panic!("interval errored; err={:?}", e));
46+
47+
tokio::run(task);
348
}

0 commit comments

Comments
 (0)